JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to get current date and time Java 8?


In this page we have given examples on how to get current date and time using Java 8 new date and time API.

We are using LocalDate, LocalTime, LocalDateTime and ZonedDateTime classes to get the current time.

CurrentDateTimeEx
package com.java2novice.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;

public class CurrentDateTimeEx {

	public static void main(String a[]) {

		// get current date without time
		LocalDate ldObj = LocalDate.now();
		System.out.println("Current date without time: "+ldObj);

		// get current time without date
		LocalTime ltObj = LocalTime.now();
		System.out.println("Current time without date: "+ltObj);

		// get current date and time together without timezone information
		LocalDateTime ldtObj = LocalDateTime.now();
		System.out.println("Current date & time without zone: "+ldtObj);

		// get current date and time together along with timezone information
		ZonedDateTime zdtObj = ZonedDateTime.now();
		System.out.println("Current date & time with zone: "+zdtObj);
	}
}

Output:
Current date without time: 2015-12-16
Current time without date: 08:30:05.519
Current date & time without zone: 2015-12-16T08:30:05.519
Current date & time with zone: 2015-12-16T08:30:05.519+05:30[Asia/Kolkata]
<< Previous Program | Next Program >>

Java-8 Date and Time API Examples

  1. Java 8 LocalDateTime example.
  2. Java 8 ZonedDateTime example.
  3. Java 8 LocalDate example.
  4. Java 8 LocalTime example.
  5. Create date time with custom values in Java 8.
  6. Java 8 Instant (Timestamp) example.
  7. Java 8 Period example.
  8. Java 8 Duration example.
  9. Java 8 Date parsing and formatting example.
  10. How to compare two dates in Java 8?
  11. How to get current date and time Java 8?
  12. How to calculate execution / elapsed time Java 8?
  13. How to convert string to Date Java 8?
  14. How to add/substract days to Date in Java 8?
Knowledge Centre
What is daemon thread?
Daemon thread is a low priority thread. It runs intermittently in the back ground, and takes care of the garbage collection operation for the java runtime system. By calling setDaemon() method is used to create a daemon thread.
Famous Quotations
We know what we are, but know not what we may be.
-- William Shakespeare

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.