How to convert Json incoming date to java date format?

I am working on Xero Apis accounts In response to json I get a date as below

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

also the same date when received in the dateString field, for example

"DateString": "2016-02-15T00:00:00",

I am trying to convert the above date to a string, but getting a different date. since in our api both dates are the same, in the field Date and DateString.

Long longDate=Long.valueOf("1455447600000")+Long.valueOf("1300");
        Date date = new Date(longDate);

        //TimeZone timeZone = TimeZone.getTimeZone("UTC"); //also tried this
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        System.out.println(cal.getTime());

output: Sun Feb 14 16:30:01 IST 2016February 14th, but in StringDate it15 Feb

JSON:

[
  {
    "Date": "/Date(1455447600000+1300)/",
    "Type": "ACCREC",
    "Total": 460,
    "Status": "AUTHORISED",
    "Contact": {
      "Name": "nn",
      "Phones": [

      ],
      "Addresses": [

      ],
      "ContactID": "6831fd62-d6f1-4dc7-9338-24566074ecf6",
      "ContactGroups": [

      ],
      "ContactPersons": [

      ],
      "HasValidationErrors": false
    },
    "DueDate": "/Date(1455620400000+1300)/",
    "Payments": [

    ],
    "SubTotal": 460,
    "TotalTax": 0,
    "AmountDue": 460,
    "HasErrors": false,
    "InvoiceID": "dcf1f09e-3e98-443e-981e-cdd9f296d607",
    "LineItems": [
      {
        "TaxType": "OUTPUT",
        "ItemCode": "Item2",
        "Quantity": 20,
        "Tracking": [

        ],
        "TaxAmount": 0,
        "LineAmount": 460,
        "LineItemID": "2a6c5078-a462-4e8c-b277-d1164885b7d9",
        "UnitAmount": 23,
        "AccountCode": "200",
        "Description": "Item2"
      }
    ],
    "Reference": "43223",
    "AmountPaid": 0,
    "DateString": "2016-02-15T00:00:00",
    "CreditNotes": [

    ],
    "Prepayments": [

    ],
    "CurrencyCode": "INR",
    "CurrencyRate": 1,
    "IsDiscounted": false,
    "Overpayments": [

    ],
    "DueDateString": "2016-02-17T00:00:00",
    "InvoiceNumber": "INV-0002",
    "AmountCredited": 0,
    "HasAttachments": false,
    "UpdatedDateUTC": "/Date(1455475695503+1300)/",
    "LineAmountTypes": "Exclusive"
  }
]
+4
source share
2 answers

+1300not a millisecond offset, it is an offset per hour + minute. If you parse only part of the date as long:

Long longDate=Long.valueOf("1455447600000");
Date date = new Date(longDate);
System.out.println(date);

You get (I'm in GMT time zone)

Sun Feb 14 11:00:00 GMT 2016

And you can see that 11 + 13 = 24, and the next day - 24 hours.

, , 13 :

Calendar c=Calendar.getInstance(TimeZone.getTimeZone(TimeZone.getAvailableIDs(13*3600*1000)[0]));
c.setTimeInMillis(longDate);
DateFormat df=DateFormat.getDateInstance();
df.setTimeZone(c.getTimeZone());
System.out.println(df.format(c.getTime()));

Feb 15, 2016

13 , , 13 * 3600 , , 13 * 3600 * 1000 . , : "" - , .

+2

java.time

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

:

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

JSON UTC.

java.time API Java. Date, Calendar TimeZone, . , API Java .

Oracle: , , java.time.

+1

Source: https://habr.com/ru/post/1629308/


All Articles