-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

  • 2
    Possible duplicate of [How to use offset time in Java Simple Date Format](https://stackoverflow.com/questions/43518627/how-to-use-offset-time-in-java-simple-date-format) –  Oct 03 '17 at 16:46
  • `+0000` means UTC. – Joe C Oct 03 '17 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": https://meta.stackoverflow.com/a/339451/7605325 –  Oct 03 '17 at 17:22
  • Thanks a lot @Hugo for the edit. – Aashirwad Gupta Oct 04 '17 at 05:35
  • 1
    @AashirwadGupta You're welcome. But remember that you can always [edit] your questions to improve them. –  Oct 04 '17 at 12:15

3 Answers3

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-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
abskmj
  • 760
  • 3
  • 6
-2

Change the +0000 to Z:

SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
spectacularbob
  • 3,080
  • 2
  • 20
  • 41