io.manasobi.utils.DateUtils.java Source code

Java tutorial

Introduction

Here is the source code for io.manasobi.utils.DateUtils.java

Source

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.manasobi.utils;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * Created by tw.jang on 16. 2. 1.
 */
public final class DateUtils {

    private DateUtils() {
    }

    public static final int HOURS_24 = 24;

    public static final int MINUTES_60 = 60;

    public static final int SECONDS_60 = 60;

    public static final int MILLI_SECONDS_1000 = 1000;

    private static final int UNIT_HEX = 16;

    /** Date pattern */
    public static final String DATE_PATTERN_DASH = "yyyy-MM-dd";

    /** Time pattern */
    public static final String TIME_PATTERN = "HH:mm";

    /** Date Time pattern */
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /** Date Time pattern */
    public static final String KOR_DATE_TIME_PATTERN = "yyyyMMdd? HHmmss";

    /** Date Time pattern */
    public static final String KOR_DATE_PATTERN = "yyyyMMdd?";

    /** Date HMS pattern */
    public static final String DATE_HMS_PATTERN = "yyyyMMddHHmmss";

    /** Time stamp pattern */
    public static final String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

    /** year pattern (yyyy)*/
    public static final String YEAR_PATTERN = "yyyy";

    /** month pattern (MM) */
    public static final String MONTH_PATTERN = "MM";

    /** day pattern (dd) */
    public static final String DAY_PATTERN = "dd";

    /** date pattern (yyyyMMdd) */
    public static final String DATE_PATTERN = "yyyyMMdd";

    /** hour, minute, second pattern (HHmmss) */
    public static final String TIME_HMS_PATTERN = "HHmmss";

    /** hour, minute, second pattern (HH:mm:ss) */
    public static final String TIME_HMS_PATTERN_COLONE = "HH:mm:ss";

    /**
     *  , ?  ?  .<br>
     *
     * @return (yyyy-MM-dd HH:mm:ss) ? ?   
     */
    public static String getNow() {
        return getNow(DATE_TIME_PATTERN);
    }

    /**
     *  , ? ? , pattern ? ? ?? .<br><br>
     *
     * DateUtils.getNow("yyyy MM dd? hh mm ss") = "2012 04 12? 20 41 50"<br>
     * DateUtils.getNow("yyyy-MM-dd HH:mm:ss") = "2012-04-12 20:41:50"
     *
     * @param pattern  ? ?  ?
     * @return patter ?  ?   
     */
    public static String getNow(String pattern) {
        LocalDateTime dateTime = LocalDateTime.now();
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
        return dateTime.format(formatter);
    }

    /**
     * ? ?  ?? ?? .<br>
     * -startDate endDate patter? ???  ? .<br><br>
     *
     * DateUtils.getDays("2010-11-24", "2010-12-30", "yyyy-MM-dd") = 36
     *
     * @param startDate ?
     * @param endDate ?
     * @param pattern  ?
     * @return ?  ?? ??
     */
    public static int getDays(String startDate, String endDate, String pattern) {

        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);

        LocalDate localStartDate = LocalDate.parse(startDate, formatter);
        LocalDate localEndDate = LocalDate.parse(endDate, formatter);

        return (int) ChronoUnit.DAYS.between(localStartDate, localEndDate);
    }

    /**
     * ? ?  ?? ?? .<br><br>
     *
     * DateUtils.getDays("2010-11-24", "2010-12-30") = 36
     *
     * @param startDate (yyyy-MM-dd) ? ? ?
     * @param endDate (yyyy-MM-dd) ? ? ?
     * @return ?  ?? ??
     */
    public static int getDays(String startDate, String endDate) {
        return getDays(startDate, endDate, DATE_PATTERN_DASH);
    }

    public static int getDays(LocalDate startDate, LocalDate endDate) {
        return (int) ChronoUnit.DAYS.between(startDate, endDate);
    }

    public static int getDays(LocalDateTime startDateTime, LocalDateTime endDateTime) {
        return (int) ChronoUnit.DAYS.between(startDateTime, endDateTime);
    }

    /**
     * ? ? ?? ?  ?.<br><br>
     *
     * DateUtils.equals(new LocalDate(1292252400000l), new LocalDate(1292252400000l)) = true
     *
     * @param date1 LocalDate? ??
     * @param date2 LocalDate? ??
     * @return ? ? ??  true  false .
     */
    public static boolean equals(LocalDate date1, LocalDate date2) {
        return date1.isEqual(date2);
    }

    /**
     * ? ? ?? ?  ?.<br><br>
     *
     * DateUtils.equals(new Date(1292252400000l), "2010-12-14") = true
     *
     * @param date LocalDate? ??
     * @param dateStr (yyyy-MM-dd) ?? ??
     * @return ? true  false 
     */
    public static boolean equals(LocalDate date, String dateStr) {
        return equals(date, dateStr, DATE_PATTERN_DASH);
    }

    /**
     * ? ? ?? ?  ?.<br><br>
     *
     * DateUtils.equals(new LocalDate(1292252400000l), "2010/12/14", "yyyy/MM/dd") = true
     *
     * @param date Date? ??
     * @param dateStr ?? ??
     * @param pattern  ?
     * @return ? ? ??  true  false .
     */
    public static boolean equals(LocalDate date, String dateStr, String pattern) {

        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
        LocalDate parsedDate = LocalDate.parse(dateStr, formatter);

        return equals(date, parsedDate);
    }

    /**
     * ? ??  ? ?  .
     * 
     * @param  inputDate (yyyy-MM-dd) ?? ??
     * @return ? true   false 
     */
    public static boolean isLeapYear(String inputDate) {
        return isLeapYear(Integer.parseInt(inputDate.substring(0, 4)));
    }

    /**
     *  ? ?  ? ?  .
     * 
     * @param year ?
     * @return year? ? true   false 
     */
    public static boolean isLeapYear(int year) {
        return LocalDate.of(year, 1, 1).isLeapYear();
    }

    public static LocalDateTime convertToDateTime(String dateTime, String pattern) {

        if (StringUtils.isEmpty(dateTime)) {
            return null;
        }

        if (dateTime.length() == 8) {
            return DateUtils.convertToDate(dateTime, "yyyyMMdd").atTime(0, 0, 0);
        }

        if (dateTime.length() == 10) {
            return DateUtils.convertToDate(dateTime).atTime(0, 0, 0);
        }

        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(dateTime, formatter);
    }

    public static LocalDateTime convertToDateTime(String dateTime) {
        return convertToDateTime(dateTime, DATE_TIME_PATTERN);
    }

    public static LocalDateTime convertToDateTime(long timeMillis) {
        Date date = new Date(timeMillis);
        return date.toInstant().atZone(ZoneId.of("Asia/Seoul")).toLocalDateTime();
    }

    public static Timestamp convertToTimestamp(String dateTime, String pattern) {
        return Timestamp.valueOf(convertToDateTime(dateTime, pattern));
    }

    public static Timestamp convertToTimestamp(String dateTime) {
        return Timestamp.valueOf(convertToDateTime(dateTime, DATE_TIME_PATTERN));
    }

    public static Timestamp convertToTimestamp(long timeMillis) {
        return Timestamp.valueOf(convertToDateTime(timeMillis));
    }

    public static LocalDate convertToDate(String date, String pattern) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(date, formatter);
    }

    public static LocalDate convertToDate(String date) {
        return convertToDate(date, DATE_PATTERN_DASH);
    }

    public static String convertToString(LocalDateTime dateTime, String pattern) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
        return dateTime.format(formatter);
    }

    public static String convertToString(LocalDateTime dateTime) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern(DATE_TIME_PATTERN);
        return dateTime.format(formatter);
    }

    public static String convertToString(LocalDate date, String pattern) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
        return date.format(formatter);
    }

    public static String convertToString(LocalDate date) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern(DATE_PATTERN_DASH);
        return date.format(formatter);
    }

    public static String convertToString(LocalTime time, String pattern) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
        return time.format(formatter);
    }

    public static String convertToString(LocalTime time) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern(TIME_HMS_PATTERN_COLONE);
        return time.format(formatter);
    }

    public static String convertToStrKorDateTime(LocalDateTime dateTime) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern(KOR_DATE_TIME_PATTERN);
        return dateTime.format(formatter);
    }

    public static String convertToStrKorDateTime(String dateTimeStr) {
        return convertToStrKorDateTime(dateTimeStr, DATE_TIME_PATTERN);
    }

    public static String convertToStrKorDateTime(String dateTimeStr, String pattern) {
        LocalDateTime dateTime = convertToDateTime(dateTimeStr, pattern);
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern(KOR_DATE_TIME_PATTERN);
        return dateTime.format(formatter);
    }

    public static String convertToStrKorDate(LocalDateTime dateTime) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern(KOR_DATE_PATTERN);
        return dateTime.format(formatter);
    }

    public static String convertToStrKorDate(String dateStr) {
        return convertToStrKorDate(dateStr, DATE_PATTERN_DASH);
    }

    public static String convertToStrKorDate(String dateStr, String pattern) {
        LocalDateTime dateTime = convertToDateTime(dateStr, pattern);
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern(KOR_DATE_PATTERN);
        return dateTime.format(formatter);
    }

    public static long convertToTimeMillis(LocalDateTime dateTime) {
        ZonedDateTime zdt = dateTime.atZone(ZoneId.of("Asia/Seoul"));
        return zdt.toInstant().toEpochMilli();
    }

}