TL; DR
LocalDate.of( 2011 , 4 , 29 ) // Represent April 29, 2011. .atStartOfDay( ZoneId.of( "America/Montreal" ) ) // Determine the first moment of the day. Often 00:00:00 but not always. .format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ) // Generate a String representing the value of this date, using standard ISO 8601 format. .replace( "T" , " " ) // Replace the `T` in the middle of standard ISO 8601 format with a space for readability.
Using java.time
The modern way is java.time classes.
If you are trying to get the first moment of the day, do not take the time 00:00:00. Anomalies in some time zones mean that the day can start at other times of the day, for example, 01:00:00.
The LocalDate class represents a date value only without time and without a time zone.
The time zone is critical for determining the date. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec .
Specify the time zone name in continent/region format, such as America/Montreal , Africa/Casablanca or Pacific/Auckland . Never use the abbreviation 3-4 letters, for example, EST or IST , since they are not real time zones, and are not standardized or even unique (!).
ZoneId z = ZoneId.of( "America/Montreal" ); LocalDate today = LocalDate.now( z );
You want to indicate a specific date in your Question.
LocalDate localDate = LocalDate.of( 2011 , 4 , 29 ) ;
Apply time zone when determining the first moment of the day.
ZonedDateTime zdt = localDate.atStartOfDay( z ); // Determine the first moment of the day on this date for this zone.
I recommend always including a timezone pointer or offset-from-UTC with your date strings. But if you insist, you can use DateTimeFormatter predefined in java.time, which does not include zone / offset: DateTimeFormatter.ISO_LOCAL_DATE_TIME . Just remove T from the middle.
String output = zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ) .replace( "T" , " " ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy datetime classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
- Java SE 8 , Java SE 9 , and then
- Built in.
- Part of the standard Java API with integrated implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) specifically for Android.
- See How to use ThreeTenABP ....