How to convert Object as timestamp to formatted date?
I have printed out timestamp(
1395500668
) that is in Object. I want to format this and print it out like
yyyy-MM-dd H:m:s
How to convert Object as timestamp to formatted date?
I have printed out timestamp(
1395500668
) that is in Object. I want to format this and print it out like
yyyy-MM-dd H:m:s
Assuming you have a string that represents epoch time in seconds (as you have not told us what your Object
actually is), first convert it to a long
:
long epoch = Long.parseLong("1395500668");
You'll then need to convert it to milliseconds:
epoch *= 1000;
Then you can convert it to a java.util.Date
using the Date
constructor that takes millisecond epoch time:
Date date = new Date(epoch);
And finally you can format that Date
as a string using standard formatting techniques.
First convert the timestamp value
into Date
and then format the date into your desired format using SimpleDateFormat
java.util.Date date = new java.util.Date(new Long(1395500668) * 1000);
String dateStr = new java.text.SimpleDateFormat("yyyy-MM-dd H:m:s").format(date);
System.out.println(dateStr);
It outputs:
2014-03-22 8:4:28
Convert it first into a Date
first by casting the Object
into a Timestamp
and then using the getTime()
method as shown in How to convert TimeStamp to Date in Java?
Then use a SimpleDateFormat
to format the date as needed as shown in Change date format in a Java string