0

I just needed a sample code block or suggestion to convert the following date string to utc time and find difference with current time in java?

date string "2016-03-21T15:58:36-04:00"

Thanks in advance

Gowsikan
  • 5,571
  • 8
  • 33
  • 43
  • 8
    There are *lots* of questions on Stack Overflow about parsing date/time strings in Java. How you looked at any of those? Bear in mind that `SimpleDateFormat.parse` will return a `Date`, which doesn't have a time zone - it's just an instant in time. If you've got the format string right and the data contains the UTC offset anyway (as it does here) then there'll be no further conversion to do. – Jon Skeet Mar 21 '16 at 13:57

2 Answers2

0

You need to format the date string in following way:

String string = "2016-03-21T15:58:36-04:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = format.parse(string);

Then you just need to use Date APIs to find the time difference.

lkq
  • 2,326
  • 1
  • 12
  • 22
-2
public static long getMillisFrom(String inStrDate) {

   DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);

   Date inDate = ISO_DATE_FORMAT.parse(inStrDate);

   Date curDate = new Date();

   long diffMillis = curDate().getTime() - inDate.getTime();
   return diffMillis

}
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Nageswara Rao
  • 954
  • 1
  • 10
  • 32