$show=/label

How To Get Current Date and Time in Java 8 and Older?

SHARE:

A quick guide to get the current date and time in java and also using new java 8 date time api.

1. Overview

In this tutorial, We'll learn how to get the current date and time in java and new date time api in java 8.

There are different classes in java to get the current date and time and let us explore the example programs.

java.util.Date
Java 8 - java.time.Clock
java.util.Calendar
java.sql.Date
java.text.SimpleDateFormat
Java 8 - java.time.LocalDate
Java 8 - java.time.LocalTime
Java 8 - java.time.LocalDateTime
Java 8 - java.time.ZonedDateTime
Java 8 - java.time.format.DateTimeFormatter

How To Get Current Date and Time in Java 8 and Older?


2. Using java.util.Date


Use the util Date class constructor to get the current date and time values.

package com.javaprogramto.java8.dates.getdatetime;

import java.util.Date;

public class DateExample {

	public static void main(String[] args) {

		// using new Date()
		Date currentDateTime = new Date();
		System.out.println("Current date time using Date() : " + currentDateTime);

		// using System.currentTimeMillis()
		long milliSeconds = System.currentTimeMillis();
		currentDateTime = new Date(milliSeconds);
		System.out.println("Current date time using System.currentTimeMillis() : " + currentDateTime);
	}
}

Output:

Current date time using Date() : Tue Dec 15 20:47:44 IST 2020
Current date time using System.currentTimeMillis() : Tue Dec 15 20:47:44 IST 2020

3. Using java.util.Calendar


Use Calendar.getTime() method to get the Date instance.

package com.javaprogramto.java8.dates.getdatetime;

import java.util.Calendar;
import java.util.Date;

public class CalendarExample {

	public static void main(String[] args) {
		
		// Getting the calendar object
		Calendar cal = Calendar.getInstance();

		// Getting the util Date.
		Date currentDateTime = cal.getTime();
		System.out.println("Current Date Time : "+currentDateTime);
	}
}

Output:

Current Date Time : Tue Dec 15 20:51:02 IST 2020

4. Using java 8 - java.time.Clock


Use java 8 new method Instant() method from Clock class.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.Clock;
import java.time.Instant;

public class ClockExample {

	public static void main(String[] args) {
		Clock clockInUTC = Clock.systemUTC();
		Instant instnat = clockInUTC.instant();
		
		System.out.println("Java 8 - Current date time : "+instnat);
	}
}

Output:


5. Using java.sql.Date


This sql Date class returns only the Date part without time units. Pass the time in milliseconds to the sql Date() constructor and returns the date.

package com.javaprogramto.java8.dates.getdatetime;

import java.sql.Date;

public class SqlDateExample {

	public static void main(String[] args) {
		
		long timeInMillis = System.currentTimeMillis();
		
		Date sqlDate = new Date(timeInMillis);
		
		System.out.println("Current date from sql date : "+sqlDate);
	}
}

Output:
Current date from sql date : 2020-12-15

6. Using java.text.SimpleDateFormat


Use SimpleDateFormat class to get the date and time in our custom desired format.

Next, call format() method with the util Date and returns date in string format.

package com.javaprogramto.java8.dates.getdatetime;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatExample {

	public static void main(String[] args) {

		// current date custom format
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		
		// current date and time
		Date currentDate = new Date();
		
		// Util Date to String using format()
		String strDate = dateFormatter.format(currentDate);

		System.out.println("Current Date using SimpleDateFormat : "+strDate);
	}
}

Output:

Current Date using SimpleDateFormat : 2020-12-15 09:20

7. Using Java 8 - java.time.LocalDate


Use LocalDate.now() method to get the current date. This method does not return the time formats and stores the only date values.

Note: If you want to get only date part in java 8 then use LocalDate class.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.LocalDate;

public class LocalDateExample {
	
	public static void main(String[] args) {
		
		// using LocalDate class
		LocalDate localDateCurrentDate = LocalDate.now();
		
		// print the current date
		System.out.println("LocalDate current date : "+localDateCurrentDate);
	}
}

Output:

LocalDate current date : 2020-12-15

8. Using Java 8 - java.time.LocalTime


In java 8, LocalTime class works opposite to the LocalDate class and LocalTime.now() method gives only time part with milli seconds by default.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.LocalTime;

public class LocalTimeExample {

	public static void main(String[] args) {
		
		// Using LocalTime class now() method
		LocalTime localTimeCurrentTime = LocalTime.now();
		
		// using 
		System.out.println("LocalTime : "+localTimeCurrentTime);
	}
}
Output:
LocalTime : 21:28:59.495299

9. Using Java 8 - java.time.LocalDateTime


Another class from java 8 LocalDateTime class which gives both date and time part. Use now() method to get the current date and time.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.LocalDateTime;

public class LocalDateTimeExample {

	public static void main(String[] args) {
		
		// using LocalDateTime class now() method
		LocalDateTime now = LocalDateTime.now();
		
		// printing
		System.out.println("Current date time from LocalDateTime : "+now);
	}
}

Output:
Current date time from LocalDateTime : 2020-12-15T21:33:16.944571

10. Using Java 8 - java.time.ZonedDateTime


Next java 8 class is ZonedDateTime and this works with timezone part along with the date time.

Use now() method to get the current date time from the current timezone.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.ZonedDateTime;

public class ZonedDateTimeExample {

	public static void main(String[] args) {

		// Using ZonedDateTime class now() method
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		
		// print current date time from ZonedDateTime class.
		System.out.println("ZonedDateTime : "+zonedDateTime);
	}
}

Output:
ZonedDateTime : 2020-12-15T21:36:12.881014+05:30[Asia/Kolkata]

11. Using Java 8 - java.time.format.DateTimeFormatter


Use DateTimeFormatter class to get the date in custom format in java 8 api.

Pass the date pattern to DateTimeFormatter.ofPattern() method and format() method to get the LocalDateTime or ZonedDateTime in string format.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterExamples {

	public static void main(String[] args) {
		DateTimeFormatter formatterInJava8 = DateTimeFormatter.ofPattern("yyyy/MMM/dd HH:mm:ss");
		
		LocalDateTime LocalDateTimeCurrentTime = LocalDateTime.now();
		String date1 = formatterInJava8.format(LocalDateTimeCurrentTime);

		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		String date2 = formatterInJava8.format(zonedDateTime);
		
		System.out.println("Date in custom format : ");
		System.out.println("Current date time from LocalDateTime : "+date1);
		System.out.println("Current date time from ZonedDateTime : "+date2);
	}
}

Output:
Date in custom format : 
Current date time from LocalDateTime : 2020/Dec/15 21:41:17
Current date time from ZonedDateTime : 2020/Dec/15 21:41:17

12. Conclusion


In this article, we've seen various ways to get the date and time in java older versions and new java 8 api with examples.



COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: How To Get Current Date and Time in Java 8 and Older?
How To Get Current Date and Time in Java 8 and Older?
A quick guide to get the current date and time in java and also using new java 8 date time api.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT_IskgDt8B-xcK-p2Ix9n_NdTIpqvzBH9MSeq4LHAtk_7JzBwwX5XqcahSYcN4C7-kepAbFtd_88CTFxCpwCAWzJoES1pPUk3ctgEYRsw5VdKPjNoh-BpGyxCLIBFP8ymakyobdzlx3E/w400-h289/How+To+Get+Current+Date+and+Time+in+Java+8+and+Older%253F.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT_IskgDt8B-xcK-p2Ix9n_NdTIpqvzBH9MSeq4LHAtk_7JzBwwX5XqcahSYcN4C7-kepAbFtd_88CTFxCpwCAWzJoES1pPUk3ctgEYRsw5VdKPjNoh-BpGyxCLIBFP8ymakyobdzlx3E/s72-w400-c-h289/How+To+Get+Current+Date+and+Time+in+Java+8+and+Older%253F.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/12/java-get-current-date-time.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/12/java-get-current-date-time.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content