The time when one day is deducted from GregorianCalendar

I am using the following code:

Calendar calendar = new GregorianCalendar(0,0,0); calendar.set(Calendar.YEAR, 1942); calendar.set(Calendar.MONTH, 3); calendar.set(Calendar.DAY_OF_MONTH, 4); Date date1 = calendar.getTime(); calendar.add(Calendar.DAY_OF_MONTH, -1); Date date2 = calendar.getTime(); System.out.println(date1 + "\n" + date2); 

This code output follows:

 Sat Apr 04 00:00:00 EEST 1942 Fri Apr 03 01:00:00 EEST 1942 

Actually, I subtract 1 day, and the time should be saved. But why does the second line of output contain 1 hour per time, while it should be 0?

EDIT:

I am currently testing my code in the time zone of Europe / Helsinki.

+4
source share
5 answers

I assume you are using the Finnish time zone. In Finland, daylight saving time was introduced in 1942, adjusting the hours from April 2, 23:59:59 to April 3, 1:00:00. The time interval April 3, 0:00:00 to 0:59:59 did not exist, so the Java calendar gives the best result.

+17
source

The library seems to be guessing "Daylight Saving"

It is difficult to determine the “right answer” for what you are doing. Eastern European summer time did not exist in 1942. Even in the United States, where the library code might have been written, there was no daylight saving time this year, as the War of War Act of 1942 forced the entire time period to be reset.

The usual SO answer for Java date and time problems is to use Joda Time. I am not sure if this will help here, but it may be.

My assumption is that the program does not correctly apply the DST shift, or simply best guesses what could have happened if EEST existed in 1942.

+2
source

Are you using the latest version of java? Check this out because on my java installation (1.6.0_16) it works fine, output:

 Sat Apr 04 00:00:00 GMT 1942 Fri Apr 03 00:00:00 GMT 1942 

Sun usually updates the database over time in java updates, so check that you are using the latest version!

Or, another:

Perhaps this is due to summer time? The US government introduced daylight saving time during World War II. Could this be the reason in your time zone, but not mine?

+1
source

The result you get may depend on the time zone in which you run the code. In your local time zone, this 1-day interval may include daylight saving time or some other one-time clock synchronization for your country / region.

0
source

I will write another answer to the serdev comment, since it is a little difficult to include a lot of information in the comment.

You say you need to iterate over dates and find distances, and if you make some assumptions that should be valid for your locale or time zone, it's not that difficult. If you ignore the time and use your code to subtract one day from Sat. 04/04/1942, you will receive Fri Fri 03 1942, as expected. If you need to find the number of days between dates, I would calculate it myself:

 // set two Calendars to April 1st, 1942 and April 5th, 1942 (both 0:00) Calendar cal1 = new GregorianCalendar(1942, 3, 1); Calendar cal2 = new GregorianCalendar(1942, 3, 5); // divide the difference in ms by the number of ms in 24 hours and round the result long diff = Math.round((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / (24.*60*60*1000)); 

A simple division with the Finnish time zone will give a difference of 3,958 days, but rounding gives the correct result in 4 days.

For this to work, you must, however, assume that the dates are continuous and that your time zone does not change more than 11 hours between the start and end dates. This is not always the case because some of the participating countries around the international date line have been “switched”. The most recent case was that parts of Kiribati missed the whole country on one side of the date line on December 31, 1994. Before the change, the nation’s time zones covered only a few hours, but since they were separated by date, the country actually had two different dates.

0
source

All Articles