在Java/Android中解析UNIX日期的JSON,无需任何库[英] Parse JSON in UNIX date in Java/Android without any library

本文是小编为大家收集整理的关于在Java/Android中解析UNIX日期的JSON,无需任何库的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我从我的服务器接收了一个JSON阵列.我以日期为时间戳字符串 - 例如,[{"date": "1418056895", ... }] - 我需要以我们的日常格式解析.我该怎么做呢?我已经尝试过,但我总是有1970年的一年.

注意:我的JSON格式在Unix中.

JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
item.setTimeStamp(feedObj.getString("date"));


public void setTimeStamp(String timeStamp) {
    this.timeStamp = timeStamp;
}

public String getTimeStamp() {
    return timeStamp;
}

在另一堂课中,我解析了时间戳以显示:

List<FeedItem> feedItems = ...;
FeedItem item = feedItems.get(position);
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
            Long.parseLong(item.getTimeStamp()),
           System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);

但是,显示的日期始终在1970年.

使用诸如1403375851930之类的值,它似乎按预期工作(它显示现代日期).

推荐答案

如果您正确解析JSON,

feedObj.getString("date");

保留您的日期.您正在阅读日期为字符串.您必须将其转换为长.

public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    Long timestamp = Long.parseLong("1418056895"); //Long.parseLong(feedObj.getString("date"));
    cal.setTimeInMillis(timestamp * 1000L);
    DateFormat format = new SimpleDateFormat("MM/dd/yyyy"); // what ever format you need.
    System.out.println(format.format(cal.getTime())); // this will be in MM/dd/yyyy
    //item.setTimeStamp(format.format(cal.getTime()));
}

您也可以使用 feedObj.getlong(" date");要直接以长格式获得约会,但这取决于您使用的JSON库.

其他推荐答案

我不太了解您在这里要问什么.但是,如果您要要求转换您以人类可读形式获得的日期,那么您应该看看 epoch转换器.日期在Epoch Timestamp中返回给您.您可以通过使用以下代码将其转换为可读形式n Java.

String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));

其他推荐答案

我已经检查了您的API文档,并且您的响应日期为unix时间格式,如下所述 wall.post

您可以使用此处描述的方法进行转换迄今为止在Java

转换后,您可以格式化日期如下所述.

 private List<FeedItem> feedItems;
TextView timestamp
 FeedItem item = feedItems.get(position);
Long timeObject = Long.parseLong(getTimeStamp());

//use the timeObject and convert it in the String as described
// in above link to convert timestamp in date object than in String

//Once available set it again in timestamp
timestamp.setText(formattedDate);

本文地址:https://www.itbaoku.cn/post/102767.html

问题描述

I have a JSON array received from my server. I obtain a date as a timestamp String – for example, [{"date": "1418056895", ... }] – and I need to parse it in our day format. How do I do this? I have tried but I always have the year as 1970.

Note: My JSON format is in UNIX.

JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
item.setTimeStamp(feedObj.getString("date"));


public void setTimeStamp(String timeStamp) {
    this.timeStamp = timeStamp;
}

public String getTimeStamp() {
    return timeStamp;
}

In another class, I parse the timestamp to display it:

List<FeedItem> feedItems = ...;
FeedItem item = feedItems.get(position);
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
            Long.parseLong(item.getTimeStamp()),
           System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);

However the displayed date is always in 1970.

Using values such as 1403375851930 it appears to work as expected (it displays a contemporary date).

推荐答案

If you are parsing JSON correctly,

feedObj.getString("date");

holds your date. You are reading date as String. You have to convert it to long.

public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    Long timestamp = Long.parseLong("1418056895"); //Long.parseLong(feedObj.getString("date"));
    cal.setTimeInMillis(timestamp * 1000L);
    DateFormat format = new SimpleDateFormat("MM/dd/yyyy"); // what ever format you need.
    System.out.println(format.format(cal.getTime())); // this will be in MM/dd/yyyy
    //item.setTimeStamp(format.format(cal.getTime()));
}

you can also use feedObj.getLong("date"); to get Date in long format directly, but it depends on which JSON library you using.

其他推荐答案

I don't really understand what you are trying to ask here. But if you're asking is to ow to convert that date that you get in human readable form then you should take a look at Epoch Converter. The date is returned to you in epoch timestamp. You can convert the same to a human readable form n java by using the following code.

String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));

其他推荐答案

I have checked your api documentation and the date you have in response is in unix time format as described here wall.post

You can convert it using the method described here Convert Unix time stamp to date in java

Once converted you can format the date as described below.

 private List<FeedItem> feedItems;
TextView timestamp
 FeedItem item = feedItems.get(position);
Long timeObject = Long.parseLong(getTimeStamp());

//use the timeObject and convert it in the String as described
// in above link to convert timestamp in date object than in String

//Once available set it again in timestamp
timestamp.setText(formattedDate);