Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Epoch seconds to date and time format in Java

I have seconds since 1970 january 1 UTC (Epoch time).

 1320105600 

I need to convert that seconds into date and time in below format.

 Friday,November 4,2011 5:00,AM 

How can I achieve this?

like image 973
androiduser Avatar asked Nov 24 '11 20:11

androiduser


People also ask

How do I convert epoch time to manual date?

use strict; use warnings; use Time::Piece; my $datestring = '07-06-2019 21:13:00'; my $time = localtime->strptime($datestring, '%d-%m-%Y %H:%M:%S'); my $epoch = $time->epoch; ... my $time = localtime($epoch); my $datestring = $time->strftime('%d-%m-%Y %H:%M:%S');

How do I convert epoch time to string?

parseLong(epochTime); //convert seconds to milliseconds Date date = new Date(unix_seconds * 1000 L); // format of the date SimpleDateFormat jdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String java_date = jdf. format(date); //System. out. println("\n" + java_date + "\n"); IDataUtil.

What is epoch time format?

In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.

Why is epoch time 1970?

January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch. Early Unix engineers picked that date arbitrarily because they needed to set a uniform date for the start of time, and New Year's Day, 1970, seemed most convenient.


1 Answers

In case you're restricted to legacy java.util.Date and java.util.Calendar APIs, you need to take into account that the timestamps are interpreted in milliseconds, not seconds. So you first need to multiply it by 1000 to get the timestamp in milliseconds.

long seconds = 1320105600; long millis = seconds * 1000; 

This way you can feed it to a.o. the constructor of java.util.Date and finally use SimpleDateFormat to convert a java.util.Date to java.lang.String in the desired date format pattern, if necessary with a predefined time zone (otherwise it would use the system default time zone, which is not GMT/UTC per se and thus the formatted time might be off).

Date date = new Date(millis); SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String formattedDate = sdf.format(date); System.out.println(formattedDate); // Tuesday,November 1,2011 12:00,AM 

In case you're already on Java8, there's a LocalDateTime#ofEpochSecond() which allows you to feed epoch seconds directly without the need for multiplying into milliseconds flavor.

LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH); String formattedDate = dateTime.format(formatter); System.out.println(formattedDate); // Tuesday,November 1,2011 12:00,AM 
like image 191
BalusC Avatar answered Sep 25 '22 01:09

BalusC