How To Convert Milliseconds To Appointment Inwards Coffee - Tutorial Example
Do you lot desire to convert milliseconds to Date inward Java ? Actually java.util.Date is internally specified inward milliseconds from epoch. So whatever appointment is expose of millisecond passed since Jan 1, 1970, 00:00:00 GMT in addition to Date provides constructor which tin give the sack hold out used to create Date from milliseconds. Knowing the fact that Date is internally maintained inward milliseconds allows you lot to shop appointment inward shape of millisecond inward Server or inward your Class because that tin give the sack hold out effectively expressed amongst a long value. In fact many experienced Java programmer shop Date every bit long value piece writing Immutable shape which requires Date, i of the argue for that is Date existence mutable in addition to long value of Date tin give the sack hold out really handy. By the ways this is adjacent inward Date related article, nosotros accept already discussed How to convert String to Date in addition to How to instruct electrical current month, twelvemonth in addition to twenty-four hours of calendar week from Date inward Java. If you lot haven’t read them already, you lot may notice them useful. In this Java tutorial nosotros volition run into instance of converting millisecond into Date inward Java.
Java plan to convert millisecond to Date inward Java
There are many dissimilar ways to convert milliseconds into Date inward Java. One tin give the sack role java.util.Date(long Millis) constructor or java.util.Calendar.setTimeInMillis() method. In this article nosotros volition run into instance of both methods to exercise Date from Millisecond inward Java. By the agency hither nosotros are using SimpleDateFormat to format Date inward Java which is non thread-safe in addition to should non hold out shared betwixt multiple threads.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Java plan to convert Millisecond to Date inward Java. Java API provides utility
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Java plan to convert Millisecond to Date inward Java. Java API provides utility
* method to instruct millisecond from Date in addition to 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 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:"
* @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 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 i Date's value into only about other Date inward Java
Date straightaway = 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 current 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 i Date's value into only about other Date inward Java
Date straightaway = 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 current 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 tardily to convert betwixt java.util.Date in addition to java.sql.Date. As SQL doesn't supply Date inward shape of java.util.Date you lot ofttimes need to convert SQL Date to util Date but cash inward one's chips on value of Date every bit long millisecond value allows you lot to exercise both java.sql.Date in addition to java.util.Date. One to a greater extent than exercise goodness of keeping appointment inward long millisecond value is, it’s tardily to re-create value of i Date into only about other inward Java.
That's all on how to convert milliseconds to Date inward Java. We accept seen 2 approach i is using Date shape piece other is using Calendar class. I personally prefer java.util.Date way. allow me know if you lot come upward across whatever other agency of converting milliseconds into Date inward Java.
Further Learning
Complete Java Masterclass
SimpleDateFormat is non thread-safe inward Java role carefully
Belum ada Komentar untuk "How To Convert Milliseconds To Appointment Inwards Coffee - Tutorial Example"
Posting Komentar