Solved

GregorianCalendar add error

Posted on 2004-10-15
5
236 Views
Last Modified: 2012-08-14
I would like to add a day to a date using GregorianCalendar but the add method ins't working properly ...
It add 12 hours to the date instead of a day ...

Please, take a look at the code bellow:

------------------------------------------------------
        GregorianCalendar calcDate = new GregorianCalendar();

       // date is : Sat Oct 16 00:00:00 BRT 1993 and is a parameter
        calcDate.setTime(date);

        calcDate.add(Calendar.DAY_OF_MONTH, 1);        
        calcDate.set(Calendar.HOUR, 0);
        calcDate.set(Calendar.MINUTE, 0);
        calcDate.set(Calendar.SECOND, 0);
        calcDate.set(Calendar.MILLISECOND, 0);

        log.debug("dateAdd Diary = " + date + " - " + calcDate.getTime());
--------------------------------------------------------
It print's: dateAdd Diary = Sat Oct 16 00:00:00 BRT 1993 - Sat Oct 16 12:00:00 BRT 1993

The set method isn't working too...

Please, help me!

0
Comment
Question by:barantes
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12326012
works fine here
0
 
LVL 3

Expert Comment

by:nesnemis
ID: 12326224
works for me too, what version of java are you using?

java -version in your dos-prompt or unix-shell
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12326248
Well,

Try to modify the format of your windows's date setting from Start>Control Panel>Regional Options.

Basically your codes should work fine as what objects said.

I hope that helps.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12326838
Time is subject to daylight saving, and that depends on your timezone. It so happens that the time of the year you're parsing is on the cusp of a daylight saving switch. If you wish to be sure that an addition to a date appears exactly as you expect it to, you need to use UTC. Run the following and you'll see:


try {
      TimeZone uct = TimeZone.getTimeZone("UCT");
      TimeZone bet = TimeZone.getTimeZone("BET");
      SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");

      sdf.setTimeZone(bet);
      Date d = sdf.parse("16-10-1993 00:00:00");
      setDate(d, bet);


      sdf.setTimeZone(uct);
      d = sdf.parse("16-10-1993 00:00:00");
      setDate(d, uct);
}
catch(Exception e) {
      e.printStackTrace();
}

..................

static void setDate(Date date, TimeZone tz) {
      TimeZone.setDefault(tz);
      GregorianCalendar calcDate = new GregorianCalendar();


      // (Your original comment follows)
      // date is : Sat Oct 16 00:00:00 BRT 1993 and is a parameter
      calcDate.setTime(date);

      calcDate.add(Calendar.DAY_OF_MONTH, 1);
      calcDate.set(Calendar.HOUR, 0);
      calcDate.set(Calendar.MINUTE, 0);
      calcDate.set(Calendar.SECOND, 0);
      calcDate.set(Calendar.MILLISECOND, 0);

      //log.debug("dateAdd Diary = " + date + " - " + calcDate.getTime());
      System.out.println("dateAdd Diary = " + date + " - " + calcDate.getTime());
}


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12338778
8-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 119
GUI builder for Eclipse? 8 27
Java Options in Zimbra Server 2 44
MySqlDump not dumping triggers 1 15
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

912 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

16 Experts available now in Live!

Get 1:1 Help Now