Solved

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

Posted on 2014-07-31
2
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

752 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