0

I have the following time / date string

   2016-08-01T20:46:03.8114163+01:00

I am trying to change into a timestamp

When I apply the following JAVA code I get

   1451685677163

Removing the 163 off the end this gives a wrong time of

   GMT: Fri, 01 Jan 2016 22:01:17 GMT

Here is my code

   DateFormat dF = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss.SSSSSSSX");
   try {
    Date date = dF.parse(ts);
    System.out.println(date.getTime());
   } catch (ParseException a) {
    a.printStackTrace();
   }

My problem must be the SimpleDateFormat but cannot understand what the configuration should be. Any help appreciated. Thanks

user2635961
  • 379
  • 3
  • 19
  • 1
    The `8114163` part of your input string is interpreted as `8114163` **milliseconds** which is added to the rest of the interpreted date. `SimpleDateFormat` cannot handle fractional seconds. You'll need to trim them, or interpret them yourself before handing off the parsed result to `SimpleDateFormat`. – Sotirios Delimanolis Aug 01 '16 at 20:07
  • What are you trying to do exactly ? – Mateo Barahona Aug 01 '16 at 20:15
  • 1
    Use MM instead of mm for month, like : DateFormat dF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); mm stands for minutes – Mateo Barahona Aug 01 '16 at 20:31
  • Many thanks . I removed all the characters after the seconds and also put MM in for months and changed my SimpleDateFormat accordingly. That works all be it I'm an hour adrift (but I can deal with that). Thanks – user2635961 Aug 01 '16 at 21:06

0 Answers0