How do I add days correctly to a date in Java?

Posted on 2014-07-31
Last Modified: 2014-07-31
I have to convert a method from C# to Java.  The method for adding days to a date is a little different, but, even so, I can't produce the same result.  Here is what I mean.

C# code:
DateTime d = new DateTime(1000,1,1);
Resulting Date (not concerned with the time): July 6, 1968, which is correct.

Java code:
Calendar calendar = Calendar.getInstance();
calendar.set(1000, Calendar.JANUARY, 1);
calendar.add(calendar.DATE, 353741);
Date d = new Date();
d = calendar.getTime();
Resulting Date (not concerned with the time): July 11, 1968, which is incorrect.

What gives?  What am I missing?
Question by:spazjr01
LVL 35

Accepted Solution

mccarl earned 500 total points
ID: 40233258
You are adding days correctly, the problem will more than likely be a result of what Calendar system C# and Java are using and exactly how those calendar are implemented. I don't have any experience with C# date time handling so I don't know what calendar it uses or how it has been implemented.

However, with Java, it is at least possible that your call to "Calendar.getInstance()" can return different instances, I only know of and have used one, the GregorianCalendar class. If you look at the Javadoc for GregorianCalendar or any other reference to historical calendar systems, you will see that back in history dates and calendar aren't as simple as "one day after the next". Calendar systems changed and different calendars are used in different parts of the world, and even when the same calendar is used, the dates on which changes happen are all different.

SO....  When you start doing operations on dates as far back as 1st January, 1000 you really need to be 100% sure of which calendar system is in use (and exactly how that system has been implemented in code) to be able to give desired results. The easy fix for this is to NOT base your dates on a date that far back in history. If this is not an option, say if you have existing data that stores the date as an offset from 1/1/1000, you could potentially (depending on the range of dates that you expect to be using) just manually correct for the different calendars between C# and Java.

Author Closing Comment

ID: 40233334
This wasn't a complete solution. However, this information led me to the complete solution.  That is, GregorianCalendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems with the support of a single discontinuity, which corresponds by default to the Gregorian date when the Gregorian calendar was instituted (October 15, 1582 in some countries, later in others). The cutover date may be changed by the caller by calling setGregorianChange().  In other words, the Calendar object uses the Julian calendar until October 15, 1582 by default.  So, once you have a starting point that precedes that date (such as, 1000, 1,1), you have two calendar types used in the object and it skews the date.  To solve this, you can go completely Gregorian or completely Julian.  Going completely Gregorian solved my problem,  the code for doing this is below:

        GregorianCalendar calendar = (GregorianCalendar)GregorianCalendar.getInstance();
        calendar.setGregorianChange(new Date(Long.MIN_VALUE));
        calendar.set(1000, Calendar.JANUARY, 1);  
        calendar.add(calendar.Date, 353741);
        Date d = new Date();
        d = calendar.getTime();

Now I get July 6, 1968 for the correct answer.  Thanks for the tip on calendars to point me in the right direction.  I've been struggling with this problem for hours.

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question