如何将传入的Json日期转换为java日期格式? [英] how to convert incoming Json date into java date format?

查看:128
本文介绍了如何将传入的Json日期转换为java日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xero帐户Apis
在json回复中我得到的日期如下

 Date :/日期(1455447600000 + 1300)/,

获取dateString字段的日期也相同

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

我正在尝试将上述日期转换为字符串但获取不同的日期。在我们的api中,两个日期相同,在Date字段和DateString字段中。

  Long longDate = Long.valueOf(1455447600000 )+ Long.valueOf( 1300); 
日期日期=新日期(longDate);

// TimeZone timeZone = TimeZone.getTimeZone(UTC); //也试了这个
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println(cal.getTime());

输出: Sun Feb 14 16:30:01 IST 2016 2月14日但在StringDate中 2月15日



json:

  [
{
日期:/日期(1455447600000 + 1300)/,
类型: ACCREC,
总计:460,
状态:授权,
联系:{
名称:nn,
电话:[

],
地址:[

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

],
ContactPersons:[

],
HasValidationErrors: false
},
DueDate:/日期(1455620400000 + 1300)/,
付款:[

],
SubTotal:460,
TotalTax:0,
AmountDue:460,
HasErrors:false,
InvoiceID:dcf1f09e-3e98-443e- 981e-cdd9f296d607,
LineItems:[
{
TaxType :OUTPUT,
ItemCode:Item2,
数量:20,
跟踪:[

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

],
预付款: [

],
CurrencyCode:INR,
CurrencyRate:1,
IsDiscounted:false,
超额付款 :[

],
DueDateString:2016-02-17T00:00:00,
InvoiceNumber:INV-0002,
AmountCredited:0,
HasAttachments:false,
UpdatedDateUTC:/日期(1455475695503 + 1300)/,
LineAmountTypes:独家
}
]


解决方案

+1300 不是毫秒偏移量,它是一个小时+分钟的偏移量。
如果您只将日期部分解析为长:

  Long longDate = Long.valueOf(1455447600000) ; 
日期日期=新日期(longDate);
System.out.println(date);

你得到(我在GMT时区)

  Sun Feb 14 11:00:00 GMT 2016 

你可以看到11 + 13 = 24,24小时是第二天。



你可以从偏移中得到时区,知道偏移是13小时和零分钟:

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

这给了我

  2016年2月15日

这里我计算的偏差为13小时,因此13 * 3600秒,因此13 * 3600 * 1000毫秒。所以你可以解析你的字符串:加号之前是什么时间,时区后面是什么。


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"
  }
]

解决方案

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.

这篇关于如何将传入的Json日期转换为java日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆