Sunday 6 March 2022

Java: convert milliseconds to Date object

Date class provides a constructor 'Date(long time)', that takes time in milliseconds and return Date object from it.

 

Signature

public Date(long time)

Find the below working application.

 

MilliSecondsToDate.java

package com.sample.app.time;

import java.util.Date;

public class MilliSecondsToDate {

	public static void main(String[] args) {
		Date today = new Date();

		long timeAfterOneHour = today.getTime() + 60 * 60 * 1000;

		Date timeAfterAnHour = new Date(timeAfterOneHour);

		System.out.println("today : " + today);
		System.out.println("timeAfterAnHour : " + timeAfterAnHour);

	}

}

Output

today : Sat Feb 26 09:47:30 IST 2022
timeAfterAnHour : Sat Feb 26 10:47:30 IST 2022




 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment