Do yous desire to convert milliseconds to Date inward Java ? Actually java.util.Date is internally specified inward milliseconds from epoch. So whatever appointment is publish of millisecond passed since Jan 1, 1970, 00:00:00 GMT together with Date provides constructor which tin live on used to create Date from milliseconds. Knowing the fact that Date is internally maintained inward milliseconds allows yous to shop appointment inward shape of millisecond inward Server or inward your Class because that tin live on effectively expressed alongside a long value. In fact many experienced Java programmer shop Date every bit long value spell writing Immutable bird which requires Date, 1 of the argue for that is Date existence mutable together with long value of Date tin live on really handy. By the ways this is side yesteryear side inward Date related article, nosotros convey already discussed How to convert String to Date together with How to larn electrical flow month, twelvemonth together with hateful solar daytime of calendar week from Date inward Java. If yous haven’t read them already, yous may notice them useful. In this Java tutorial nosotros volition encounter event of converting millisecond into Date inward Java.
Java programme to convert millisecond to Date inward Java
There are many unlike ways to convert milliseconds into Date inward Java. One tin purpose java.util.Date(long Millis) constructor or java.util.Calendar.setTimeInMillis() method. In this article nosotros volition encounter event of both methods to practice Date from Millisecond inward Java. By the agency hither nosotros are using SimpleDateFormat to format Date inward Java which is non thread-safe together with should non live on shared betwixt multiple threads.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Java programme to convert Millisecond to Date inward Java. Java API provides utility
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Java programme to convert Millisecond to Date inward Java. Java API provides utility
* method to larn millisecond from Date together with convert Millisecond to Date inward Java.
* @author http://javarevisited.blogspot.com
*/
public class MillisToDate {
public static void main(String args[]) {
//Converting milliseconds to Date using java.util.Date
//current fourth dimension inward 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 electrical flow 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:"
* @author http://javarevisited.blogspot.com
*/
public class MillisToDate {
public static void main(String args[]) {
//Converting milliseconds to Date using java.util.Date
//current fourth dimension inward 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 electrical flow 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 1 Date's value into merely about other Date inward Java
Date instantly = 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:
electrical flow Date: Midweek February 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
master copy Date: 29:02:12:01:58:46
copied Date: 29:02:12:01:58:46
//copying 1 Date's value into merely about other Date inward Java
Date instantly = 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:
electrical flow Date: Midweek February 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
master copy Date: 29:02:12:01:58:46
copied Date: 29:02:12:01:58:46
Another useful usage of keeping Date inward millisecond is, It’s slowly to convert betwixt java.util.Date together with java.sql.Date. As SQL doesn't supply Date inward shape of java.util.Date yous ofttimes quest to convert SQL Date to util Date but boot the bucket along value of Date every bit long millisecond value allows yous to practice both java.sql.Date together with java.util.Date. One to a greater extent than practice goodness of keeping appointment inward long millisecond value is, it’s slowly to re-create value of 1 Date into merely about other inward Java.
That's all on how to convert milliseconds to Date inward Java. We convey seen 2 approach 1 is using Date bird spell other is using Calendar class. I personally prefer java.util.Date way. allow me know if yous come upwards across whatever other agency of converting milliseconds into Date inward Java.
Further Learning
Complete Java Masterclass
SimpleDateFormat is non thread-safe inward Java purpose carefully