0

2018/05/18 13:30:42-0400 gets printed by the below code but I want 2018/05/18 13:30:42 +00:00 to be printed.

public static void main(String[] args) throws Exception{


    String input = "2018/05/18 13:30:42 +00:00";

    SimpleDateFormat parser = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = parser.parse(input);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ssZ");
    String formattedDate = formatter.format(date);

    System.out.println(formattedDate);

}
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • I guess just print the variable input then. – kumesana May 22 '18 at 15:55
  • http://idownvotedbecau.se/noresearch/, e.g. google search for the question title: [`java Parsing date with timezone and retaining the timezone value`](https://www.google.com/search?q=java+Parsing+date+with+timezone+and+retaining+the+timezone+value) – Andreas May 22 '18 at 16:00
  • 1
    @shmosel No, not `ZonedDateTime`. Parse as `OffsetDateTime` as no time zone is involved, only an offset-from-UTC is involved (an offset of zero hours). – Basil Bourque May 22 '18 at 16:13
  • The original to this duplicate Question does not have the issue of spaces & slashes in the input string. You can remove those to comply with ISO 8601 standard format used in that other Question. `OffsetDateTime.parse( "2018/05/18 13:30:42 +00:00".replace( "/" , "-" ).replace( " +" , "+" ).replace( " -" , "-" ).replace( " " , "T" ) )` – Basil Bourque May 23 '18 at 03:15
  • Thanks @BasilBourque – Punter Vicky May 23 '18 at 03:47

0 Answers0