我需要将unix时间戳转换为日期对象.
我试过这个:
java.util.Date time = new java.util.Date(timeStamp);
Run Code Online (Sandbox Code Playgroud)
时间戳值是: 1280512800
日期应该是"2010/07/30 - 22:30:00"(因为我通过PHP得到它),但我得到了Thu Jan 15 23:11:56 IRST 1970.
应该怎么做?
我在服务器中生成的某个日志文件中有毫秒数,我也知道生成日志文件的区域设置,我的问题是将指定格式的毫秒转换为日期.该日志的处理发生在位于不同时区的服务器上.转换为"SimpleDateFormat"程序时,机器的日期是这样的,因为这样的格式化日期并不代表服务器的正确时间.有没有办法优雅地处理这个问题?
long yourmilliseconds = 1322018752992l;
//1322018752992-Nov 22, 2011 9:25:52 PM
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS",Locale.US);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
calendar.setTimeInMillis(yourmilliseconds);
System.out.println("GregorianCalendar -"+sdf.format(calendar.getTime()));
DateTime jodaTime = new DateTime(yourmilliseconds,
DateTimeZone.forTimeZone(TimeZone.getTimeZone("US/Central")));
DateTimeFormatter parser1 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss,SSS");
System.out.println("jodaTime "+parser1.print(jodaTime));
Run Code Online (Sandbox Code Playgroud)
输出:
Gregorian Calendar -2011-11-23 08:55:52,992
jodaTime 2011-11-22 21:25:52,992
Run Code Online (Sandbox Code Playgroud)