Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GregorianCalendar add error

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

715 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