Reputation: 1446
My requirement is determining the next date based on frequency of schedule. So if frequency is DAILY and the first date is 25-Oct-2015 23:59:59, the next duedate should be exactly 24 hours apart ie 26-Oct-2015 23:59:59
Calendar.add(int field, int amount) seems to be taking care of the same Eg:
DAILY frequency -- calendar.add(Calendar.DATE, 1);
WEEKLY frequency -- calendar.add(Calendar.DATE, 7);
MONTHLY frequency -- calendar.add(Calendar.MONTH, 1);
The following is the code abstract of the same:
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
==================================================
Thu Oct 29 17:17:26 IST 2015 -- in all cases diff is 24 hrs
Fri Oct 30 17:17:26 IST 2015
Sat Oct 31 17:17:26 IST 2015
Sun Nov 01 17:17:26 IST 2015
Mon Nov 02 17:17:26 IST 2015
Tue Nov 03 17:17:26 IST 2015
Wed Nov 04 17:17:26 IST 2015
Thu Nov 05 17:17:26 IST 2015
Fri Nov 06 17:17:26 IST 2015
In case of DAILY frequency and server being in Eastern time (EDT), a few anomaly was ocuuring with add()
As of Nov-1, DST settings change the same are reflected in add
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
-------------------------------------
Wed Oct 28 17:18:14 IST 2015
Thu Oct 29 17:18:14 IST 2015
Fri Oct 30 17:18:14 IST 2015
Sat Oct 31 17:18:14 IST 2015
Sun Nov 01 18:18:14 IST 2015 -- here diff is of 24 + 1 hr
Mon Nov 02 18:18:14 IST 2015
Tue Nov 03 18:18:14 IST 2015
Wed Nov 04 18:18:14 IST 2015 -- else everywhere diff is 24 hours
Thu Nov 05 18:18:14 IST 2015
Fri Nov 06 18:18:14 IST 2015
In case by first date is 25-Oct-2015 23:59:59, in this case, the extra 1 hour shift is causing anomaly as after 31-Oct-2015 23:59:59, the next date is 2-Nov-2015 00:59:59
Further observing the Code, found out that
// The rest of the fields (week, day or AM_PM fields)
// require time zone offset (both GMT and DST) change
// adjustment.
Actually the server is in EDT, where I'm getting following I/O relation. I merely tried to debug it on my local instance which is in IST.
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
Wed Oct 28 08:09:48 EDT 2015
Thu Oct 29 08:09:48 EDT 2015
Fri Oct 30 08:09:48 EDT 2015
Sat Oct 31 08:09:48 EDT 2015
Sun Nov 01 08:09:48 EST 2015
Mon Nov 02 08:09:48 EST 2015
Tue Nov 03 08:09:48 EST 2015
Wed Nov 04 08:09:48 EST 2015
Thu Nov 05 08:09:48 EST 2015
Fri Nov 06 08:09:48 EST 2015
What should be a reliable way of using a library to ensure that my dates generated are in proper sequence.
Upvotes: 1
Views: 386
Reputation: 4216
I think the anomaly is because, the daylight is ending for EDT zone on that day(31st October night). NO such problem is occurring for IST as India do not have daylight saving.
Upvotes: 0
Reputation: 4216
Try the below code. The below code doesn't simpley add 1 to DAY or MONTH. It adds milliseconds for a day in order to get the proper result.
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
Calendar calendar = Calendar.getInstance();
formatter.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
calendar.setTimeInMillis(System.currentTimeMillis());
long time = calendar.getTimeInMillis();
for(int i=0;i<10;i++) {
System.out.println(formatter.format(calendar.getTime()));
time = time + 86400000;
calendar.setTimeInMillis(time);
}
}
Definitely formatting of the date can be changed by changing the line -
new SimpleDateFormat("E MMM dd hh:mm:ss z yyyy");
It gives output -
Di Okt 27 08:15:59 EDT 2015
Mi Okt 28 08:15:59 EDT 2015
Do Okt 29 08:15:59 EDT 2015
Fr Okt 30 08:15:59 EDT 2015
Sa Okt 31 08:15:59 EDT 2015
So Nov 01 07:15:59 EST 2015
Mo Nov 02 07:15:59 EST 2015
Di Nov 03 07:15:59 EST 2015
Mi Nov 04 07:15:59 EST 2015
Do Nov 05 07:15:59 EST 2015
Upvotes: 0