Date and time acquisition method using DateAndTime API

Overview

Implement the following pattern in Java and Kotlin (almost the same) with Date and Time API.

--Change date and time --Original format --ISO format --First date and time of the month --End of the month

Implementation example

Click here for Java's Utils class.

LocalDateUtils.java


import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class LocalDateTimeUtils {
    private static final DateTimeFormatter YYYYMM_FORMAT = DateTimeFormatter.ofPattern("yyyyMM");

    //Change date and time
    public static LocalDateTime getDateTimeAfterOneYear(LocalDateTime dateTime) {
        return dateTime.plusYears(1);
    }

    //Original format
    public static String getYearAndMonth(LocalDateTime dateTime) {
        return dateTime.format(YYYYMM_FORMAT);
    }

    //ISO format example)2011-12-03
    public static String getIsoLocalDate(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
    }

    //First date and time of the month
    public static LocalDateTime getFirstDayDateTime(LocalDateTime dateTime) {
        return LocalDateTime.of(dateTime.with(TemporalAdjusters.firstDayOfMonth()).toLocalDate(), LocalTime.MIN);
    }

    //End of the month
    public static LocalDateTime getLastDayDateTime(LocalDateTime dateTime) {
        return LocalDateTime.of(dateTime.with(TemporalAdjusters.lastDayOfMonth()).toLocalDate(), LocalTime.MAX);
    }
}

Click here for Kotlin extension classes.

LocalDateTime.kt


import java.time.LocalDateTime
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters

private val YYYYMM_FORMAT = DateTimeFormatter.ofPattern("yyyyMM")

//Change date and time
fun LocalDateTime.getDateTimeAfterOneYear(): LocalDateTime = plusYears(1)

//Original format
fun LocalDateTime.getYearAndMonth(): String = format(YYYYMM_FORMAT)

//ISO format example)2011-12-03
fun LocalDateTime.getIsoLocalDate(): String = format(DateTimeFormatter.ISO_LOCAL_DATE)

//First date and time of the month
fun LocalDateTime.getFirstDayDateTime(): LocalDateTime = LocalDateTime.of(with(TemporalAdjusters.firstDayOfMonth()).toLocalDate(), LocalTime.MIN)

//End of the month
fun LocalDateTime.getLastDayDateTime(): LocalDateTime =  LocalDateTime.of(with(TemporalAdjusters.lastDayOfMonth()).toLocalDate(), LocalTime.MAX)

Recommended Posts

Date and time acquisition method using DateAndTime API
Date and time
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Handle Java 8 date and time API with Thymeleaf with Spring Boot
[Rails] Search method when date and time have each column
[MySQL] [java] Receive date and time
The design concept of Java's Date and Time API is interesting
Explanation of Ruby Time and Date objects
Java 8 to start now ~ Date time API ~
Try to make a timeline report of method execution time using JFR API
Create API using Retrofit2, Okhttp3 and Gson (Java)
Summary of Japan time setting and display method
variable and method
Date () and Calendar ()
Change date and time to Japanese notation in Rails
[Rails] Precautions when comparing date and time with DateTime
Usability of date and time classes in each language
Handling of date and time in Ruby. Use Date and Time properly.