0

I am having troubles parsing the following date 2020-06-11T09:03:46.000-0300. As far i know, i should use pattern 'Z' to represent the '-0300' Zone offset, but this pattern does not produce the expected output. The code i am using to make the parse is

@Test
public void testDate(){
    // The String and the pattern
    String strFecha = "2020-06-11T09:03:46.000-0300";
    String formato = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    // Create Formatter
    DateTimeFormatter dtf = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern(formato)
            .toFormatter()
            .withZone(ZoneId.systemDefault());

    // parse the String to ZonedDateTime
    TemporalAccessor accessor = dtf.parse(strFecha, new ParsePosition(0));
    ZonedDateTime fecha = ZonedDateTime.from(accessor);
    LocalDateTime fechaOff = LocalDateTime.ofInstant(fecha.toInstant(), ZoneId.systemDefault());

    // print
    System.out.println("str: " + strFecha  + ", " + fecha + ", fecha: " + fechaOff);
}

The problem is, my code does not produces my expected output, which is 2020-06-11T07:03:46-05:00 (3 hours less), instead, the code returns the original date 2020-06-11T09:03:46.000-05:00 it does not make any change in time (Still 9am). I can't figure the mistake in my code. any help would be apreciate

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Fernando CT
  • 113
  • 1
  • 6
  • 2
    Does this help? [How can I convert Java ZonedDateTime to OffsetDateTime with default time zone offset?](https://stackoverflow.com/questions/53071687/how-can-i-convert-java-zoneddatetime-to-offsetdatetime-with-default-time-zone-of) – Abra Jun 11 '20 at 19:04
  • Hello, thanks but no. As far i know, the offset given by "-05:00" is refeered to timezone offset, and i can manage this kind of offsets ussing "z" lower case, my offset does not have the ":", is time offset only – Fernando CT Jun 11 '20 at 19:31
  • I can’t seem to reproduce. Running in America/Bogota time zone I get `str: 2020-06-11T09:03:46.000-0300, 2020-06-11T07:03:46-05:00[America/Bogota], fecha: 2020-06-11T07:03:46`. Which appears to me to be correct. Isn’t this as what you would expect? – Ole V.V. Jun 13 '20 at 06:38

0 Answers0