$show=/label

How To Get Current Date, Time and Timestamp In Java 8?

SHARE:

A quick guide on how to get the current date, time and Timestamp objects in java 8 api.

1. Overview

In this article, we will learn how to get current date, time and timestamp in java 8. 

Let us explore the java 8 new classes LocalDateTime, LocalDate, LocalTime and Instant to work with date and time values of Date Time api.

How To Get Current Date, Time and Timestamp In Java 8?



2. How to get current date in java 8


Use java.time.LocalDate class to get the current date in format of "yyyy-MM-dd" using now() method.

And also we can get the current date from any timezone using now(ZoneId.of()).

Finally, let us convert LocalDateTime to LocalDate object using toLocalDate() method.

In the below example. we have shown the different ways to retrieve the date object in java 8.
package com.javaprogramto.java8.dates.currentdate;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class CurrentDate {

	public static void main(String[] args) {
		
		// getting the current date from LocalDate.now() method
		LocalDate currentDate = LocalDate.now();
		
		System.out.println("Current Date from LocalDate : "+currentDate);
		
		// Getting the current daet in Gmt +5
		LocalDate gmtPlus5 = LocalDate.now(ZoneId.of("GMT+05"));
		
		System.out.println("Current time in GMT +05:00 : "+gmtPlus5);
		
		// Gettting the date from LocalDateTime object.
		LocalDateTime localDateTime = LocalDateTime.now();
		LocalDate fromLocalDateTime = localDateTime.toLocalDate();
		System.out.println("From LocalDateTime : "+fromLocalDateTime);
	}
}

Output:
Current Date from LocalDate : 2021-01-01
Current time in GMT +05:00 : 2021-01-01
From LocalDateTime : 2021-01-01

In the output, we could see the same date because timezone date is falling the current date even adding +5 hours. If we are at the end of the day then you can see the different date.

3. How to get current time in java 8


To retrieve the only time part, we can use java.time.LocalTime class. LocalTime.now() method gets the time parts from system date in format hh:mm:ss.sssss.

Use now(ZoneId.of()) method returns the time in the given timezone.

And also we can get the time from LocalDateTime object using toLocalTime() method.

package com.javaprogramto.java8.dates.currentdate;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;

public class CurrentTime {

	public static void main(String[] args) {
		
		// getting the current date from LocalTime.now() method
		LocalTime currentDate = LocalTime.now();
		
		System.out.println("Current time from LocalTime in IST (+05:30): "+currentDate);
		
		// Getting the current daet in Gmt +5
		LocalTime gmtPlus5 = LocalTime.now(ZoneId.of("GMT+06:30"));
		
		System.out.println("Current time in GMT +05:00 : "+gmtPlus5);
		
		// Gettting the date from LocalTimeTime object.
		LocalDateTime LocalTimeTime = LocalDateTime.now();
		LocalTime fromLocalTimeTime = LocalTimeTime.toLocalTime();
		System.out.println("From LocalDateTime : "+fromLocalTimeTime);
	}
}

Output:
Current time from LocalTime in IST (+05:30): 17:52:08.567623
Current time in GMT +05:00 : 18:52:08.568073
From LocalDateTime : 17:52:08.568161

4. How to get current timestamp in java 8


By using java.time.Instant class to get the current timestamp in milli seconds and seconds.

Use Insant.toEpochMilli() and Instant.getEpochSecond() methods to get the current time in milliseconds and seconds respectively.

package com.javaprogramto.java8.dates.currentdate;

import java.time.Instant;

public class CurrentTimeStamp {

	public static void main(String[] args) {
		
		// getting the current timestamp from Instant.now() method
		Instant currentInstant = Instant.now();
		
		// getting the current time in milliseoconds from Instant
		long timeInMillis = currentInstant.toEpochMilli();
		
		System.out.println("Current timestamp in milli seconds "+timeInMillis);

		// Getting the current instant in seconds
		long timeInSeconds = currentInstant.getEpochSecond();
		
		System.out.println("Current timestamp in seconds : "+timeInSeconds);
	}
}

Output:
Current timestamp in milli seconds 1609504761862
Current timestamp in seconds : 1609504761

5. Conclusion


In this article, we've seen how to get the current date, time and timestamp objects using java 8 api.


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,16,Arrays,16,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,25,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,90,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,35,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,10,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,100,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,103,java.lang,5,java.util. function,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,1,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,2,Math,1,Matrix,5,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,27,String,58,String Programs,12,String Revese,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: How To Get Current Date, Time and Timestamp In Java 8?
How To Get Current Date, Time and Timestamp In Java 8?
A quick guide on how to get the current date, time and Timestamp objects in java 8 api.
https://1.bp.blogspot.com/-EOgM7-LHiZs/X-8aUoK9m5I/AAAAAAAADMA/GpCuEBPsLeU2FSmnV4-mCPWLj_s8q8VyQCLcBGAsYHQ/w640-h430/How%2BTo%2BGet%2BCurrent%2BDate%252C%2BTime%2Band%2BTimestamp%2BIn%2BJava%2B8%253F.png
https://1.bp.blogspot.com/-EOgM7-LHiZs/X-8aUoK9m5I/AAAAAAAADMA/GpCuEBPsLeU2FSmnV4-mCPWLj_s8q8VyQCLcBGAsYHQ/s72-w640-c-h430/How%2BTo%2BGet%2BCurrent%2BDate%252C%2BTime%2Band%2BTimestamp%2BIn%2BJava%2B8%253F.png
JavaProgramTo.com
https://www.javaprogramto.com/2021/01/java-8-get-current-date-time-timestamp.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/01/java-8-get-current-date-time-timestamp.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