Converting Date With Time In Pst Into Utc Format
i am having a variable str(string type)having '28-Nov-2013 09:15 AM' as its value.How to convert it into UTC format(the above mentioned time in str variable is in PST, hence UTC sh
Solution 1:
Wise programmers leave the heavy-lifting of date-time calculations to a specialized library. In Java, that would be Joda-Time (or in Java 8, JSR 310).
Here is example code for Joda-Time 2.3 in Java 7.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.StringdateString="28-Nov-2013 09:15 AM"; // Assumed to be the local date-time in United States west coast.//String dateString = "28-Nov-2013 09:15 PM"; // Test "PM" as well as "AM" if you like.// Joda-Time has deprecated use of 3-letter time zone codes because of their inconsistency. Use other identifier for zone.// Time Zone list: http://joda-time.sourceforge.net/timezones.html
org.joda.time.DateTimeZonecaliforniaTimeZone= org.joda.time.DateTimeZone.forID( "America/Los_Angeles" );
// Joda-Time formatting codes: http://www.joda.org/joda-time/key_format.html
org.joda.time.format.DateTimeFormatterdateStringFormat= org.joda.time.format.DateTimeFormat.forPattern( "dd-MMM-yyyy hh:mm aa" ).withZone( californiaTimeZone );
org.joda.time.DateTimecaliforniaDateTime= dateStringFormat.parseDateTime( dateString );
org.joda.time.DateTimeutcDateTime= californiaDateTime.toDateTime( org.joda.time.DateTimeZone.UTC );
// Both of these date-time objects represent the same moment in the time-line of the Universe, // but presented with different time-zone offsets.
System.out.println( "californiaDateTime: " + californiaDateTime );
System.out.println( "utcDateTime: " + utcDateTime );
When run…
californiaDateTime: 2013-11-28T09:15:00.000-08:00
utcDateTime: 2013-11-28T17:15:00.000Z
Post a Comment for "Converting Date With Time In Pst Into Utc Format"