Current Date and Time in Java 8
In this article, We are going to learn how to get current data and time by using java 8 new features with simple examples step-by-step.
Here we will use some new java 8 date time API to display the data and time in java.
Here we are going to use some java 8 date and time classes which are given below.
Here we will use some new java 8 date time API to display the data and time in java.
Here we are going to use some java 8 date and time classes which are given below.
- java.time.LocalDate class
- java.time.LocalTime class
- java.time.LocalDateTime class
We can also use some old classes to get the current data and time in java e.g Date and Calendar classes but here we will focus only above listed 3 classes which is java 8 LocalDate, LocalTime, and LocalDateTime.
Let's start with LocalDate class.
Let's start with LocalDate class.
Java 8 LocalDate Class
Java 8 LocalDate class are defined in java.time.* package and it contains several useful methods but here we are using now() method which is a static method.
LocalDate class represents date in this format year-month-day
Java 8 LocalDate class is a final class i.e it is immutable class and LocalDate class extends Object class and implements several interfaces like Serializable, ChronoLocalDate, Temporal, TemporalAdjuster.
LocalDate Class Example
This is simple LocalDate class example for printing date.
class DateExample
{
public static void main(String args[])
{
//display the current date from the system clock in the default time zone
System.out.println("Current date is : "+java.time.LocalDate.now());
}
}
Output: Current date is : 2017-11-16
Output: Current date is : 2017-11-16
Java 8 LocalTime Class
Java 8 LocalTime class is defined in java.time.* package and it contains several useful methods but here we are going to use LocalTime.now() method which is static method.
LocalTime class represents time in this format hour-minute-second.
Time is represented to nanosecond precision.
Time is represented to nanosecond precision.
Java 8 LocalTime class is also a immutable class i.e it is final class and it extends Object class and implement several interface like Serializable, Comparable<LocalTime>, etc.
LocalTime Class Example
This is simple LocalTime class example for printing time in java.
class TimeExample
{
public static void main(String args[])
{
//display the current time form the system clock in the default time-zone
System.out.println("Current time is : "+java.time.LocalTime.now());
}
}
Output: Current time is : 13:9:59.546
Java 8 LocalDateTime Class
Java 8 LocalDateTime class is also defined in java.time.* package and it also contains some useful method but here we will use LocalDateTime.now() method for representing date and time in java.
LocalDateTime class represents date and time both int this format year-month-day-hour-minute-second.
LocalDateTime class is final class i.e it immutable class and this class extends Object class and implement interfaces like Serializable, Temporal, TemporalAdjuster, etc.
LocalDateTime Class Example
This is simple example LocalDateTime class where will display both date and time together.
class DateTimeExample
{
public static void main(String args[])
{
//display the date and time from the system clock in default time-zone
System.out.println("Current date-time is : "+java.time.LocatDateTime.now());
}
}
Output: Current date-time is : 2017-11-16T13:27:10.087
here we learned how to get current date and time by using some new java 8 API like LocalDate class, LocalTime class and LocalDateTime class. There are other new classes in java 8 date and time api but we will learn in later.
here we learned how to get current date and time by using some new java 8 API like LocalDate class, LocalTime class and LocalDateTime class. There are other new classes in java 8 date and time api but we will learn in later.
Correctiom
ReplyDeleteSystem.out.println("Current date-time is : "+java.time.now());
System.out.println("Current date-time is : "+java.time.LocalDateTime.now());
Thank you for this correction.
Delete