Saturday, August 21, 2021

How to convert milliseconds to Date in Java - Tutorial example

Do you want to convert milliseconds to Date in Java? Actually java.util.Date is internally specified in milliseconds from epoch. So any date is the number of milliseconds passed since January 1, 1970, 00:00:00 GMT and Date provides constructor which can be used to create Date from milliseconds. Knowing the fact that Date is internally maintained in milliseconds allows you to store date in form of milliseconds in Server or in your Class because that can be effectively expressed with a long value.

In fact, many experienced Java programmers store Date as long values while writing Immutable class which requires Date, one of the reasons for that is Date being mutable and long value of Date can be very handy? 

By the ways this is next in Date related article, we have already discussed How to convert String to Date and How to get a current month, year, and day of the week from Date in Java. If you haven’t read them already, you may find them useful. In this Java tutorial, we will see an example of converting milliseconds into Dates in Java.



Java program to convert millisecond to Date in Java

How to create Date from millisecond in Java with ExampleThere are many different ways to convert milliseconds into Date in Java. One can use java.util.Date(long Millis) constructor or java.util.Calendar.setTimeInMillis() method. In this article, we will see examples of both methods to create Date from A millisecond in Java. 

By the way here we are using SimpleDateFormat to format Date in Java which is not thread-safe and should not be shared between multiple threads.



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Java program to convert  Millisecond to Date in Java. Java API provides utility
 * method to get millisecond from Date and convert Millisecond to Date in Java.
 * @author http://javarevisited.blogspot.com
 */

public class MillisToDate {
 
    public static void main(String args[]) {
     
       //Converting milliseconds to Date using java.util.Date
       //current time in milliseconds
       long currentDateTime = System.currentTimeMillis();
     
       //creating Date from millisecond
       Date currentDate = new Date(currentDateTime);
     
       //printing value of Date
       System.out.println("current Date: " + currentDate);
     
       DateFormat df = new SimpleDateFormat("dd:MM:yy:HH:mm:ss");
     
       //formatted value of current Date
       System.out.println("Milliseconds to Date: " + df.format(currentDate));
     
       //Converting milliseconds to Date using Calendar
       Calendar cal = Calendar.getInstance();
       cal.setTimeInMillis(currentDateTime);
       System.out.println("Milliseconds to Date using Calendar:"
               + df.format(cal.getTime()));
     
       //copying one Date's value into another Date in Java
       Date now = new Date();
       Date copiedDate = new Date(now.getTime());
     
       System.out.println("original Date: " + df.format(now));
       System.out.println("copied Date: " + df.format(copiedDate));
    }
     
}

Output:
current Date: Wed Feb 29 01:58:46 VET 2012
Milliseconds to Date: 29:02:12:01:58:46
Milliseconds to Date using Calendar:29:02:12:01:58:46
original Date: 29:02:12:01:58:46
copied Date: 29:02:12:01:58:46


Another useful usage of keeping Date in a millisecond is, It’s easy to convert between java.util.Date and java.sql.Date. SQL doesn't provide Date in form of java.util.Date you often need to convert SQL Date to util Date but keep the value of Date as long millisecond value allows you to create both java.sql.Date and java.util.Date. One more benefit of keeping the date in long millisecond value is, it’s easy to copy the value of one Date into another in Java.

That's all on how to convert milliseconds to Date in Java. We have seen two approaches one is using Date class while the other is using Calendar class. I personally prefer java.util.Date way. let me know if you come across any other way of converting milliseconds into Date in Java.


Other Date and Time tutorials from Javarevisited Blog

2 comments :

Anonymous said...

1. Epoch date is used as the reference date; which is represented by zero (0). There fore it is possible to have dates before the Epoch date, simply by giving negative long number. (-12239424000000L should represent 24 Feb 1582; the date when Gregorian Calendar was introduced).

2. Java 8 has introduced a new package java.time (http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) and many new classes for working with date and time. For working with instant you could use the java.time.Instant class (http://docs.oracle.com/javase/8/docs/api/java/time/Instant.html)

* To convert from Date to Instant:
Instant.ofEpochMilli(date.getTime());

* To convert from Instant to Date:
Date.from(instant);

3. java.sql.Date, java.sql.Time, and java.sql.Timestamp all extend from java.util.Date

Charles said...

You are losing the milliseconds. Java 8's DataTimeFormatter class doesn't seem to support that.

Post a Comment