The other answers are correct but outdated.
java.time
The old date-time classes (java.util.Date/.Calendar etc.) bundled with the earliest versions of Java are now legacy.
Those old classes have been supplanted by the java.time package. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
LocalDateTime
The LocalDateTime
class represent a date-time without time zone. Use those for the first piece.
Your format is close to standard ISO 8601 format, just replace the SPACE with a T
.
String input = "2012-06-25 15:02:22.948";
String inputStandardized = input.replace( " " , "T" );
LocalDateTime ldt = LocalDateTime.parse( inputStandardized );
Offset from UTC
The other piece is the offset-from-UTC. We use the ZoneOffset
class for this.
ZoneOffset offset = ZoneOffset.of( "+0530" );
Without an offset or time zone the LocalDateTime
is not an actual moment on the timeline but rather a rough idea about a possible moment. Now we add your offset-from-UTC to mark an actual moment, represented by the OffsetDateTime
class.
OffsetDateTime odt = OffsetDateTime.of( ldt , offset );
Zoned
A time zone is an offset plus rules for handling anomalies such as Daylight Saving Time (DST). So better to use a time zone than a mere offset.
For example, if the context of this data is known to be time in India, use a time zone such as Asia/Kolkata
to get a ZonedDateTime
.
ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( zoneId );