4

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

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

also same date in getting in dateString field like

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

i am trying to convert above date into string but getting different date. as in our api both dates are same, in Date field and DateString field.

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 2016 14 Feb but in StringDate it's 15 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"
  }
]
Musaddique S
  • 1,539
  • 2
  • 15
  • 36
  • See also [this question](http://stackoverflow.com/questions/4032967/json-date-to-java-date). – Elliott Frisch Feb 18 '16 at 05:59
  • 1
    @ElliottFrisch Incorrectly closed. That alleged duplicate is about parsing ISO 8601 compliant strings. This Question is about parsing what is apparently a large number, a count from epoch. – Basil Bourque Feb 18 '16 at 07:45
  • yes it's not duplicate. – Musaddique S Feb 18 '16 at 08:24
  • @ Basil Bourque, you are right. @ElliottFrisch reopen this. – Musaddique Feb 18 '16 at 09:05
  • Please reopen this question. as i want help to solve this – Musaddique S Feb 18 '16 at 09:13
  • possible duplicate of [convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ format to MM/dd/yyyy hh:mm:ss.SSS format? From ISO8601 to UTC](http://stackoverflow.com/questions/5393847/how-can-i-convert-a-timestamp-from-yyyy-mm-ddthhmmsssssz-format-to-mm-dd-yyyy) have a look – Taj Ahmed Feb 18 '16 at 09:51
  • thanks for reply but I know how to convert date to date format. but here i am getting different date from api. after converting to date is different that in mentioned in stringdate – Musaddique S Feb 18 '16 at 10:07

2 Answers2

2

The +1300 is not a milliseconds offset, it's an hour + minute offset. If you parse just the date part as a long:

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

You get (I'm in GMT timezone)

Sun Feb 14 11:00:00 GMT 2016

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

You can get the timezone from the offset, knowing the offset is 13 hours and zero minutes:

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

Which gives me

Feb 15, 2016

Here so I calculate the offset as being 13 hours, hence 13*3600 seconds, hence 13*3600*1000 milliseconds. So you can parse your string: what's before the plus sign is the time, what's after is the timezone.

JP Moresmau
  • 7,388
  • 17
  • 31
1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161