在 Java/Android 中将 int 转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24537602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:47:40  来源:igfitidea点击:

Convert int to date in Java/Android

javaandroiddatenumbersint

提问by Niunzin

I'm trying to get data from a website, and when I tried to get the date of a post (expected: 13/06/2014 11:55), i got:

我正在尝试从网站获取数据,当我尝试获取帖子的日期时(预计:13/06/2014 11:55),我得到:

23377855

23377855

Can someone help me to convert this number to a date? Thanks!

有人可以帮我将此数字转换为日期吗?谢谢!

采纳答案by syntagma

You can use the standard Java Date API:

您可以使用标准的 Java 日期 API:

        long yourNumber = 23377855;
        Date date = new Date(yourNumber);

Or you can use Joda Timelibrary, provides much better overall functionality than Java Date API:

或者您可以使用Joda Time库,提供比 Java Date API 更好的整体功能:

        long yourNumber = 23377855;
        DateTime dt = new DateTime(yourNumber);

回答by Josef E.

Java is expecting milliseconds:

Java 期望毫秒:

java.util.Date time= new java.util.Date((long)urDateNum*1000);

So you must multiply by 1000

所以你必须乘以1000

Docs say:

文档说:

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

分配一个 Date 对象并将其初始化为表示自称为“纪元”的标准基准时间(即格林威治标准时间 1970 年 1 月 1 日 00:00:00)以来的指定毫秒数。

Note:

笔记:

The cast to longis very important in this situation. Without it the integeroverflows.

long在这种情况下,演员阵容非常重要。没有它,integer溢出。