1

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
stackoverflow
  • 17,218
  • 47
  • 124
  • 188

2 Answers2

6

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:

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 324,247
  • 67
  • 682
  • 662
1

using the excellent jodatime library:

DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
long time = fmt.parseDateTime("2013-01-25T07:31:51.00Z").getMillis();
mantrid
  • 2,799
  • 19
  • 17