Java时间戳 - 如何使用时间戳1420432787创建日期和时间字符串?

Rat*_*ore -2 java android

如何使用时间戳1420432787创建日期和时间字符串,该时间戳等于1/05/2015 10:09:47.

Bellow代码也不起作用

 Date date = new Date(1420432787);
 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:a");
 System.out.println(simpleDateFormat.format(date));
Run Code Online (Sandbox Code Playgroud)

这个1/17/1970 10:34 AM的结果

Pra*_*ode 5

Date构造函数接受的时间长度以毫秒为单位,而不是秒.

你需要将它乘以1000,并确保你提供它.

Date date = new Date(timeInSeconds * 1000);
Run Code Online (Sandbox Code Playgroud)

然后你可以将它解析为你的日期格式

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:a");
System.out.println(simpleDateFormat.format(date));
Run Code Online (Sandbox Code Playgroud)