为什么我得到不同JSON日期值的相同值? [英] Why am i getting same values of different JSON date values?

查看:92
本文介绍了为什么我得到不同JSON日期值的相同值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我得到不同JSON日期值的相同值的原因. 这是我用于解析JSON日期格式的日期值的代码:

I do not know the reason why am i getting same values of different JSON date values. Here is my code for parsing date values in JSON date format:

package com.jsondate.parsing;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class JSONDateParsing extends Activity {
    /** Called when the activity is first created. */
    String myString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        TextView tv = new TextView(this);
        //Date d = (new Date(1266029455L));
        //Date d = (new Date(1266312467L));
        Date d = (new Date(1266036226L));


        //String s = d.getDate() + "-" + d.getMonth() + "-" + d.getYear() + d.getHours() + d.getMinutes() + d.getSeconds();
       // SimpleDateFormat sdf=new SimpleDateFormat("yyyy MMM dd @ hh:mm aa");
        //Toast.makeText(this, d.toString(), Toast.LENGTH_SHORT);
        Log.e("Value:", d.toString());
        myString = d.toString();
        String []words = myString.split(" ");

        for(int i = 0;i < words.length; i++)
            Log.e("Value:", words[i]);

        myString = words[2] + "-" + words[1] + "-" + words[5] + " " + words[3];

        tv.setText(myString);
        setContentView(tv);


    }
}

推荐答案

据我所知,没有标准的方式可以在JSON中表示日期.看来您收到的是一个整数值,表示自格林尼治标准时间1970年1月1日00:00:00以来经过的的数量.这与Date(long date)构造函数的预期有所不同.自时期以来,构造函数期望毫秒.您需要将JSON中的值乘以1000,才能在Date中正确使用它们.

As far as I know, there is no standard way of representing a date in JSON. It looks as though what you are receiving is an integral value representing the number of seconds that have elapsed since the epoch of January 1, 1970, 00:00:00 GMT. This is somewhat different than what the Date(long date) constructor is expecting. The constructor is expecting milliseconds since the epoch. You need to multiply the values from the JSON by 1000 to use them correctly with Date.

long jsonDate = 1266036226L;
Date date = new Date(jsonDate * 1000);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
String stringDate = dateFormat.format(date);


以下是您所提供示例的不同表示形式.它们符合您的期望吗?


Here are the different representations for the examples that you have given. Do they correspond with what you are expecting?

  • 1266029455< ==> 2010年2月12日09:50:55
  • 1266312467< ==> 2010年2月16日04:27:47
  • 1266036226< ==> 2010年2月12日11:43:46
  • 1266072180< ==> 2010年2月13日09:43:46

请注意,SimpleDateFormat的默认行为是使用本地时区.就我而言,这是GMT-0500.可以通过调用setTimeZone方法来指定其他时区.

Note that the default behavior of SimpleDateFormat is to use the local time zone. In my case this is GMT-0500. A different time zone can be specified by calling the setTimeZone method.

这篇关于为什么我得到不同JSON日期值的相同值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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