Solved

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

Posted on 2014-07-31
2
301 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 36

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

622 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