Joda time
Much easier in Joda-Time . Joda-Time processes the default ISO 8601 standard string.
When the input string includes an offset ( +00:00
in this case), the rest of the string is parsed accordingly. While passing the time zone object, Joda-Time corrects the analyzed value in this zone. Below is a sample code for demonstration.
Notice how the adjustment for Calcutta in India means that the date flipped from the 17th to the 18th.
String input = "2014-12-17T21:37:00+00:00"; DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ); DateTime dateTimeMontréal = new DateTime( input , zone ); // Parse as UTC because of "+00:00" offset, then adjust to desired time zone of Montréal.
Easily adjusts to other time zones.
DateTime dateTimeParis = dateTimeMontréal.withZone( DateTimeZone.forID( "Europe/Paris" ) ); DateTime dateTimeUtc = dateTimeMontréal.withZone( DateTimeZone.UTC ); DateTime dateTimeKolkata = dateTimeMontréal.withZone( DateTimeZone.forID( "Asia/Kolkata" ) );
Dump for the console.
System.out.println( "input: " + input ); System.out.println( "dateTimeMontréal: " + dateTimeMontréal ); System.out.println( "dateTimeParis: " + dateTimeParis ); System.out.println( "dateTimeUtc: " + dateTimeUtc ); System.out.println( "dateTimeKolkata: " + dateTimeKolkata );
At startup.
input: 2014-12-17T21:37:00+00:00 dateTimeMontréal: 2014-12-17T16:37:00.000-05:00 dateTimeParis: 2014-12-17T22:37:00.000+01:00 dateTimeUtc: 2014-12-17T21:37:00.000Z dateTimeKolkata: 2014-12-18T03:07:00.000+05:30
source share