Java time local date
Java:
Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc.
The main feature of java is we can say it as platform independence. It means that we can execute the code in a system which is compiled in an other system. It is a flexible feature of java.
Java consists of different kind of data types such as primitive data types such as int, byte, short, long, float, bool, char, String etc. and also non – primitive data types. It also consists of different methods, variables and also operators.
In the java programming language we have many built in packages. One such built in packages is java.time package. In this package we have a class called Localdate class. It is stored in the time package as “ java.time.LocalDate “ class.
Java . time .LocalDate class:
The time package is mainly used to work with the specific date and also time in an Application Programming Interface ( API ).
We can say that it is an immutable class. It is mainly used to represent the specific date in a default format of year – month – date that is “ yyyy – mm – dd “ . In this class we also use the concept of inheritance that means that it is used to inherit the object class.
Not only for inheritance concept we can also use this class for the concept of interface. In the interfaces concept it is used to implement the Chrono Local Date interface.
Syntax of java . time . LocalDate class :
Inorder to define a particular class or package in java we need to know the syntax for defining a certain package or class and the syntax to define “ java . time. LocalDate “ class is as shown below :
public final class LocalDate extends implements ChronoLocalDate
So we can declare the java.time.LocalDate class as shown above.
There are many methods that are involved in the LocalDate class. These methods are built in methods. Some of the methods are as shown below :
- get Day Of Year ( )
- Month getMonth ( )
- get Month Value ( )
- getYear ( )
- length Of Year ( )
- length Of Year ( )
- Localdtae OfYear ( )
- LocalDate now ( )
- LocalDate minusDays ( )
- Boolean is LeapYear ( )
- compareTo ( )
- Boolean equals ( )
There are many such methods that are in built or primitive by default itself.
Now let us consider few examples with the following code to implement the method “ java.time.LocalDate “ as shown below :
Example 1:
import java.io.*;
import java.util.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Demo
{
public static void main ( String args [ ] )
{
LocalDate date1 = LocalDate.now ( );
String str1 = date1.format ( DateTimeFormatter . ISO_DATE );
System.out.println ( “ First date in string : “ + str1 );
LocalDate date2 = LocalDate.of ( 2009 , 06 , 15 );
String str2 = date2.format ( DateTimeFormatter . ISO_DATE );
System.out.println ( “ Second date in string : “ + str2 );
LocalDate date3 = LocalDate.of ( 2015 , 10 , 18 );
String str3 = date1.format ( DateTimeFormatter . ISO_DATE );
System.out.println ( “Third date in string : “ + str3 );
}
}
Output 1:
First date in string : 2022 – 08 – 31
Second date in string : 2009 – 06 – 15
Third date in string : 2015 – 10 – 18
Example 2:
import java.io.*;
import java.util.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Demo1
{
public static void main ( String args [ ] )
{
String str1 = “ 2019 – 11 – 15 “;
LocalDate date1 = LocalDate.parse ( str1 );
System.out.println ( “ First date in string : “ + date1 );
String str2 = “ 2020 – 08 – 09 “;
LocalDate date2 = LocalDate.parse ( str2 );
System.out.println ( “ Second date in string : “ + date2 );
} // main method
} // demo class
Output 2:
First date in string : 2019 – 11 – 15
Second date in string : 2020 – 08 – 09
We can also use the LocalDate method in many ways not only in the above shown ways.