如果我正在运行 unix 命令
echo 1378223625 | xargs -L 1 -I '{}' date -d "@{}" "+%d-%m-%Y-%H-%M %Z"
结果是 03-09-2013-17-53 CEST
如果我从java运行
public class DateExperiment {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy-H-m z");
System.out.println(df.format(1378223625) );
}
}
我的结果是 16-01-1970-23-50 CET
unix 命令给出了正确的结果,但 java 给出了错误的结果。
谁能解释为什么会出现这种差异?什么错误?
最佳答案
如果您使用数值作为 SimpleDateFormat#format(..)
的参数,它会使用 Date
constructor which expects a long
.那个构造函数
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.
您提供的值代表
16-01-1970-23-50 CET
这里的问题是你有秒,而不是毫秒。
你会想要
System.out.println(df.format(1378223625000L));
关于java - unix date 命令和 java simpledateformat 的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24357840/