Saturday, February 27, 2021

How to convert milliseconds to date in java


In java, java.util.Date class has constructor Date(long milliseconds) that provides a Date() class object for the passed milliseconds. The milliseconds are counterd from first moment of 1970 ie January 1, 1970, 00:00:00 GMT.

Below is the the program to convert milliseconds to date in java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class MillisToDateCoverter {
 
    public static void main(String[] args) {
        long milliSeconds = 1560672963853L; 
   
        //Creates a simple date formatter 
        DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss"); 
        //Create date from milliseconds
        Date date = new Date(milliSeconds); 
        //Parse the date using date formatter
        System.out.println("Date:" + dateFormat.format(date)); 
    } 
}

 

Output:  Date:16 Jun 2019 13:46:03

No comments:

Post a Comment

How to get value from Map using Key in Java8

 In Java8, using below code can get value from Map using key: package com.puneet.java8examples; import java.util.HashMap; import java.u...