Convert JSON date format

后端 未结 5 1943
旧时难觅i
旧时难觅i 2020-12-19 07:28

I\'m receiving a JSON object with date value like this:

{\"PostingDate\":\"\\/Date(1325134800000-0500)\\/\"}

And I want to parse it in Java

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-19 08:15

    Hier is a working parsing method based on the version of fge but improved as

    1. it used jode's DateTime with the correct timezone initialized
    2. minor change to pattern to accept +0200 too

    =>

    private static final Pattern bingTimePattern = Pattern.compile("\\/Date\\((\\d+)([-+]\\d+)?\\)\\/");
    
    public static DateTime parseBingTime(String timeAsString) throws ParseException {
        Matcher matcher = bingTimePattern.matcher(timeAsString);
        if (!matcher.find())
            throw new ParseException("wrong date time format " + timeAsString, 0);
    
        final long millis = Long.parseLong(matcher.group(1));
        String tz = matcher.group(2);
        if (tz.isEmpty())
            tz = "+0000";
    
        return new DateTime(millis, DateTimeZone.forID(tz));
    }
    

提交回复
热议问题