How to change milliseconds to Date object in yyyy-MM-dd HH:mm:ss format like 2017-04-12 23:14:52?
-
please provide the input milliseconds? – kk. Jun 12 '17 at 14:08
-
6Do you understand that a `Date` object doesn't have a format? Please read https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/ – Jon Skeet Jun 12 '17 at 14:08
-
I hope this answer will help you https://stackoverflow.com/questions/4142313/java-convert-milliseconds-to-time-format – kk. Jun 12 '17 at 14:09
-
What have you tried? Did you search before asking? *Very* funny if nothing useful and helpful turned up. It’s certainly already out there, and you can find it faster than anyone can type it once again here. – Ole V.V. Jun 12 '17 at 14:30
-
@Tim, the question you are linking to is only asking about (and getting) formatting into hours, minutes, seconds and millis, not year, month and day as is being asked for here. I agree with you that this question must have been asked and answered not only once before, but many times. – Ole V.V. Jun 13 '17 at 09:28
-
1I also agree with you, @Tim, that there seems to be two questions in this one: (1) how to convert millis since the epoch to a date (or date-time) object (2) how to format said object. *Both* have been asked and answered many times before. – Ole V.V. Jun 13 '17 at 09:31
2 Answers
You cannot do that. For a couple of reasons.
TL;DR: Don’t use Date
, use Instant
. Neither of these can have a format in them. Formatting into a string is dependent on time zone, so you need to choose a time zone.
First, I understand from the discussion that you are asking for a java.util.Date
object having the format yyyy-MM-dd HH:mm:ss
. A Date
object does not have and cannot have a format in it. The thing you should try to understand here is the difference between data itself and presentation of data to a user. An int
may hold the value 25389, but it doesn’t hold it in the format 25389 (in fact the internal representation is quite different from 25389). The same int
may be presented to a user as 25389, 000025389, 25,389 or +25389, just to mention a few out of many possibilities. The formatting happens outside the int
while the int
stays just the same.
Similarly, a Date
object holds a point in time. The same date may be formatted into for example 2017-04-12 23:14:52 or April 12, 2017 11:14:52 PM. It may even be formatted for different time zones, which would be a good idea if the system has users in different time zones. Alternatively we may show the user a calendar leaf and/or a clock showing the time. Again, formatting happens outside of the Date
while the Date
stays just the same.
Elaborating on the time zone issue, the same point in time represented by the same millisecond value could be formatted to 2017-04-12 17:44:52 in UTC, 2017-04-12 19:44:52 in my time zone, 2017-04-12 23:14:52 in Asia/Kolkata time zone or even 2017-04-13 05:44:52 in Pacific/Auckland time zone. Note that in the last case not even the date is the same. So there is not just one way to change your milliseconds into the format you asked for. We need to know which time zone you want it for before we can help you.
So what I believe you need is not one thing, but two
- A way to store your point in time in your program.
- A way to format your point in time into a string in yyyy-MM-dd HH:mm:ss format for a user in some time zone.
For storing your point in time, use either of
- A
long
for the milliseconds value you already have - A
java.time.Instant
object.
Why didn’t I mention java.util.Date
? Because this class is long outdated. Its design turned out to be troublesome very quickly. They tried to repair it by deprecating most of the methods and introducing java.util.Calendar
, but that didn’t work very well either. Finally, drawing on the experiences from a library known as Joda-Time they introduced the java.time
classes in Java 8 in 2014. That’s three years ago as of writing, and counting. So IMHO we should by now have thrown Date
and friends overboard and started using the newer classes. So prefer Instant
over Date
.
Changing your milliseconds to an Instant
is straightforward:
long milliseconds = 1492019092000L;
Instant pointInTime = Instant.ofEpochMilli(milliseconds);
For formatting your instant into a string for the user, as I said, we require a time zone. Then do
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
String formattedDateTimeString = pointInTime.atZone(ZoneId.of("Asia/Kolkata"))
.format(formatter);
So you need to fill in the desired time zone where I put ZoneId.of("Asia/Kolkata")
. If you want to use the JVM’s current time zone setting, just fill in ZoneId.systemDefault()
. Beware, though, that the time zone setting may be changed, even by an unrelated program running in the same JVM, so relying on this may be fragile.
The result of the above code snippet is a string like
2017-04-12 23:14:52
PS If after reading the above you really insist, here’s how to get a java.util.Date
from the above:
Date myOutdatedDateInstance = Date.from(pointInTime);
(and excuse me for repeating, it still doesn’t have the desired format, that is not possible).

- 81,772
- 15
- 137
- 161
You can try this sample code.
public class MillDateConverter {
public static String dFormat = "yyy-MM-dd HH:mm:ss";
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dFormat);
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
long milliSec=Long.parseLong("1086073200000");
System.out.println(milliSec);
calendar.setTimeInMillis(milliSec);
System.out.println(simpleDateFormat.format(calendar.getTime()));
}
}

- 315
- 2
- 7
-
By doing this i will get String simpleDateFormat.format(calendar.getTime()) returns String but i want java.utill.Date Object. I tried doing simpleDateFormat.format(calendar.getTime())) after getting the String but then i am getting Tue Apr 25 23:52:17 IST 2017 format which i don't want – Nilendu Kumar Jun 12 '17 at 16:33
-
You are asking the impossible, or maybe rather, the self-contradictory, @NilenduKumar. A `java.util.Date` object hasn’t got and cannot have a format in it. It’s a point in time, nothing more. – Ole V.V. Jun 13 '17 at 20:09