Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Saturday, December 12, 2020

Java - Get Time In MilliSeconds

1. Overview

In this tutorial, We'll learn how to get the time in milliseconds in java. Time in milliseconds is the right way and format in storing into the database for date time columns. Because this is stored as Number type and which reduces the space than DateTime type in SQL.

Let us come to our topic today is getting the time milliseconds can be retrieved from Date, Calendar and java 8 api classes such Instant, ZonedDateTime classes.

Java - Get Time In MilliSeconds


2. Using java.util.Date


First, we'll try with the simple way to get the time in milliseconds format is from Date class. Date class has a method getTime() which returns the milliseconds in long value for the given time or current time.

package com.javaprogramto.java8.dates.milliseconds;

import java.util.Date;

/**
 * Example to get time in milli seconds in java using util Date api
 * 
 * @author JavaProgramTo.com
 *
 */
public class MilliSecondsFromDate {

	public static void main(String[] args) {
		
		// Getting the current date from Date class.
		Date currentDate = new Date();
		
		// Getting the time in milliseconds.
		long milliSeconds = currentDate.getTime();
		
		// printing the values
		System.out.println("Current date : "+currentDate);
		System.out.println("Current date time in milliseconds : "+milliSeconds);
		
		// Creating the future date
		Date futureDate = new Date(2025, 01, 01, 02, 30, 50);
		
		// Getting the future date
		milliSeconds = futureDate.getTime();
		
		// printing the future date time values
		System.out.println("Future date : "+futureDate);
		System.out.println("Future date time in milliseconds : "+milliSeconds);
	}
}

Output:
Current date : Sat Dec 12 21:48:25 IST 2020
Current date time in milliseconds : 1607789905027
Future date : Sun Feb 01 02:30:50 IST 3925
Future date time in milliseconds : 61696501250000

3. Using java.util.Calendar


Next, use the Calendar class to get the time in milli seconds. This class has a method getTimeInMillis() which returns the milliseconds for the time.

package com.javaprogramto.java8.dates.milliseconds;

import java.util.Calendar;
import java.util.Locale;

/**
 * Example to get time in milli seconds in java using Calendar api
 * 
 * @author JavaProgramTo.com
 *
 */
public class MilliSecondsFromCalendar {

	public static void main(String[] args) {
		
		// Getting the current date from Calendar class.
		Calendar calendar = Calendar.getInstance();
		
		// Getting the time in milliseconds.
		long milliSeconds = calendar.getTimeInMillis();
		
		// printing the values
		System.out.println("Current calender time in milliseconds : "+milliSeconds);
		
		// Creating another calendar object for Canada locale
		Calendar canadaLocale = Calendar.getInstance(Locale.CANADA);
		
		// Getting the future date
		milliSeconds = canadaLocale.getTimeInMillis();
		
		// printing the future date time values
		System.out.println("Future date time in milliseconds : "+milliSeconds);
	}
}

Output:
Current calender time in milliseconds : 1607790439838
Future date time in milliseconds : 1607790439859

4. Using Java 8 API


There are multiple ways to get the date time in milliseconds in java 8 date time api using Instant and ZonedDateTime classes.

Use toEpochMilli() method to get the date time in milli seconds epoch format.

package com.javaprogramto.java8.dates.milliseconds;

import java.time.Instant;
import java.time.ZonedDateTime;

/**
 * Example to get time in milli seconds in java 8 Using ZonedDateTime and Instant.
 * 
 * @author JavaProgramTo.com
 *
 */
public class MilliSecondsInJava8 {

	public static void main(String[] args) {
		
		// Getting milli seconds from ZonedDateTime class.
		
		// Creating zoned date time
		ZonedDateTime dateTime = ZonedDateTime.now();
		
		// getting the instant from zoned date time
		Instant instant = dateTime.toInstant();
		
		// Converting Instant time to epoch format milli seconds
		long timeInMilliSeconds = instant.toEpochMilli();
		
		// print the output
		System.out.println("Milli seconds from ZonedDateTime : "+timeInMilliSeconds);
		
		// Getting the milli seconds from Instant class.
		// Creating Instant object
		Instant instantTime = Instant.now();
		
		// Getting millis epoch value
		timeInMilliSeconds = instantTime.toEpochMilli();
		
		// printing
		System.out.println("Milli seconds from Instant : "+timeInMilliSeconds);
	}
}

Output:
Milli seconds from ZonedDateTime : 1607790957290
Milli seconds from Instant : 1607790957291

5. Conclusion


In this article, we've seen how to get the time in milli seconds in java 8 and older versions with examples.

Date class - use getTime() method
Calender class - use getTimeInMilli()
Java 8 api - use toEpochMilli()


No comments:

Post a Comment

Please do not add any spam links in the comments section.