tl;dr
LocalDateTime.parse( // Lacking an offset or time zone, parse as a `LocalDateTime`. *Not* a specific moment in time.
"2008-01-01 13:15:00".replace( " " , "T" ) // Alter input string to comply with ISO 8601 standard format.
)
.toLocalDate() // Extract a date-only value.
.atStartOfDay( // Do not assume the day starts at 00:00:00. Let class determine start-of-day.
ZoneId.of( "Europe/Paris" ) // Determining a specific start-of-day requires a time zone.
) // Result is a `ZonedDateTime` object. At this point we have a specific moment in time, a point on the timeline.
.toString() // Generate a String in standard ISO 8601 format, wisely extended to append the name of the time zone in square brackets.
2008-01-01T00:00+01:00[Europe/Paris]
To generate a String in your desired format, pass a DateTimeFormatter
.
LocalDateTime.parse( // Lacking an offset or time zone, parse as a `LocalDateTime`. *Not* a specific moment in time.
"2008-01-01 13:15:00".replace( " " , "T" ) // Alter input string to comply with ISO 8601 standard format.
)
.toLocalDate() // Extract a date-only value.
.atStartOfDay( // Do not assume the day starts at 00:00:00. Let class determine start-of-day.
ZoneId.of( "Europe/Paris" ) // Determining a specific start-of-day requires a time zone.
) // Result is a `ZonedDateTime` object. At this point we have a specific moment in time, a point on the timeline.
.format( // Generate a String representing the object’s value.
DateTimeFormatter.ISO_LOCAL_DATE_TIME // Built-in predefined formatter close to what you want.
)
.replace( "T" , " " ) // Replace the standard’s use of a 'T' in the middle with your desired SPACE character.
2008-01-01 00:00:00
Details
Other Answers are correct, but use old date-time classes now outmoded by the java.time framework.
java.time
The java.time framework is built into Java 8 and later. Much of the java.time functionality is back-ported to Java 6 & 7 (ThreeTen-Backport) and further adapted to Android (ThreeTenABP).
First alter the input string to comply with the canonical version of ISO 8601 format. The standard ISO 8601 formats are used by default in java.time classes for parsing/generating strings that represent date-time values. We need to replace that SPACE in the middle with a T
.
String input = "2008-01-01 13:15:00".replace( " " , "T" ); // → 2008-01-01T13:15:00
Now we can parse it as a LocalDateTime
, where “Local” means no specific locality. The input lacks any offset-from-UTC or time zone info.
LocalDateTime ldt = LocalDateTime.parse( input );
ldt.toString()… 2008-01-01T13:15:00
If you do not care about time-of-day nor time zone, then convert to a LocalDate
.
LocalDate ld = ldt.toLocalDate();
ld.toString()… 2008-01-01
First Moment Of Day
If instead you want the time-of-day set to the first moment of the day, use a ZonedDateTime
class, then convert to a LocalDate
object to call its atStartOfDay
method. Be aware that the first moment may not be the time 00:00:00
because of Daylight Saving Time or perhaps other anomalies.
The time zone is crucial because for any given moment the date varies around the world by zone. For example, a few moments after midnight in Paris is a new day for Parisians but is still “yesterday” in Montréal for the Canadians.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone( zoneId );
LocalDate ldFromZdt = zdt.toLocalDate();
ZonedDateTime zdtStartOfDay = ldFromZdt.atStartOfDay( zoneId );
zdtStartOfDay.toString()… 2008-01-01T00:00:00-05:00[America/Montreal]
UTC
To see that moment through the lens of the UTC time zone, extract a Instant
object. Both the ZonedDateTime
and Instant
will represent the same moment on the timeline but appear as two different wall-clock times.
An Instant
is the basic building-block class in java.time, always in UTC by definition. Use this class frequently, as you should generally be doing your business logic, data storage, and data exchange in UTC.
Instant instant = zdtStartOfDay.toInstant();
instant.toString()… 2008-01-01T05:00:00Z
We see 5 AM rather than stroke-of-midnight. In standard format, the Z
on the end is short for Zulu
and means “UTC”.
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.