How To Convert Milliseconds To Appointment Inwards Coffee - Tutorial Example

Do you lot desire to convert milliseconds to Date inwards Java ? Actually java.util.Date is internally specified inwards milliseconds from epoch. So whatsoever appointment is publish of millisecond passed since Jan 1, 1970, 00:00:00 GMT in addition to Date provides constructor which tin endure used to create Date from milliseconds. Knowing the fact that Date is internally maintained inwards milliseconds allows you lot to shop appointment inwards shape of millisecond inwards Server or inwards your Class because that tin endure effectively expressed amongst a long value. In fact many experienced Java programmer shop Date every bit long value acre writing Immutable degree which requires Date, 1 of the argue for that is Date existence mutable in addition to long value of Date tin endure rattling handy. By the ways this is adjacent inwards Date related article, nosotros convey already discussed How to convert String to Date in addition to How to instruct electrical current month, yr in addition to hateful solar daytime of calendar week from Date inwards Java. If you lot haven’t read them already, you lot may notice them useful. In this Java tutorial nosotros volition encounter instance of converting millisecond into Date inwards Java.


Java plan to convert millisecond to Date inwards Java

Do you lot desire to convert milliseconds to Date inwards Java  How to convert milliseconds to Date inwards Java - tutorial exampleThere are many dissimilar ways to convert milliseconds into Date inwards Java. One tin purpose java.util.Date(long Millis) constructor or java.util.Calendar.setTimeInMillis() method. In this article nosotros volition encounter instance of both methods to exercise Date from Millisecond inwards Java. By the means hither nosotros are using SimpleDateFormat to format Date inwards Java which is non thread-safe in addition to should non endure 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 inwards Java. Java API provides utility
 * method to instruct millisecond from Date in addition to convert Millisecond to Date inwards 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 inwards 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 1 Date's value into only about other Date inwards Java
       Date at 1 time = 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: quarta-feira 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 inwards millisecond is, It’s slowly to convert betwixt java.util.Date in addition to java.sql.Date. As SQL doesn't render Date inwards shape of java.util.Date you lot oft bespeak to convert SQL Date to util Date but drib dead along 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 inwards long millisecond value is, it’s slowly to re-create value of 1 Date into only about other inwards Java.

That's all on how to convert milliseconds to Date inwards Java. We convey seen ii approach 1 is using Date degree acre other is using Calendar class. I personally prefer java.util.Date way. allow me know if you lot come upward across whatsoever other means of converting milliseconds into Date inwards Java.

Further Learning
Complete Java Masterclass
SimpleDateFormat is non thread-safe inwards Java purpose carefully

Sumber https://javarevisited.blogspot.com/

0 Response to "How To Convert Milliseconds To Appointment Inwards Coffee - Tutorial Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel