tl;dr
ZonedDateTime zdt = LocalDateTime.parse( "2014-02-15 05:18:08".replace( " " , "T" ) ).atOffset( ZoneOffset.UTC ).atZoneSameInstant( ZoneId.of( "Asia/Kolkata" ) ) ;
LocalDate ld = zdt.toLocalDate();
LocalTime lt = zdt.toLocalTime();
Use objects, not strings
You should be retrieving date-time values from your database as date-time objects rather than Strings.
As of JDBC 4.2 and later, we can exchange java.time objects with the database.
Instant instant = Instant.now() ; // Capture the current moment in UTC.
myPreparedStatement.setObject( … , instant ) ;
Retrieval.
Instant instant = myResultSet.getObject( … , Instant.class ) ;
java.time
The java.time classes in Java 8 and later supplant the troublesome old legacy date-time classes as well as the 3rd-party Joda-Time library.
- The
java.sql.Timestamp
class is replaced by Instant
.
- The
java.sql.Date
class is replaced by LocalDate
.
- The
java.sql.Time
class is replaced by LocalTime
.
-
Parsing String
If you are stuck with such a String, parse it using a java.time classes. The other Answers are using the troublesome old date-time classes bundled with the earliest versions of Java. Those are now legacy, and should be avoided.
Your input string is almost in standard ISO 8601 format. Merely replace the SPACE in the middle with a T
.
String input = "2014-02-15 05:18:08".replace( " " , "T" ) ;
LocalDateTime
Parse as a LocalDateTime
as the string lacks any info about offset-from-UTC or time zone.
LocalDateTime ldt = LocalDateTime.parse( input ) ;
OffsetDateTime
I will assume the value in your input String was intended to be a moment in UTC time zone. So adjust into UTC.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
ZonedDateTime
You asked for this to be adjusted into the India time zone, which is five and a half hours ahead of UTC.
The atZoneSameInstant
means the resulting ZonedDateTime
represents the very same simultaneous moment as the OffsetDateTime
. The two are different only in that they view that same moment through two different lenses of wall-clock time.
ZoneId z = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( z );
LocalDate
& LocalTime
If you want to work with the date portion and time-of-day portion separately, extract each as a Local…
.
LocalDate ld = zdt.toLocalDate();
LocalTime lt = zdt.toLocalTime();
Generating String representation
The toString
method on the classes all generate a String representation using standard ISO 8601 formats. To use other formats, use the DateTimeFormatter
class. Search Stack Overflow for many examples and discussions.
The easiest way is let the class automatically localize for you. Specify a Locale
for the desired human language and the desired cultural norms to decide issues such as capitalization, abbreviation, and such.
Locale locale = new Locale( "en" , "IN" ); // English language, India cultural norms.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ).withLocale( locale );
String output = zdt.format( f );
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.