All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.imsejin.common.util.DateTimeUtils Maven / Gradle / Ivy

package io.github.imsejin.common.util;

import io.github.imsejin.common.constant.DateType;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;

import static java.time.format.DateTimeFormatter.ofPattern;

/**
 * Datetime utilities
 */
public final class DateTimeUtils {

    private DateTimeUtils() {
    }

    /**
     * Checks if it's leap year.
     *
     * 
{@code
     *     isLeapYear(2019); // false
     *     isLeapYear(2020); // true
     * }
* * @param year year you want to check * @return the year is leap year or not */ public static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } /** * Gets today's date formatted as 'yyyyMMdd'. * *
{@code
     *     today(); // 20191231
     * }
* * @return today's date */ public static String today() { return LocalDate.now().format(ofPattern(DateType.DATE.value())); } /** * Gets today's date formatted with pattern. * *
{@code
     *     today();                 // 20191231
     *
     *     today(DateType.YEAR);    // 2019
     *     today(DateType.MONTH);   // 12
     *     today(DateType.DAY);     // 31
     *     today(DateType.HOUR);    // 17
     *     today(DateType.MINUTE);  // 59
     *     today(DateType.SECOND);  // 59
     *     today(DateType.F_ALL);   // 2019-12-31 17:59:59.723
     * }
* * @param type type of the date * @return today's datetime formatted with pattern */ public static String today(DateType type) { return LocalDateTime.now().format(ofPattern(type.value())); } /** * Gets yesterday's date formatted as 'yyyyMMdd'. * *
{@code
     *     today();     // 20191231
     *     yesterday(); // 20191230
     * }
* * @return yesterday's date */ public static String yesterday() { return LocalDate.now().minusDays(1).format(ofPattern(DateType.DATE.value())); } /** * Gets the corresponding element of yesterday's date formatted as 'yyyyMMdd'. * *
{@code
     *     yesterday();                 // 20191230
     *
     *     yesterday(DateType.YEAR);    // 2019
     *     yesterday(DateType.MONTH);   // 12
     *     yesterday(DateType.DAY);     // 30
     * }
* * @param type type of the date * @return yesterday's datetime formatted with pattern */ public static String yesterday(DateType type) { return LocalDateTime.now().minusDays(1).format(ofPattern(type.value())); } /** * Gets the current datetime formatted as 'yyyyMMddHHmmss'. * *
{@code
     *     now(); // 20191231175959
     * }
* * @return corresponding element of yesterday's datetime */ public static String now() { return LocalDateTime.now().format(ofPattern(DateType.DATE_TIME.value())); } /** * Check if the date is actual. *
* (Support date formats for "yyyy-MMdd", "yyyy-MM-dd") * *
{@code
     *     validate("2019-02-28");  // true
     *     validate("20190229");    // false
     *     validate("20200229");    // true
     *     validate("2020-02-29");  // true
     * }
* * @param date date * @return whether the date is valid */ public static boolean validate(String date) { try { SimpleDateFormat dateFormat = new SimpleDateFormat(DateType.DATE.value()); dateFormat.setLenient(false); dateFormat.parse(date.replace("-", "")); return true; } catch (ParseException ex) { return false; } } /** * Check if the date and day of the week are actual. *
* (Support date formats for "yyyy-MMdd", "yyyy-MM-dd") * *
{@code
     *     validate("20190228", DayOfWeek.THURSDAY);    // true
     *     validate("2019-02-28", DayOfWeek.THURSDAY);  // true
     *     validate("20190229", DayOfWeek.FRIDAY);      // false
     *     validate("20200229", DayOfWeek.SATURDAY);    // true
     *     validate("2020-02-29", DayOfWeek.SATURDAY);  // true
     * }
* * @param date date * @param dayOfWeek day of week * @return whether the date is valid */ public static boolean validate(String date, DayOfWeek dayOfWeek) { // 유효한 날짜인지 확인한다 if (!validate(date)) return false; date = date.replace("-", ""); LocalDate localDate = LocalDate.parse(date, ofPattern(DateType.DATE.value())); // 유효한 요일인지 확인한다 return localDate.getDayOfWeek().equals(dayOfWeek); } /** * Gets the last date of the year and month. * *
{@code
     *     withMonthlyLastDate(2019, 2); // 20190228
     *     withMonthlyLastDate(2020, 2); // 20200229
     * }
* * @param year year * @param month month * @return last date of the year and month */ public static String getLastDateOfMonth(int year, int month) { LocalDate lastDate = YearMonth.of(year, month).atEndOfMonth(); return lastDate.format(ofPattern(DateType.DATE.value())); } /** * Gets the last date of the year and month. * *
{@code
     *     withMonthlyLastDate(2019, Month.FEBRUARY); // 20190228
     *     withMonthlyLastDate(2020, Month.FEBRUARY); // 20200229
     * }
* * @param year year * @param month month * @return last date of the year and month */ public static String getLastDateOfMonth(int year, Month month) { LocalDate lastDate = YearMonth.of(year, month).atEndOfMonth(); return lastDate.format(ofPattern(DateType.DATE.value())); } /** * Gets the last date of the year and month. * *
{@code
     *     withMonthlyLastDate("2019", "2"); // 20190228
     *     withMonthlyLastDate("2020", "2"); // 20200229
     * }
* * @param year year * @param month month * @return last date of the year and month */ public static String getLastDateOfMonth(String year, String month) { LocalDate lastDate = YearMonth.of(Integer.parseInt(year), Integer.parseInt(month)).atEndOfMonth(); return lastDate.format(ofPattern(DateType.DATE.value())); } /** * Gets the last date of the year and month. * *
{@code
     *     withMonthlyLastDate("2019", Month.FEBRUARY); // 20190228
     *     withMonthlyLastDate("2020", Month.FEBRUARY); // 20200229
     * }
* * @param year year * @param month month * @return last date of the year and month */ public static String getLastDateOfMonth(String year, Month month) { LocalDate lastDate = YearMonth.of(Integer.parseInt(year), month).atEndOfMonth(); return lastDate.format(ofPattern(DateType.DATE.value())); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy