Converting Epoch time to date string
Solution 1
Look into SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.format(new Date(myTimeAsLong));
Solution 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);
Solution 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
Solution 4
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));
}
Solution 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);
nexus490
Updated on October 30, 2021Comments
-
nexus490 over 2 years
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?
-
nexus490 over 12 yearsWhat would i put for the Timezone argument?
-
nexus490 over 12 yearsIve got that to print out a string in the correct format but it prints out the incorrect date it gives todays date as 16/00/1970. The Long used was 1332026487.
-
Reinard over 12 yearsIt's because you might have to play around with the parameter you pass in the constructor. You can find all display possibilities in the Jav API
-
Jon Skeet over 12 yearsThis answer: is printing the minute instead of month; ignores the choice of culture (which is very important); ignores the choice of time zone (which is incredibly important). You've really got to think about these things - it's no help (IMO) to just ignore them.
-
Jon Skeet over 12 years@nexus490: Well what time zone do you want to represent the value in? For example, in an hour it will be 18/03/2012 in my time zone, but in the US it will still be 17/03/2012, for the same number of milliseconds since the epoch. What would the right answer be? That's something only you know, as you know your application requirements (hopefully). Maybe you want UTC. Maybe you want the system local time zone. Maybe you want the user's home time zone.
-
dbm over 11 yearsThe Long provided by @nexus490 seems to be in seconds (i.e. the "real" epoch format), while the Java implementation of the Date class would need the time in milliseconds. Try multiplying your Long by 1000 and it should work. Besides that, +1000 for Jon Skeet's comment.
-
Basil Bourque over 9 years3 problems with this answer. This answer ignores the crucial issue of time zone. This answer uses a different format than requested in the Question. And this answer essentially duplicates three other answers posted months ago. I don't see any value-add here.
-
Javad over 9 yearsThank for for stressing this: "if you've got seconds since the epoch, multiply by 1000."
-
Leo over 8 yearsJust a minor comment, you need to change the HH:MM:ss.SSS to HH:mm:ss.SSS -- MM is the month, and not minutes.
-
harperville over 4 yearsThis is how you'd get milliseconds:
long now = Instant.now().toEpochMilli();
Also,import java.time.Instant;
andimport java.text.SimpleDateFormat;
I'm a Java noob and I had to look elsewhere for the full picture. For the sake of being complete, I'm adding this here. -
C.T. Bell over 2 yearsPerfect! Thank you