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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

860 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