aspose file tools*
The moose likes Beginning Java and the fly likes How to get tomorrow's date? 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 get tomorrow Watch "How to get tomorrow New topic
Author

How to get tomorrow's date?

Steven Marco
Greenhorn

Joined: Jul 23, 2002
Posts: 21
Does anyone know how I can get tomorrow's date or the day after. Today's date will be the system date. Thanks.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
GregorianCalendar calendar = new GregorianCalendar();

//Display the date now:
Date now = calendar.getTime();
DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, Locale.UK);
String formattedDate = fmt.format(now);
System.out.println(formattedDate);

//Advance the calendar one day:
calendar.add(calendar.DAY_OF_MONTH, 1);
Date tomorrow = calendar.getTime();
formattedDate = fmt.format(tomorrow);
System.out.println(formattedDate);

//Advance the calendar 30 more days:
calendar.add(calendar.DAY_OF_MONTH, 30);
Date futureDay = calendar.getTime();
formattedDate = fmt.format(futureDay);
System.out.println(formattedDate);
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Steven,

you can use the Calendar class to calculate with dates.

eg:


Yours,
Bu.


all events occur in real time
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Sven,


you beated me about some milliseconds,

Bu.
Steven Marco
Greenhorn

Joined: Jul 23, 2002
Posts: 21
Thanks everyone. I am now trying to format the date, basically I have a String variable "dateFormat" that I pass in as parameter, it can be:

MM/DD/YYYY or MM.DD.YYYY

I tried the following but it doesn't work:

//////////////////////////////////////////////

GregorianCalendar calendar = new GregorianCalendar();

//////////////////Display the date now:
Date now = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date formattedDate = sdf.parse(now);
System.out.println(formattedDate);
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Use format() instead of parse(). The format() method is for formatting a Date object into a String. The parse() is to parse a String into a Date object.

Here is a basic example:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Another issue is that SimpleDateFormat uses the format string in a case-sensitive manner. That is, 'M' is months, 'm' is minutes. 'd' and 'y' are date and year respectively, while 'D' is something else entirely (day in year), and 'Y' isn't anything (it just gets treated as a literal 'Y'). So if MM/DD/YYYY and MM.DD.YYYY represent day, months, and years, you'll need to modify the strings to use the correct case. One way to do this is:

If you ever want to refer to minutes or day-in-year, you'll need to do something else. Of course if MM/DD/YYYY and MM.DD.YYYY are the only two options, I'd probably just prepare formatters for those two options, held in static final variables, and let the user choose between those two choices somehow. No need to create a new SimpleDateFormat each time. But the choice will probably depend on other needs, in terms of how the program is to be used.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get tomorrow's date?