×

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.


Related Topics

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

2 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Java Single Linkedlist

Like arrays, linked lists are a type of linear data structure. Unlike arrays, which store each element in the same location, linked lists employ pointers to connect the elements together. In...

25 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.