tl;dr
Never use java.util.Date
class.
Use java.time instead.
LocalDate.now() // Capture the current date as seen in the wall-clock time of the JVM’s current default time zone.
.plusDays( 1 ) // Move to tomorrow’s date. Returns a fresh `LocalDate` object rather than modifying (“mutating”) the original, per immutable objects pattern.
.toString() // Generate a String representing this date value. Use standard ISO 8601 format: YYYY-MM-DD.
2018-06-03
Wrong type
get a new Date instance representing tomorrow that has no time component to it (only year, month & day).
You are using a date-with-time-of-day class to represent a date-only value. Square peg, round hole.
Instead, use the date-only class: LocalDate
.
Avoid legacy classes
You are using terrible old date-time classes that were supplanted years ago by the industry-leading java.time classes.
I'm trying to take the current Date (now)
Get the current date by calling LocalDate.now
.
LocalDate today = LocalDate.now() ;
That call implicitly applied your JVM’s current default time zone to determine the current date. A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.
LocalDate today = LocalDate.now( ZoneId.systemDefault() ) ;
add one day to it
Easy. Call the LocalDate::plusDays
method. The java.time classes use the immutable objects pattern. So rather than modify (“mutate”) the original object, a new fresh object is produced.
LocalDate tomorrow = today.plusDays( 1 ) ;
In some situations you may want to represent the amount of time to add as a an object itself. Use Period
for a number of years, months, days.
Period p = Period.ofDays( 1 ) ;
LocalDate localDate = today.plus( p ) ;
Strings
To generate a String
representing the value of our LocalDate
, call toString
if you want the standard ISO 8601 format. The standard format seems to be what you desire.
String output = tomorrow.toString() ; // Generate String in standard ISO 8601 format, YYYY-MM-DD.
2018-01-23
You can specify a custom formatting pattern with DateTimeFormatter
. Or, better, let that same class automatically localize for you using its ofLocalized…
methods.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.