Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Epoch time to date string

I have seen this question asked multiple times and none of the answers seem to be what i need.

I have a long type variable which has an epoch time stored in it.

What i want to do is convert it to a String

for example if the epoch time stored was for today the final string would read:

17/03/2012

How would i to this?

like image 261
nexus490 Avatar asked Mar 17 '12 23:03

nexus490


People also ask

How do I convert epoch string to date?

Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds.

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 Unix epoch time to date string in hive?

Hive from_unixtime() is used to get Date and Timestamp in a default format yyyy-MM-dd HH:mm:ss from Unix epoch seconds. Specify the second argument in pattern format to return date and timestamp in a custom format.

What is epoch date format?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).


4 Answers

Look into SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.format(new Date(myTimeAsLong));
like image 129
Reinard Avatar answered Oct 21 '22 01:10

Reinard


You'd create a Date from the long - that's easy:

Date date = new Date(epochTime);

Note that epochTime here ought to be in milliseconds since the epoch - if you've got seconds since the epoch, multiply by 1000.

Then you'd create a SimpleDateFormat specifying the relevant pattern, culture and time zone. For example:

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
format.setTimeZone(...);

Then use that to format the date to a string:

String text = format.format(date);
like image 29
Jon Skeet Avatar answered Oct 21 '22 02:10

Jon Skeet


Date date = new Date(String); 

this is deprecated.

solution

  Date date = new Date(1406178443 * 1000L);
  DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
  String formatted = format.format(date);

make sure multiply by 1000L

like image 20
Damith Ganegoda Avatar answered Oct 21 '22 03:10

Damith Ganegoda


If the method should be portable, better use the default (local time) TimeZone.getDefault():

String epochToIso8601(long time) {
    String format = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
    sdf.setTimeZone(TimeZone.getDefault());
    return sdf.format(new Date(time * 1000));
}
like image 5
Martin Zeitler Avatar answered Oct 21 '22 03:10

Martin Zeitler