Solved

GregorianCalendar add error

Posted on 2004-10-15
5
233 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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