Android Calendar: first and last day of week -> zero month - java

I want to get the date of the first monday of the current week and the first friday of the current week.
I tried it this way:
Calendar calendarFirstDay = Calendar.getInstance(Locale.GERMANY);
Calendar calendarLastDay = Calendar.getInstance(Locale.GERMANY);
Date now = new Date();
calendarFirstDay.setTime(now);
calendarLastDay.setTime(now);
calendarFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendarLastDay.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
MyTextView.setText(calendarFirstDay.get(Calendar.DATE) + "." + calendarFirstDay.get(Calendar.MONTH) + "." + calendarFirstDay.get(Calendar.YEAR) + " - " + calendarLastDay.get(Calendar.DATE) + "." + calendarLastDay.get(Calendar.MONTH) + "." + calendarLastDay.get(Calendar.YEAR));
If I try this script today (Sunday, 26.1.2014), the output is the following:
20.0.2014 - 24.0.2014
Correct would be 20.1.2014 - 24.1.2014
Does somebody knows why my month is zero?

From the JavaDocs:
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
Thus the first month (January) has a MONTH value of 0, and not 1 (as you'd expect).
There's a much better solution though: use a DateFormat or SimpleDateFormat to format dates as text. That way, you simply don't have to worry about this and let the DateFormat take care of it. For example:
DateFormat myFormat = new SimpleDateFormat("dd.MM.yyyy");
MyTextView.setText(
myFormat.format(calendarFirstDay.getTime()) + " - " +
myFormat.format(calendarLastDay.getTime())
);

As stated in
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set(int,%20int,%20int)
if you use this method, January will be 0 and Decmember will be 11, so you should just add 1 to your month.
EDIT: You are using the get method but it's probably 0-based, too.

Related

Java - Getting same day of week for next year

I was looking for a way to make optimum of Java 8's LocalDate to find next year's date which falls on same day of week.
For example today is 1rd March 2022 - Tuesday. Assume this is the input date, the the output date that I expect to get is 28th Feb 2023, because its the same day (Tuesday) for next year
One option that I tried was adding 364 days as shown in the example
public static void main(String args[]){
LocalDate currentDate= LocalDate.now();
System.out.println("Current Date: " + currentDate + " Day" currentDate.getDayOfWeek().name());
System.out.println("Next Year's date: " + currentDate.plusDays(364) + " Next year day: " +currentDate.plusDays(364).getDayOfWeek().name());
}
This works but may have drawback with leap years.
Other option is to add 1 year to current date and do a while loop check for dayOfWeek for each day - 1
Is there a better way to do this?

Java Calendar generates invalid month and date

Plase have a look at the below code
Calendar date = Calendar.getInstance();
initialClientLetterDate.setText(date.get(Calendar.YEAR)+"/"+date.get(Calendar.MONTH)+"/"+date.get(Calendar.DAY_OF_WEEK));
This generates the invalid "month" and "date". The output is 2014/09/06. Why is this? I just wanted to get current year, date and month.
You are using DAY_OF_WEEK which is 6 for FRIDAYand MONTH starts from 0 not 1 so you have to add 1 in it.You can use DAY_OF_MONTH instead of DAY_OF_WEEK.
You can do like as below code. You should not forgot to set locale.
public static void main(String args[]) {
Calendar date = Calendar.getInstance(Locale.US);
System.out.println(date.get(Calendar.YEAR) + "/"
+ (date.get(Calendar.MONTH) + 1) + "/"
+ date.get(Calendar.DAY_OF_MONTH));
}
DAY_OF_WEEK Returns an int (starting at 1?) for the day in the week so Friday would be 6, Month does the same except starting at 0 so 09 would be October.
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#DAY_OF_WEEK
date.get(Calendar.YEAR) = Gives the current year
date.get(Calendar.MONTH) = Gives the month of the year as an integer from 0 to 11, where 0 = Jan and 11 = dec
date.get(Calendar.DAY_OF_WEEK) = Gives the day of the week from 0 to 6, where 0 = Monday
So if you want to get the current date you must do
date.get(Calendar.YEAR)+"/"+(date.get(Calendar.MONTH)+1)+"/"+date.get(Calendar.DAY_OF_MONTH)

Date difference from current date in java

I am developing an application in JAVA swing, in that I wanted the date difference from current date like if today is 16/04/2013 then it should return 15/04/2013. I have tried the following code:
Calendar cal = new GregorianCalendar();
Calendar cal2 = new GregorianCalendar();
cal.roll(Calendar.DAY_OF_YEAR, -1);
//if within the first 30 days, need to roll the year as well
if(cal.after(cal2)){
cal.roll(Calendar.YEAR, -1);
}
System.out.println("Year " + cal.get(Calendar.YEAR));
System.out.println("Month " + cal.get(Calendar.MONTH));
System.out.println("Day " + cal.get(Calendar.DAY_OF_MONTH));
In this code I was expecting to get one day back date. But instead I am getting one month back date.
Ex. if today is 16/04/2013, the expected output is 15/04/2013, but I am getting 15/03/2013 ( one month one day back) as an output.
You dont need any manual manipulations, Calendar will do all necessary date arithmetic automatically, simply do this
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DATE, -1);
Note that months in Calendar start from 0 so April is 3
That's a classic example why java.util.Date implementation sucks: Months numeration starts in zero:
0-> January
1-> February
2-> March
3-> April.
What you mean:
new Date(10,1,2013) //10th of January of 2013
What you get: 10th of February of 3983 (1970+2013)

Android SimpleDateFormat problem

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse("2011-09-13");
Log.e(MY_DEBUG_TAG, "Output is "+ date.getYear() + " /" + date.getMonth() + " / "+ (date.getDay()+1));
Is out putting
09-13 14:20:18.740: ERROR/GoldFishActivity(357): Output is 111 /8 / 3
What is the issue?
The methods you are using in the Date class are deprecated.
You get 111 for the year, because getYear() returns a value that is the result of subtracting 1900 from the year i.e. 2011 - 1900 = 111.
You get 3 for the day, because getDay() returns the day of the week and 3 = Wednesday. getDate() returns the day of the month, but this too is deprecated.
You should use the Calendar class instead.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse("2011-09-13");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Log.e(MY_DEBUG_TAG, "Output is "+ cal.get(Calendar.YEAR)+ " /" + (cal.get(Calendar.MONTH)+1) + " / "+ cal.get(Calendar.DAY_OF_MONTH));
Read the javadoc of java.util.Date carefully.
getYear returns the number of years since 1900.
getMonth returns the month, starting from 0 (0 = January, 1 = February, etc.).
getDay returns the day of week (0 = Sunday, 1 = Monday, etc.), not the day of month.
And all these methods are deprecated. You shouldn't use them anymore.

Challenging Java/Groovy Date Manipulation

I have a bunch of dates formatted with the year and week, as follows:
2011-10
The week value is the week of the year(so 1-52). From this week value, I need to output something like the following:
Mar 7
Explicitly, I need the Month that the given week is in, and the date of the first Monday of that week. So in other words it is saying that the 10th week of the year is the week of March 7th.
I am using Groovy. What kind of date manipulation can I do to get this to work?
Here's a groovy solution:
use(groovy.time.TimeCategory) {
def (y, w) = "2011-10".tokenize("-")
w = ((w as int) + 1) as String
def d = Date.parse("yyyy-w", "$y-$w") + 1.day
println d.format("MMM dd")
}
Use a GregorianCalendar (or Joda, if you don't mind a dependency)
String date = "2011-10";
String[] parts = date.split("-");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(parts[0]));
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.WEEK_OF_YEAR, Integer.parseInt(parts[1])+1);
DateFormat df = new SimpleDateFormat("MMM d");
System.out.println(df.format(cal.getTime()) + " (" + cal.getTime() + ")");
EDIT: Added +1 to week, since calendar uses zero-based week numbers
Date date = new SimpleDateFormat("yyyy-w", Locale.UK).parse("2011-10");
System.out.println(new SimpleDateFormat("MMM d").format(date));
The first line returns first day of the 10th week in British Locale (March 7th). When Locale is not enforced, the results are dependent on default JVM Locale.
Formats are explained here.
You can use SimpleDateFormat, just like in java. See groovyconsole.appspot.com/script/439001
java.text.DateFormat df = new java.text.SimpleDateFormat('yyyy-w', new Locale('yourlocale'))
Date date = df.parse('2011-10')
To add a week, simply use Date date = df.parse('2011-10')+7
You don't need to set the Locale if your default Locale is using Monday as the first day of week.

Categories