how to convert incoming Json date into java date format?

前端 未结 2 2016
南旧
南旧 2020-12-21 04:53

I am working on Xero accounts Apis In json response i am getting date like below

 \"Date\": \"/Date(1455447600000+1300)/\",

also same date

2条回答
  •  臣服心动
    2020-12-21 05:28

    java.time

    I should like to contribute the modern solution

        Pattern jsonDatePattern = Pattern.compile("/Date\\((\\d+)([+-]\\d{4})\\)/");
        String dateFromJson = "/Date(1455447600000+1300)/";
        Matcher m = jsonDatePattern.matcher(dateFromJson);
        if (m.matches()) {
            long epochMillis = Long.parseLong(m.group(1));
            String offsetString = m.group(2);
            OffsetDateTime dateTime = Instant.ofEpochMilli(epochMillis)
                    .atOffset(ZoneOffset.of(offsetString));
            System.out.println(dateTime);
        }
    

    Output:

    2016-02-15T00:00+13:00

    This agrees with the date and time in your JSON date string and additionally informs you of the UTC offset.

    I am using and warmly recommending java.time, the modern Java date and time API. And discouraging the classes Date, Calendar and TimeZone used in the question and in the other answer. They are long outdated, and the modern Java date and time API is so much nicer to work with.

    Link

    Oracle tutorial: Date Time explaining how to use java.time.

提交回复
热议问题