File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to add Date  to 1 more day? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to add Date  to 1 more day?" Watch "How to add Date  to 1 more day?" New topic
Author

How to add Date to 1 more day?

chaohua wang
Ranch Hand

Joined: Dec 22, 2002
Posts: 62
Hi, Folks,

I want to add 1 more day for a Date.
I tied to to do like this :

Date completionDate =new Date(completionDate.getTime() +12);

but it doesn't work, 12 hours is not enough?
Please help, thank you very much!

Chwang
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Have a look at the Calendar class. This is the prefered way of manipulating dates in Java. You'll notice there is an add() method that allows you to add time to your date.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Originally posted by chaohua wang:
Date completionDate =new Date(completionDate.getTime() +12);

but it doesn't work, 12 hours is not enough?
Please help, thank you very much!

With that line of code you are adding 12 milliseconds to the date - not 12 hours.

Also, a day is 24 hours, not 12 hours.
[ October 03, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Jesper Young:
Also, a day is 24 hours, not 12 hours.
Except in places where daylight saving time applies, sometimes it is 23 or 25 hours. Which is why you should use the Calendar object for date and time arithmetic, as Paul Sturrock said.
chaohua wang
Ranch Hand

Joined: Dec 22, 2002
Posts: 62
Thank you very much, I got it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to add Date to 1 more day?