42

JLocalDateTime Class API Guide in Java 8

 5 years ago
source link: https://www.tuicool.com/articles/hit/fANFz2U
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this article, we will learn more about the commonly-used LocalDateTime Class APIs with examples. LocalDateTime represents a combination of date and time.

This is the most commonly used class when we need a combination of date and time. The class offers a variety of APIs, and we will look at some of the most commonly used ones.

The java.time.LocalDateTime class is an immutable class that represents a date-time without time-zone information, such as ‘2018-08-12T10:35:55.'

Below, the class diagram shows a list of APIs that the LocalDateTime class provides. MF3UFjI.png!web

1. LocalDateTime APIs to Get Current Date-Time and Specific Date-Time Object

LocalDateTime   class provides the following APIs to create the current date-time and specific date-time object respectively.

  •   static LocalDateTime now() :  Obtains the current date-time from the system clock in the default time-zone.
  •   static LocalDateTime now(Clock clock) : Obtains the current date-time from the specified clock.
  •   static LocalDateTime now(ZoneId zone) : Obtains the current date-time from the system clock in the specified time-zone.
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */

public class LocalDateTimeExample {

    public static void main(String[] args) {
        createLocalDateTime();
    }
    private static void createLocalDateTime() {
        // Current date time
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);

        // Current date time from specified time-zone
        LocalDateTime dateTime2 = LocalDateTime.now(ZoneId.of("UTC"));
        System.out.println(dateTime2);

        // Current date time from specified clock
        LocalDateTime dateTime3 = LocalDateTime.now(Clock.systemUTC());
        System.out.println(dateTime3);

        // Specific date time
        LocalDateTime dateTime4 = LocalDateTime.of(2017, Month.JULY, 12, 10, 35, 55);
        System.out.println(dateTime4);
    }
}

Output:

2018-08-10T18:08:43.787
2018-08-10T12:38:43.789
2018-08-10T12:38:43.789
2017-07-12T10:35:55

2. LocalDateTime APIs to Get a Year, Month, Day From LocalDateTime

LocalDateTime   class provides the following APIs to get a year, month, day from the   LocalDateTime .

  •   int getYear() : Gets the year field.
  •   Month getMonth() : Gets the month-of-year field using the  Month   enum.
  •   int getDayOfMonth() : Gets the day-of-month field.
  •   DayOfWeek getDayOfWeek() : Gets the day-of-week field, which is the enum  DayOfWeek .
  •   int getDayOfYear() : Gets the day-of-year field.
import java.time.LocalDateTime;

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */

public class LocalDateTimeExample {

    public static void main(String[] args) {
        getYearMonthDayFromLocalDateTime();
    }
    private static void getYearMonthDayFromLocalDateTime() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println("Year : " + dateTime.getYear());
        System.out.println("Month : " + dateTime.getMonth().getValue());
        System.out.println("Day of Month : " + dateTime.getDayOfMonth());
        System.out.println("Day of Week : " + dateTime.getDayOfWeek());
        System.out.println("Day of Year : " + dateTime.getDayOfYear());
    }
}

Output:

Year : 2018
Month : 8
Day of Month : 10
Day of Week : FRIDAY
Day of Year : 222

3. LocalDateTime APIs to Get Hour, Minute, Second From LocalDateTime

LocalDateTime   class provides the following APIs to get Hour, Minute, Second, from   LocalDateTime .

int getHour()
int getMinute()
int getNano()
int getSecond()
import java.time.LocalDateTime;

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */

public class LocalDateTimeExample {

    public static void main(String[] args) {
        getHourMinuteSecondfromLocalDateTime();
    }

    private static void getHourMinuteSecondfromLocalDateTime() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println("Hour : " + dateTime.getHour());
        System.out.println("Minute : " + dateTime.getMinute());
        System.out.println("Second : " + dateTime.getSecond());
        System.out.println("Nano : " + dateTime.getNano());
    }
}

4. LocalDateTime APIs to Add or Subtract Years, Months, Days, Hours, Minutes and Seconds to LocalDateTime

Below example shows how to add or subtract years, months, days, hours, minutes and seconds to LocalDateTime   class.

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */
public class LocalDateTimeExample {

    public static void main(String[] args) {
        addOrSubstractUsingLocalDateTime();
    }

    private static void addOrSubstractUsingLocalDateTime() {
        LocalDateTime dateTime = LocalDateTime.now();
        // LocalDateTime's plus methods
        System.out.println("Addition of days : " + dateTime.plusDays(5));
        System.out.println("Addition of months : " + dateTime.plusMonths(15));
        System.out.println("Addition of years : " + dateTime.plusYears(5));
        System.out.println("Addition of Hours : " + dateTime.plusHours(2));
        System.out.println("Addition of Minutes : " + dateTime.plusMinutes(30));
        System.out.println("Addition of Seconds : " + dateTime.plusSeconds(20));

        // LocalDateTime's minus methods
        System.out.println("Subtraction of days : " + dateTime.minusDays(5));
        System.out.println("Subtraction of months : " + dateTime.minusMonths(15));
        System.out.println("Subtraction of years : " + dateTime.minusYears(5));
        System.out.println("Subtraction of Hours : " + dateTime.minusHours(2));
        System.out.println("Subtraction of Minutes : " + dateTime.minusMinutes(30));
        System.out.println("Subtraction of Seconds : " + dateTime.minusSeconds(20));
    }
}

Output:

Addition of days : 2018-08-15T18:14:13.385
Addition of months : 2019-11-10T18:14:13.385
Addition of years : 2023-08-10T18:14:13.385
Addition of Hours : 2018-08-10T20:14:13.385
Addition of Minutes : 2018-08-10T18:44:13.385
Addition of Seconds : 2018-08-10T18:14:33.385
Subtraction of days : 2018-08-05T18:14:13.385
Subtraction of months : 2017-05-10T18:14:13.385
Subtraction of years : 2013-08-10T18:14:13.385
Subtraction of Hours : 2018-08-10T16:14:13.385
Subtraction of Minutes : 2018-08-10T17:44:13.385
Subtraction of Seconds : 2018-08-10T18:13:53.385

5. LocalDateTime APIs to Compare LocalDateTime Objects in Java

LocalDateTime   class provides below APIs compare  LocalDateTime   objects in Java.

  •   boolean isAfter(ChronoLocalDateTime other) : Checks if this date-time is after the specified date-time.
  •   boolean isBefore(ChronoLocalDateTime other)   : Checks if this date-time is before the specified date-time.
  •   boolean isEqual(ChronoLocalDateTime other) : Checks if this date-time is equal to the specified date-time.
  •   int compareTo(ChronoLocalDateTime other)   : Compares this date-time to another date-time.
import java.time.LocalDateTime;

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */

public class LocalDateTimeExample {

    public static void main(String[] args) {
        compareLocalDateTimeObjects();
    }


    private static void compareLocalDateTimeObjects() {
        LocalDateTime dateTime1 = LocalDateTime.of(2017, 05, 22, 10, 55, 25);
        LocalDateTime dateTime2 = LocalDateTime.of(2017, 06, 11, 05, 35, 26);
        LocalDateTime dateTime3 = LocalDateTime.of(2017, 05, 22, 10, 55, 25);

        // Using isBefore() method
        if (dateTime1.isBefore(dateTime2)) {
            System.out.println("dateTime1 is before dateTime2");
        }

        // Using isAfter() method
        if (dateTime2.isAfter(dateTime3)) {
            System.out.println("dateTime2 is after dateTime3");
        }

        // Using isEqual() method
        if (dateTime1.isEqual(dateTime3)) {
            System.out.println("dateTime1 is equal to dateTime3");
        }

        // Using compareTo() method
        if (dateTime1.compareTo(dateTime3) == 0) {
            System.out.println("dateTime1 is equal to dateTime3");
        }
    }
}

Output:

dateTime1 is before dateTime2
dateTime2 is after dateTime3
dateTime1 is equal to dateTime3
dateTime1 is equal to dateTime3

6. LocalDateTime API to Convert From LocalDateTime to LocalDate in Java

LocalDateTime   class provides the following APIs to convert from  LocalDateTime to   LocalDate in Java.

  •   LocalDate toLocalDate()   : Gets the  LocalDate   part of this date-time.
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */
public class LocalDateTimeExample {

    public static void main(String[] args) {
        convertLocalDateTimeToLocalDate();
    }

    private static void convertLocalDateTimeToLocalDate() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);

        LocalDate localDate = dateTime.toLocalDate();
        System.out.println(localDate);
    }
}

Output:

2018-08-10T18:16:35.802
2018-08-10

Learn more about the LocalDate class APIs on this Java 8 - LocalDate Class API Guide .

7. LocalDateTime APIs to Convert From LocalDateTime to LocalTime in Java

LocalDateTime   class provides the following APIs to convert from LocalDateTime to  LocalTime in Java.

  •   LocalTime toLocalTime() : Gets the  LocalTime   part of this date-time.
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */

public class LocalDateTimeExample {

    public static void main(String[] args) {
        convertLocalDateTimeToLocalTime();
    }

    private static void convertLocalDateTimeToLocalTime() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);

        LocalTime localDate = dateTime.toLocalTime();
        System.out.println(localDate);
    }
}

Output:

2018-08-10T18:17:08.598
18:17:08.598

Learn more about the LocalTime   class APIs on Java 8 - LocalTime Class API Guide

Related Java 8 Date and Time API Guides


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK