Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, January 1, 2021

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

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.


No comments:

Post a Comment

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