如何在Java中将currentTimeMillis转换为日期?
我在服务器中生成的某些日志文件中有毫秒,我也知道从中生成日志文件的语言环境,我的问题是将毫秒转换为指定格式的日期。该日志的处理在不同时区的服务器上进行。转换为“ 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));
输出:
Gregorian Calendar -2011-11-23 08:55:52,992jodaTime 2011-11-22 21:25:52,992
回答:
Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(timeStamp);
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
以上是 如何在Java中将currentTimeMillis转换为日期? 的全部内容, 来源链接: utcz.com/qa/434745.html