×

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

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.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Encapsulation Program in Java

Encapsulation Program in Java Encapsulation program in Java demonstrates the technique to bind methods and fields in a single unit. The term encapsulation is inspired by the word ‘capsule’, which is...

3 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Java Sort String

In this article, you will be acknowledged about how to sort a string in Java. Introduction Firstly, let us revise what is string Strings are collections of characters that are frequently used in...

4 minutes read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

Java Variable Declaration

In this article, you will be acknowledged about java variable declaration. You will be able to learn and interpret about declaring a variable in Java. Variable in Java Variables are necessary for...

6 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.