Search code examples
javatimezonetalendsimpledateformatutc

How to convert the UTC date into PST date in java -simpledateformat


I am trying to use the simpledateformat function but i am continuously getting errors as "Unparseable date:"

Currently the time which is stored in a string testtime=2021-09-14T21:15:09.863Z;//UTC time I would want to convert this into PST time in the same format using T and Z notation;

Date date1=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm.sss'Z'").parse(testtime); 

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm.sss'Z", Locale.US);

 dateFormat.setTimeZone(TimeZone.getTimeZone("PST"));

System.out.println("PRINTING the TIME in PST"+dateFormat.format(date1.getTime()));

What is missing here,please advise?


Solution

  • Use java.time and you need no formatter

    I recommend that you use java.time, the modern Java date and time API, for your date and time work. I am assuming that by PST you mean North American Pacific Standard Time (America/Vancouver or America/Los_Angeles). Other interpretations exist that are just as valid.

        String testtime = "2021-09-14T21:15:09.863Z";
        
        Instant instant1 = Instant.parse(testtime);
        ZoneId desiredZone = ZoneId.of("America/Los_Angeles");
        ZonedDateTime pstDateTime = instant1.atZone(desiredZone);
        
        System.out.println("PRINTING the TIME in PST: " + pstDateTime);
    

    Output:

    PRINTING the TIME in PST: 2021-09-14T14:15:09.863-07:00[America/Los_Angeles]

    Oops, we didn’t get PST. We got Pacific Daylight Time or PDT. Wanting PST in September, I doubt that it makes any sense. Unless, of course, you meant Philippines Standard Time or Pitcairn Standard Time.

    ISO 8601: Your string is in ISO 8601 format. Instant and the other classes of java.time parse and print ISO 8601 format as their default, that is, without us specifying any formatter. So I didn’t. The output from ZonedDateTime isn’t strictly ISO 8601 format. If you wanted that, you may format the date and time using the builtin DateTimeFormatter.ISO_OFFSET_DATE_TIME:

        System.out.println("PRINTING the TIME in PST: "
                + pstDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
    

    PRINTING the TIME in PST: 2021-09-14T14:15:09.863-07:00

    What were you missing?

    Apart from using the long outdated and troublesome classes:

    • Never hardcode Z as a literal in your format pattern string. In means UTC so you need to parse and format it as an offset, or you will get incorrect result in most cases.
    • In particular printing Z after the PST time is wrong. Instead we want offset -08:00 for PST and -07:00 for PDT as in my output above.
    • As others have pointed out there is a typo in the time part of your format pattern string both times: HH:mm.sss. It should be HH:mm.ss.SSS for two-digit seconds and three-digit milliseconds. This typo caused the exception that you probably got.
    • Don’t rely on PST or other three letter abbreviations for time zones. As I said, they have multiple interpretation. Use a time zone ID like America/Vancouver, so in the region/city format.

    Links