简体   繁体   中英

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
  }
]

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

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM