Converting Epoch time to date string

2019-01-24 00:51发布

问题:

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?

回答1:

Look into SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.format(new Date(myTimeAsLong));


回答2:

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);


回答3:

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



回答4:

Joda-Time

If by epoch time you meant a count of milliseconds since first moment of 1970 in UTC, then here is some example code using the Joda-Time library…

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTime = new DateTime( yourMilliseconds, timeZone );
String output = DateTimeFormat.forStyle( "S-" ).withLocale( Locale.CANADA_FRENCH ).print( dateTime );

Other Epochs

That definition of epoch is common because of its use within Unix. But be aware that at least a couple dozen epoch definitions are used by various computer systems.



回答5:

try this

Date date = new Date(1476126532838L);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
format.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));//your zone
formatted = format.format(date);
System.out.println(formatted);


回答6:

You need to be aware that epoch time in java is in milliseconds, while what you are converting may be in seconds. Ensure that both sides of the conversions are in milliseconds, and then you can fetch the date parameters from the Date object.



回答7:

Try this...

sample Epoch timestamp is 1414492391238

Method:

public static String GetHumanReadableDate(long epochSec, String dateFormatStr) {
    Date date = new Date(epochSec * 1000);
    SimpleDateFormat format = new SimpleDateFormat(dateFormatStr,
            Locale.getDefault());
    return format.format(date);
}

Usability:

long timestamp = Long.parseLong(engTime) / 1000;
String engTime_ = GetHumanReadableDate(timestamp, "dd-MM-yyyy HH:mm:ss aa");

Result:

28-10-2014 16:03:11 pm

Happy coding



回答8:

based upon previous answers, while passing system default Locale and TimeZone...

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));
}