How to convert timestamp into date and time in java?
Asked Answered
T

3

5

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
  }
]
Typewriter answered 23/6, 2015 at 5:28 Comment(2)
How are you parsing the Json? Look at gson or jacksonSometime
@pavankumarparasa Please avoid posting extraneous info and data. Minimize your scenario to the minimum needed to demonstrate your issue. See How to create a Minimal, Complete, and Verifiable example.Alephnull
U
8

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
Unders answered 23/6, 2015 at 5:30 Comment(3)
You mean HH, not hh.Skinner
@ScaryWombat can we read the timestamp? without using these DateFormats. I mean just by looking at it, can we say which year or month or date it is resembling.Woodie
the timestamp is in seconds since 1970/01/01 so yeah, if you have the skill you can work it out, but why would you?Unders
A
3

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]
Alephnull answered 23/6, 2015 at 20:44 Comment(2)
I mean...I have a timestamp in jsonTypewriter
Good info for the comment about Time Zone ! I was wondering why I obtain 01:13:20.000Z and not 10:13:20 (as Scary Wombat ) for the time part !Chou
H
-3

when we want read/parse a string value as date, we should know the date format of that input string, otherwise we will get wrong date value.

batch_date:1419038000 what is the date format of this value ?

If we want to get the date value in the required format as in your case 'dd-mm-yyyy hh:mm:ss'. there are two possible ways.

1) request batch_date value should be in your required format. or

2) if batch_date value is another format, we should know the original format, and build a Date object with original format using SimpleDateFormat parse method and then convert it into required format using another SimpleDateFormat format method.

Higbee answered 23/6, 2015 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.