Convert String into java.time.ZonedDateTime

SSF :

I want to convert elements of an array list into ZonedDateTime object after parsing them. A string is shown below.

"2017-02-12 06:59:00 +1300"

At the moment I use the DateTimeFormatter:

DateTimeFormatter dateTimeFormatter =
  DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss ZZ");

And try to use parse, to get the time:

this.actionTime = dateTimeFormatter.parse(actionTime, ZonedDateTime::from);

See below method:

public DateCalculatorTest(String actionTime, int expectedDayOfWeek) {
    DateTimeFormatter dateTimeFormatter = 
      DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss ZZ");
    DateTimeFormatter localTimeFormatter = 
      DateTimeFormatter.ofPattern("YYYY-MM-dd");
    this.actionTime = dateTimeFormatter.parse(actionTime, ZonedDateTime::from);
    this.expectedDayOfWeek = expectedDayOfWeek;
}

However, I am not able to parse the string. I get the following error:

Text '2017-02-12 06:59:00 +1300' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2017, DayOfMonth=12, MonthOfYear=2, OffsetSeconds=46800},ISO resolved to 06:59 of type java.time.format.Parsed

Is there a way to do this with java.time?

SSF :

I managed to fix this using the following:

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss ZZ");
    DateTimeFormatter localTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    this.symbol = symbol;
    this.actionTime = ZonedDateTime.parse(actionTime, dateTimeFormatter);
    this.actionDate = LocalDate.parse(expectedResult, localTimeFormatter);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76375&siteId=1