简体   繁体   中英

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. 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.

See also:

using the excellent jodatime library:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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