File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get tomorrow's date?

 
Steven Marco
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steven,

you can use the Calendar class to calculate with dates.

eg:


Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sven,


you beated me about some milliseconds,

Bu.
 
Steven Marco
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic