I'm trying to convert the created_utc date from Reddit's json to a Date object, but I keep getting an "Unparceable" error. An example of their dates is: created_utc": 1.43701862E9, which I'm told is a unix timestamp.
From my research this code should convert it:
String date = "1.43701862E9";
java.util.Date time = new java.util.Date((long)date*1000);
but obviously I'm getting an error on multiplying the date by 1000.
This is the code I normally use to convert string dates:
String date = "1.43701862E9";
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
format.parse(date);
This should work for you:
public static void main(String[] args) {
String date = "1.43701862E9";
java.util.Date time = new java.util.Date(Double.valueOf(date).longValue()*1000);
System.out.println(time);
}
Output:
Wed Jul 15 23:50:20 EDT 2015
Since you're using scientific notation you can't parse the String using the Long class: Long.parseLong(String s) (Nor can you simply cast a String, as you're trying). Instead, I used the Double.valueOf() method and preserve the Long using .longValue()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With