-1

I have a value in miliseconds 1601626934449

Which generated via https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis()

but can I somehow be able to get time in human readable format, or in brief I need to be able to know what the value in miliseconds 1601626934449 is ?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
mjoshi
  • 3
  • 1

3 Answers3

3

You can convert millis into LocalDateTime to store time

long millis = System.currentTimeMillis();
LocalDateTime datetime = Instant.ofEpochMilli(millis)
                                .atZone(ZoneId.systemDefault()).toLocalDateTime();

Then you can print your data using toString() or your desire format using DateTimeFormatter.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
System.out.println(datetime.format(formatter));

Output: 2020-10-02 18:39:54.609

Eklavya
  • 17,618
  • 4
  • 28
  • 57
3

Use java.time on Java 8 or higher. Using that, it's easy to reach your goal.
You basically create an Instant from the epoch milliseconds (which represent a moment in time), make it a ZonedDateTime by applying a ZoneId (my system's default in the following example) and then either format the output String by a built-in DateTimeFormatter or by creating a custom one with a desired pattern to make it as human-readable as required.

Here's an example:

public static void main(String[] args) {
    // your example millis
    long currentMillis = 1601626934449L;
    // create an instant from those millis
    Instant instant = Instant.ofEpochMilli(currentMillis);
    // use that instant and a time zone in order to get a suitable datetime object
    ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
    // then print the (implicitly called) toString() method of it
    System.out.println(currentMillis + " is " + zdt);
    // or create a different human-readable formatting by means of a custom formatter
    System.out.println(
        zdt.format(
            DateTimeFormatter.ofPattern(
                "EEEE, dd. 'of' MMMM uuuu 'at' HH:mm:ss 'o''clock in' VV 'with an offset of' xxx 'hours'",
                Locale.ENGLISH
            )
        )
    );
}

which outputs (on my system)

1601626934449 is 2020-10-02T10:22:14.449+02:00[Europe/Berlin]
Friday, 02. of October 2020 at 10:22:14 o'clock in Europe/Berlin with an offset of +02:00 hours
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    The question is so common that I voted to delete it without even looking at this nice answer. After voting to delete, I turned to this answer but it was already late as SO does not provide an option to retract a delete vote. Although many of my high-quality answers have been wasted because the questions were deleted (after getting closed) and I felt very sad every time but I never felt frustration which I am feeling right now because of not having the option to retract the delete vote. I wish no one else vote to delete this question! – Arvind Kumar Avinash Oct 11 '20 at 21:32
  • 1
    @ArvindKumarAvinash I think that's part of the game here on stackoverflow. So nevermind... What's interesting is the missing possibility of retracting a delete vote, didn't know that one can't. Thanks for mentioning, I will keep that in mind before I vote for deleting a question. – deHaar Oct 12 '20 at 06:29
0

You can create a Date object and use it to get all the information you need:

https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date(long)

Andi
  • 162
  • 9
  • I recommend you don’t. The `Date` class is poorly designed and long outdated. We’d prefer to use java.time, the modern Java date and time API, as in the other two answers. – Ole V.V. Oct 03 '20 at 14:02