Monday, November 18, 2024
Google search engine
HomeLanguagesJavajava.time.LocalDate Class in Java

java.time.LocalDate Class in Java

Java is the most popular programming language and widely used programming language. Java is used in all kind of application like as mobile application, desktop application, web application. In this Java java.time.LocalDate class is imported which represents to display the current date.

java.time: It is the package used to work with the current date and time API.

Class:

Class Description
LocalDate   It is a class that represents a date. 
DateTimeFormatter   It is a class used to format and parse date and time.

Methods:

Methods

Description

adjustInto(Temporal temporal) This method adjusts the specified temporal object to having the same date as this object.
 atStartOfDay() This method combines this date with the time of midnight to create a LocalDateTime at the start of this date.
atStartOfDay(ZoneId zone) This method returns a zoned date-time from this date at the earliest valid time according to the rules in the time-zone.
 atTime(int hour, int minute) This method combines this date with a time to create a LocalDateTime.
 atTime(int hour, int minute, int second) This method combines this date with a time to create a LocalDateTime.
 compareTo(ChronoLocalDate other) This method compares this date to another date.
equals(Object obj) This method checks if this date is equal to another date.
format(DateTimeFormatter formatter) This method formats this date using the specified formatter.
from(TemporalAccessor temporal) This method obtains an instance of LocalDate from a temporal object.
 get(TemporalField field) This method gets the value of the specified field from this date as an int.
 getChronology() This method gets the chronology of this date, which is the ISO calendar system.
getDayOfMonth() This method gets the day-of-month field.
 getDayOfWeek() This method gets the day-of-week field, which is an enum DayOfWeek.
getDayOfYear() This method gets the day-of-year field.
 getEra() This method gets the era applicable at this date.
getLong(TemporalField field) This method gets the value of the specified field from this date as a long.
getMonth() This method gets the month-of-year field using the Month enum.
getMonthValue() This method gets the month-of-year field from 1 to 12.
 getYear() This method gets the year field.
hashCode() A hash code for this date.
isAfter(ChronoLocalDate other) This method checks if this date is after the specified date.
isBefore(ChronoLocalDate other) This method checks if this date is before the specified date.
isEqual(ChronoLocalDate other) This method checks if this date is equal to the specified date.
isLeapYear() This method checks if the year is a leap year, according to the ISO proleptic calendar system rules.
isSupported(TemporalField field) This method checks if the specified field is supported.
lengthOfMonth() This method returns the length of the month represented by this date.
lengthOfYear() This method returns the length of the year represented by this date.
now()  It is a method used to return the instance of LocalDateTime class.
ofPattern() It is a method used with DateTimeFormatter to format and parse date and time. It accepts all types or sorts of value, to display in a different format.
query(TemporalQuery<R> query) This method queries this date using the specified query.
range(TemporalField field) This method gets the range of valid values for the specified field.
 toEpochDay() This method converts this date to Epoch Day.
withDayOfMonth(int dayOfMonth) This method returns a copy of this LocalDate with the day-of-month altered.
withDayOfYear(int dayOfYear) This method returns a copy of this LocalDate with the day-of-year altered.
 withMonth(int month) This method returns a copy of this LocalDate with the month-of-year altered.
 withYear(int year) This method returns a copy of this LocalDate with the year altered.

Approach

  1. Import classes such as java.time.LocalDateTime and java.time.format.DateTimeFormatter with java.time package.
  2. Use LocalDateTime.now() and now() method.
  3. Use DateTimeFormatter.ofPattern to sort the current date.
  4. Display the date which is stored in the variable.

Below is the implementation of the problem statement:

Java




// import package used to work with current date and time
// api.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // now() is a method to return the
        // instance of LocalDateTime class.
        LocalDateTime localDate = LocalDateTime.now();
        // DateTimeFormatter class used to format and
        // parse date and time. ofPattern() is a method
        // used with DateTimeFormatter to format and
        // parse date and time.
        DateTimeFormatter dateformatter
            = DateTimeFormatter.ofPattern("MM dd, YYYY");
        // display the date
        System.out.println(dateformatter.format(localDate));
    }
}


Output

03 16, 2021

 
 Java 

Java




import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // Parses the date
        LocalDate dt1 = LocalDate.parse("2021-01-07");
        LocalDate result = dt1.withDayOfYear(01);
 
        // Prints the date with year
        System.out.println("The date with day of year is: "
                           + result);
    }
}


Output

The date with day of year is: 2021-01-01
RELATED ARTICLES

Most Popular

Recent Comments