简体   繁体   English

Java如何将时间文字转换为长值?

[英]Java How to convert time literal to long value?

If I have a time (String) coming into my class with the following format 如果我有时间(字符串)以以下格式进入我的班级

2013-01-25T07:31:51.00Z

How do i turn that into a long? 我该如何变长?

I don't even know what format that is to put in to a DateFormat. 我什至不知道将什么格式放入DateFormat中。 Anyone else have a clue? 还有其他线索吗?

DateFormat formatter = new SimpleDateFormat(" MMM yyyy HH:mm:ss z");

Desired results 所需结果

long time = changeMe("2013-01-25T07:31:51.00Z");

System.out.print(time);

//012432423  <-- But only the actual long

Looks like you have format here. 看起来您这里具有格式。 Try this: 尝试这个:

import javax.xml.bind.DatatypeConverter;

public long changeMe(String isoTimestamp) {
  final Calendar c = DatatypeConverter.parseDateTime(isoTimestamp);
  return c.getTimeInMillis();
}

BTW c.getTime() returns Date object, if you prefer. BTW c.getTime()返回Date对象(如果您愿意)。

See also: 也可以看看:

using the excellent jodatime library: 使用出色的jodatime库:

DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
long time = fmt.parseDateTime("2013-01-25T07:31:51.00Z").getMillis();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM