-2

It seems a Javascript date in String, but how to format it to a java.util.Date? Is it possible by SimpleDateFormat?

Okay, I didn't read it carefully.

Sayakiss
  • 6,878
  • 8
  • 61
  • 107

3 Answers3

2

One of the examples mentioned in the documentation exactly matches your case:

"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"  2001-07-04T12:08:56.235-07:00

So

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse(yourString);

will give what you want.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
1

The pattern of javascript date you are getting is

"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"  2001-07-04T12:08:56.235-07:00

in terms of java.

Use the sample code:

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.SimpleDateFormat;

class Test
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        System.out.println(sdf.parse("2015-08-01T06:51:14.000+08:00"));
        Date date = sdf.parse("2015-08-01T06:51:14.000+08:00");
    }
}
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
0

From the API documentation of SimpleDateFormat (see the examples), the pattern to use in your case is:

"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"

Bear in mind that using Z instead of XXX only works if time zone is written as -0700 (no colon).

joanlofe
  • 3,360
  • 2
  • 26
  • 44