• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to get tomorrow's date?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sven,


you beated me about some milliseconds,

Bu.
 
Steven Marco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s;
 Date date;
 Format formatter;
 Calendar calendar = Calendar.getInstance();

 date = calendar.getTime();
 formatter = new SimpleDateFormat("dd/MMM/yyyy");
 s = formatter.format(date);
 System.out.println("Today : " + s);

 calendar.add(Calendar.DATE, 1);
 date = calendar.getTime();
 formatter = new SimpleDateFormat("dd/MMM/yyyy");
 s = formatter.format(date);
 System.out.println("Tomorrow : " + s);
 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use Date, Calendar, nor GregorianCalendar. Those classes are obsolete (and badly designed, to boot). This part of the Java™ Tutorials tells you what to use. LocalDate, most probably.
 
Everybody's invited. Except this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic