Solved

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

Posted on 2014-07-31
2
278 Views
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);
d.AddDays(353741);
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?
0
Comment
Question by:spazjr01
2 Comments
 
LVL 35

Accepted Solution

by:
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.
0
 

Author Closing Comment

by:spazjr01
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.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now