In both cases, the date what I want to set in this example in millis is 1395227280627
, and in human readable format is 3/19/2014 12:08:00 PM
1) Here is the first approach, which does not work correctly:
Calendar ret1=GregorianCalendar.getInstance();
ret1.set(Calendar.YEAR, year);
ret1.set(Calendar.MONTH, month);
ret1.set(Calendar.DAY_OF_YEAR, day);
ret1.set(Calendar.HOUR_OF_DAY, hour);
ret1.set(Calendar.MINUTE, minute);
ret1.set(Calendar.SECOND, 0);
Log.e("Time in millis:", Long.toString(ret1.getTimeInMillis()));
The output of this approach is 1390129680626
in millis and in human readable format is 1/19/2014 12:08:00 PM
. The issue is, the month is not getting correctly set.
2) The second approach, which correctly works is:
Calendar ret2=GregorianCalendar.getInstance();
ret2.set(year, month, day, hour, minute, 0);
Log.e("Time in millis:", Long.toString(ret2.getTimeInMillis()));
The output of this approach is 1395227280627
in millis, and 3/19/2014 12:08:00 PM
in human readable format, so exactly what I want. I was curious on what gone wrong, and after some research, loads of ppl stated that the java calendar is horrible, and has a lot of very weird thing, including some broken parts. I have started to backtrack the approach that works, here is the result:
Backtracking calendar.set(year, month, day, hour, minute, second)
/**
* Sets the year, month, day of the month, hour of day, minute, and second fields.
* Other fields are not changed; call {@link #clear} first if this is not desired.
* The month value is 0-based, so it may be clearer to use a constant like {@code JANUARY}.
*/
public final void set(int year, int month, int day, int hourOfDay, int minute, int second) {
set(year, month, day, hourOfDay, minute);
set(SECOND, second);
}
So the second
part translates to exactly what I did in the first approach, check.
Backtracking calendar.set(year, month, day, hourOfDay, minute)
/**
* Sets the year, month, day of the month, hour of day, and minute fields.
* Other fields are not changed; call {@link #clear} first if this is not desired.
* The month value is 0-based, so it may be clearer to use a constant like {@code JANUARY}.
*/
public final void set(int year, int month, int day, int hourOfDay, int minute) {
set(year, month, day);
set(HOUR_OF_DAY, hourOfDay);
set(MINUTE, minute);
}
So the hourOfDay
and minute
part translates to exactly what I did in the first approach, check.
Backtracking calendar.set(year, month, day)
/**
* Sets the year, month, and day of the month fields.
* Other fields are not changed; call {@link #clear} first if this is not desired.
* The month value is 0-based, so it may be clearer to use a constant like {@code JANUARY}.
*/
public final void set(int year, int month, int day) {
set(YEAR, year);
set(MONTH, month);
set(DATE, day);
}
So the year
, month
and day
part exactly translates to what I did in the first approach.
The conclusion is, every call I make in the first approach gets called in the second approach in exactly the same way, so I don't understand, what is happenning. Can anyone help me out here, and explain why is this happenning?