2

I have a timedate string in a round-trip data pattern like this: 2012-07-05T11:30:44.1533815Z. This comes from some .NET service.

How do I convert it to a long in Java?

I've tried SimpleDateTimeFormat seems I can't figure out the correct format specifiers..

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158

5 Answers5

5

Try this:

public long getTime(String time) throws ParseException {
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'");
  Date date = df.parse(time);

  return date.getTime();
}

cheers

peshkira
  • 6,069
  • 1
  • 33
  • 46
2

You need to parse it to a Date using SimpleDateFormat.

From a date, you can get, using getTime(), the unix epoch in milliseconds, which is a long.

This would start like this :

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSS'Z'", Locale.ENGLISH);
long myTime = format.parse("2012-07-05T11:30:44.1533815Z").getTime();

But you have to check what's the meaning of the last chars of your date ? Is that a timezone definition ? If you don't determine it and simply escape it, you'll risk to interpret the date with a bad time zone.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
2

Try this:

try {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-dd'T'HH:mm:ss.SSSSSSS'Z'");
    long time = dateFormat.parse("2012-07-05T11:30:44.1533815Z").getTime();
} catch (ParseException e) {
              .....
}
Fredrik LS
  • 1,480
  • 9
  • 15
2

Since this is a datetime in the iso format, you can easily parse it using the excellent Joda datetime library as this:

DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
DateTime dt = fmt.parseDateTime("2012-07-05T11:30:44.1533815Z");
long dtl = dt.getMillis();

See Joda Userguide under the "Input and Output" chapter and http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html. It will take care of parsing the ISO8601 format in the correct way.

Nicolas Mommaerts
  • 3,207
  • 4
  • 35
  • 55
0

Please note this, If i am giving dateTime UTC "2017-07-13T05:43:24.6966215Z" it gives me back in IST Thu Jul 13 07:39:30 IST 2017 instead of Thu Jul 13 11:13:25 IST 2017

String rawDate = "2017-07-13T05:43:24.6966215Z";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Date date = simpleDateFormat.parse(rawDate);
System.out.println(date);

So after gone through this answer i came to know this given string is not Date format rather than ISO Instant format.

Milliseconds (SSS) can only be three digits. On more than that, the date rolls over - e.g. 10:38:14.1000 becomes 10:38:15.000.

To convert Instant to Date Object

String rawDate = "2017-07-13T05:43:24.6966215Z";
DateTimeFormatter instantFormater = DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.of("Asia/Kolkata"));
Instant instant = Instant.from(instantFormater.parse(rawDate));
System.out.println(Date.from(instant));

It gives expected Thu Jul 13 11:13:24 IST 2017 Check this answer

Naveen raj
  • 891
  • 1
  • 10
  • 18