A quick guide to get the current date time in milliseconds using Date, Calendar and java 8 api classes.
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.
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);
}
}
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.
0 comments:
Post a Comment