-2

I have had look to answers mentioned already but they don't tackle the problem I am facing.

I have a String in GMT as:

2017-10-03T19:45:00.000+0000

Which I need to parse as any other time zone. The problem is those +0000. When I try to parse it using SimpleDateFormat it does take server timezone into consideration instead of taking user's timeZone.

Here I have created a demo of the issue: https://www.jdoodle.com/embed/v0/java/jdk-1.8/92U

Code:

String dateString = "2017-10-02T19:45:23.000+0000";
SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000"); 
parseFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 
try{
    Date newDate = parseFormat.parse(dateString);
    System.out.println(newDate);
    parseFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); 
    String newDateString = parseFormat.format(newDate);
    //Instead of +0000 it should be +05:30 but it is not so. 
    System.out.println(newDateString);
} catch(ParseException e){
    e.printStackTrace();
}

Output:

Mon Oct 02 19:45:23 GMT 2017
2017-10-03T01:15:23.000+0000

5
  • 2
    Possible duplicate of How to use offset time in Java Simple Date Format Commented Oct 3, 2017 at 16:46
  • +0000 means UTC. Commented Oct 3, 2017 at 16:54
  • 2
    And it's better to include the code in the question, so if the link is unavailable, the question doesn't become "incomplete": meta.stackoverflow.com/a/339451/7605325 Commented Oct 3, 2017 at 17:22
  • Thanks a lot @Hugo for the edit. Commented Oct 4, 2017 at 5:35
  • 1
    @AashirwadGupta You're welcome. But remember that you can always edit your questions to improve them. Commented Oct 4, 2017 at 12:15

3 Answers 3

1

java.time

The other Answers suggesting replacing +0000 with Z are correct as a practical workaround, but use troublesome old date-time classes now supplanted by the java.time classes.

String input = "2017-10-03T19:45:00.000+0000".replace( "+0000" , "Z" ) ;
Instant instant = Instant.parse( input ) ;

For more info, see this Question.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this solves the problem. I did replace the pattern to have a Z in the end.
-1

Please set the timezone of parser to IST before parsing the date.

import java.util.*;
import java.text.*;
public class MyClass{

     public static void main(String []args){
        System.out.println("Hello World");
        String dateString = "2017-10-02T19:45:23.000+0000";
        SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
        //parseFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 
        try{
            parseFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); 
            Date newDate = parseFormat.parse(dateString);
            System.out.println(newDate);
            String newDateString = parseFormat.format(newDate);
            //Instead of +0000 it should be +05:30 but it is not so. 
            System.out.println(newDateString);
        } catch(ParseException e){
            e.printStackTrace();
        }
     }
}

Output:

Hello World
Mon Oct 02 19:45:23 GMT 2017
2017-10-03T01:15:23.000+0530

1 Comment

Thanks this solves the problem. I did replace the pattern to have a Z in the end.
-2

Change the +0000 to Z:

SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

1 Comment

Thanks this solves the problem. I did replace the pattern to have a Z in the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.