user3776241 user3776241 - 1 month ago 22
Java Question

Date to timestamp

I'm trying to convert this date string 2014-07-17T22:41:17+0000 to a timestmap, similar to this: 1405645259000.

I've tried something similar to this:

String string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
System.out.println(date);


How do I convert this (2014-07-17T22:41:17+000) to a Date? What is the correct format? I'm at a loss.

Answer

Take a look at http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Your date string has the same format as

yyyy-MM-dd'T'HH:mm:ssZ  //Eg: 2001-07-04T12:08:56-0700

So, your code should be:

String string = "2014-07-17T22:41:17+0000";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH).parse(string);
System.out.println(date);