Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert timestamp into 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
  }, 
  {
    "batch_date": 1419037000
  }
]
like image 988
pavaniiitn Avatar asked Jun 23 '15 05:06

pavaniiitn


People also ask

How do I convert a timestamp to a date?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

How do I change the format of a timestamp in Java?

We can format the Timestamp value using SimpleDateFormat class. Initially, by using the Timestamp class, the time is getting displayed in a standard format, but we can format it to our own choice using SimpleDateFormat class.

How do I convert a timestamp to a String in Java?

The toString() method of the java. sql. Timestamp class returns the JDBC escape format of the time stamp of the current Timestamp object as String variable. i.e. using this method you can convert a Timestamp object to a String.


2 Answers

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
like image 145
Scary Wombat Avatar answered Sep 22 '22 03:09

Scary Wombat


No Such Data Type

There is no such thing as "JSON timestamp". JSON has very few defined data types. No date-time types among them.

As the correct Answer by Scary Wombat states, your number is apparently a count of whole seconds from the Unix epoch of 1970. The java.util.Date class tracks time as milliseconds since the Unix epoch, rather than whole seconds. So you need to multiply by 1,000. You also need to deal with long integers (64-bit) rather than int (32-bit). Append an uppercase L to the numeric literals, and declare any variables as long.

The java.util.Date/.Calendar classes are notoriously troublesome. They are now supplanted by the java.time package built into Java 8 and later, and/or the 3rd-party Joda-Time library.

Various JSON-processing libraries support converters for creating java.time or Joda-Time objects. Or you can perform a conversion in your code, shown below.

Be aware that both java.time and Joda-Time supports assigning a time zone. Code below assigns UTC for demonstration purposes, but you can assign your desired/expected zone.

Joda-Time

Here is some code in Joda-Time 2.8.1 showing the use of your input number as either seconds or milliseconds.

long secondsSinceEpoch = 1419038000L;
DateTime dateTimeSeconds = new DateTime( secondsSinceEpoch , DateTimeZone.UTC );
DateTime dateTimeMillis = new DateTime( secondsSinceEpoch * 1000L , DateTimeZone.UTC );  // Note the crucial "L" appended to the numeric literal.

Dump to console.

System.out.println( "dateTimeSeconds: " + dateTimeSeconds );
System.out.println( "dateTimeMillis: " + dateTimeMillis );

When run.

dateTimeSeconds: 1970-01-17T10:10:38.000Z
dateTimeMillis: 2014-12-20T01:13:20.000Z

java.time

Similar code to above, but using java.time of Java 8.

Instant instant = Instant.ofEpochSecond( 1419038000L );
ZonedDateTime zdtUtc = ZonedDateTime.ofInstant( instant , ZoneOffset.UTC );
ZonedDateTime zdtMontréal = zdtUtc.withZoneSameInstant( ZoneId.of( "America/Montreal" ) );

Dump to console.

System.out.println( "zdtUtc: " + zdtUtc );
System.out.println( "zdtMontréal: " + zdtMontréal );

When run.

zdtUtc: 2014-12-20T01:13:20Z
zdtMontréal: 2014-12-19T20:13:20-05:00[America/Montreal]
like image 33
Basil Bourque Avatar answered Sep 24 '22 03:09

Basil Bourque