簡體   English   中英

轉換JSON日期格式

[英]Convert JSON date format

我收到一個日期值如下的JSON對象:

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

我想用Java代碼解析它到Date或將它作為String

我想知道這樣做的簡單方法。

我認為第一個數字( 1325134800000 )是自紀元以來的毫秒數, -0500是時區。 這似乎是下面的示例代碼的情況,這似乎做你想要的。

以下代碼使用Jackson解析JSON輸入,如果您還沒有選擇JSON解析庫,我建議使用它。 它缺乏錯誤檢查等。

示例代碼:

public final class Foo
{
    public static void main(final String... args)
        throws IOException
    {
        // What the JSON value must match exactly
        // Not anchored since it will be used with the (misnamed) .matches() method
        final Pattern pattern
            = Pattern.compile("\\\\/Date\\((\\d+)(-\\d+)?\\)\\\\/");

        final ObjectMapper mapper = new ObjectMapper();

        // Parse JSON...
        final JsonNode node = mapper.readTree(
            "{\"PostingDate\": \"\\/Date(1325134800000-0500)\\/\"}");

        if (!node.has("PostingDate")) {
            System.err.println("Bad JSON input!");
            System.exit(1);
        }

        // Get relevant field
        final String dateSpec = node.get("PostingDate").getTextValue();

        // Try and match the input.
        final Matcher matcher = pattern.matcher(dateSpec);

        if (!matcher.matches()) {
            System.err.println("Bad pattern!"); // Yuck
            System.exit(1);
        }

        // The first group capture the milliseconds, the second one the time zone

        final long millis = Long.parseLong(matcher.group(1));
        String tz = matcher.group(2);
        if (tz.isEmpty()) // It can happen, in which case the default is assumed to be...
            tz = "+0000";

        // Instantiate a date object...    
        final Date date = new Date(millis);

        // And print it using an appropriate date format
        System.out.printf("Date: %s %s\n",
            new SimpleDateFormat("yyyy/MM/dd HH:MM:ss").format(date), tz);
    }
}

輸出:

Date: 2011/12/29 06:12:00 -0500

Hier是一種基於fge版本的工作解析方法,但改進為

  1. 它使用了jode的DateTime,並初始化了正確的時區
  2. 模式的微小變化也接受+0200

=>

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));
}

我使用Jquery DatePicker創建了一個簡單的JavaScript函數

  function JsonToJSDate(jsonDate) { var reg = /-?\\d+/; var m = reg.exec(jsonDate); return new Date(parseInt(m[0])); } 

$('#Started')。val($。datepicker.formatDate('mm / dd / yy',JsonToJSDate(yourDateVarHere)));

簡單的事情,但處理我的工作。 從JSON中提取對象值並應用子字符串。
例如:

      String postingDateObjectValue = "\\/Date(1442436473422)\\/";

      String dateStringInMillis = postingDateObjectValue .substring(7,20);

現在解析millis並在任何你想要的地方使用它們。

在Java> = 8中,您可以使用新的java.time API

輸入包含:

  • 一個unix時間戳1325134800000 ),這是自unix epoch( 1970-01-01T00:00Z )以來的毫秒數
  • UTC偏移量-0500 ),這是與UTC的差異(在這種情況下,比UTC晚5小時)

在新的java.time API中,有許多不同類型的日期/時間對象。 在這種情況下,我們可以選擇使用java.time.Instant (表示自unix epoch以來的納秒數)或java.time.OffsetDateTime (表示Instant轉換為特定偏移中的日期/時間)。

為了解析String ,我使用java.time.format.DateTimeFormatterBuilder來創建java.time.format.DateTimeFormatter 我還使用java.time.temporal.ChronoField來指定我正在解析的字段:

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    // epoch seconds
    .appendValue(ChronoField.INSTANT_SECONDS)
    // milliseconds
    .appendValue(ChronoField.MILLI_OF_SECOND, 3)
    // offset
    .appendPattern("xx")
    // create formatter
    .toFormatter();

我還使用正則表達式從輸入String提取相關部分(盡管您也可以使用substring()來獲取它):

String s = "/Date(1325134800000-0500)/";

// get just the "1325134800000-0500" part - you can also do s.substring(6, 24)
s = s.replaceAll(".*/Date\\(([\\d\\+\\-]+)\\)/.*", "$1");

然后我可以解析我想要的類型:

// parse to Instant
Instant instant = Instant.from(fmt.parse(s));
// parse to OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(s, fmt);

Instant相當於2011-12-29T05:00:00ZInstant只是時間軸中的一個點,你可以認為它始終是UTC)。 OffsetDateTime具有相同的瞬間,但轉換為-0500偏移量,因此其值為2011-12-29T00:00-05:00 InstantOffsetDateTime代表相同的時間點。


要轉換為java.util.Date ,請使用Instant

// convert to java.util.Date
Date date = Date.from(instant);

// if you have an OffsetDateTime, you can do this:
Date date = Date.from(odt.toInstant());

這是因為java.util.Date 沒有時區/偏移量信息 ,它只表示自unix epoch(與Instant相同的概念)以來的毫秒數,因此可以很容易地從Instant轉換。


Java 6和7

對於Java 6和7,您可以使用ThreeTen Backport ,這是Java 8的新日期/時間類的一個很好的后端 對於Android ,你還需要ThreeTenABP (更多關於如何在這里使用它)。

與Java 8的區別在於包名(在Java 8中是java.time ,在ThreeTen Backport(或Android的ThreeTenABP中)是org.threeten.bp ),但類和方法名稱是相同的。 因此,格式化程序創建和InstantOffsetDateTime的解析代碼是相同的。

另一個區別是,在Java <= 7中, java.util.Date類沒有from()方法。 但是您可以使用org.threeten.bp.DateTimeUtils類進行轉換:

// convert to java.util.Date
Date date = DateTimeUtils.toDate(instant);

// or from the OffsetDateTime
Date date = DateTimeUtils.toDate(odt.toInstant());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM