Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GregorianCalendar add error

Posted on 2004-10-15
5
Medium Priority
?
260 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses

916 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