Yugandhar Yugandhar - 5 months ago 25
JSON Question

How to convert a timestamp in JSON into normal date and time in java?

I have a timestamp in json that is from a Linux server. I would like to convert it into a simple date time format using Java.

I need the date and time in the following format : dd-mm-yyyy hh:mm:ss

here is my JSON data:

batch_date:1419038000
[
{
"total_batches": 1,
"total_sessions": 12,
"cid": 12,
"batch_date": 1419038000,
"bid": 131,
"completed_sessions": 2,
"c_image": "http://sc02.learnsocial.com/ci/234x140/12-696-Big-Data--Hadoop-Development.jpg",
"c_title": "Big Data & Hadoop Development",
"learners": 451
},
{
"total_batches": 1,
"total_sessions": 12,
"cid": 45,
"batch_date": 1419037000,
"bid": 154,
"completed_sessions": 34,
"c_image": "https://d46zycbjkz8af.cloudfront.net/ci/234x140/9-638-ANDROID-DEVELOPMENT.jpg",
"c_title": "Android Development",
"learners": 238
},
{
"total_batches": 1,
"total_sessions": 12,
"cid": 34,
"batch_date": 1419036000,
"bid": 451,
"completed_sessions": 33,
"c_image": "https://d46zycbjkz8af.cloudfront.net/ci/234x140/1278-1787-Java-Tutorial-For-Beginners-Step-by-Step.jpg",
"c_title": "Java Development",
"learners": 827
}
]

Answer

The batch date

"batch_date": 1419038000, 

looks like seconds from epoch,

so

new Date (batch_date * 1000); 

then use SimpleDateFormat should do the trick

SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

-- code --

    long batch_date = 1419038000; 
    Date dt = new Date (batch_date * 1000); 

    SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    System.out.println(sfd.format(dt));

-- output --

20-12-2014 10:13:20
Comments