Java 如何将时间戳转换为适当的日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41144296/
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
How to convert TimeStamp to appropriate Date format?
提问by neckobik
I have the TimeStamp
and I need to convert it to Data
type object that should match this pattern - "2016-11-16T18:42:33.049Z". How can I do that?
我有TimeStamp
并且我需要将它转换为Data
应该匹配此模式的类型对象 - “2016-11-16T18:42:33.049Z”。我怎样才能做到这一点?
采纳答案by xenteros
Date d = new Date((long)timestamp*1000);
will create a Date
instance. Displaying it later is another thing.
将创建一个Date
实例。稍后显示它是另一回事。
I think it's what you want:
我认为这就是你想要的:
DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
System.out.println(f.format(date));
Test:
测试:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
Date d = new Date((long)1481723817*1000);
DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
System.out.println(f.format(d));
}
}
>>2016-12-14T14:56:57.056Z
回答by Vishesh
Convert like this.
像这样转换。
String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));
you can also use the data object in the manner u want.
您还可以按照您想要的方式使用数据对象。