What is the easiest way to do this in Java? Ideally I will be using Unix time in milliseconds as input and the function will output a String like
November 7th, 2011 at 5:00 PM
I wanted to convert my unix_timestamps like 1372493313 to human readable format like Jun 29 4:08.
The above answered helped me for my Android app code. A little difference was that on Android it recommends to use locale settings as well, and my original unix_timestamp was in seconds, not milliseconds, and Eclipse wanted to add try/catch block or throw exception. So my working code has to be modified a bit like this:
/**
*
* @param unix_timestamp
* @return
* @throws ParseException
*/
private String unixToDate(String unix_timestamp) throws ParseException {
long timestamp = Long.parseLong(unix_timestamp) * 1000;
SimpleDateFormat sdf = new SimpleDateFormat("MMM d H:mm", Locale.CANADA);
String date = sdf.format(timestamp);
return date.toString();
}
And here is the calling code:
String formatted_timestamp;
try {
formatted_timestamp = unixToDate(unix_timestamp); // timestamp in seconds
} catch (ParseException e) {
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With