Apparently something is going wrong on your end.
Here is my version of your code. Using Java 8 Update 45.
try {
String input = "2015-06-20T01:57:13Z";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
java.util.Date date = sdf.parse( input );
long millisSinceEpoch = date.getTime();
System.out.println( "millis: " + millisSinceEpoch );
} catch ( ParseException ex ) {
// Handle exception
}
When run.
millis: 1434765433000
I get your expected results.
Perhaps the problem is related to your incorrect use of a SimpleDateFormat instance as static, as other pointed out in comments and Answers.
By the way, the java.util.Date/.Calendar classes are notoriously troublesome. They are now supplanted by either:
Both of these frameworks support your input string’s ISO 8601 format by default.
And both of these frameworks assign a time zone to their respective date-time objects.
Joda-Time
Here is code in Joda-Time 2.8.1. Search StackOverflow for many more examples and discussions.
String input = "2015-06-20T01:57:13Z";
DateTimeZone zone = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeKolkata = new DateTime( input , zone );
DateTime dateTimeUtc = dateTimeKolkata.withZone( DateTimeZone.UTC );
long millisSinceEpoch = dateTimeKolkata.getMillis();
Dump to console.
System.out.println( "dateTimeKolkata: " + dateTimeKolkata );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "millisSinceEpoch: " + millisSinceEpoch );
When run.
dateTimeKolkata: 2015-06-20T07:27:13.000+05:30
dateTimeUtc: 2015-06-20T01:57:13.000Z
millisSinceEpoch: 1434765433000
java.time
Code for java.time will be similar in concept to the Joda-Time code seen above, except that java.time use Factory methods rather than "new" constructors.
See this Question, Parse ISO timestamp using Java 8 java.time api (standard edition only) for code examples.
Avoid Count-Since-Epoch
I suggest avoiding working directly with a count-from-epoch (milliseconds in your case) whenever possible. Use a decent date-time framework and stick with its intelligent objects. Do you handle text as raw byte arrays instead of String
instances? Same logic applies to date-time values.