Solved

GregorianCalendar add error

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why is enum singleton a better approach than static factory 3 67
hashmap order 17 63
batch vs regular insert in spring DAO 2 55
Java Inheritance super keyword use 8 74
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

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