Converting Epoch time to date string Converting Epoch time to date string android android

Converting Epoch time to date string


Look into SimpleDateFormat

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


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


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