io.uengine.util.DateUtils.java Source code

Java tutorial

Introduction

Here is the source code for io.uengine.util.DateUtils.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.uengine.util;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Date Utility.
 *
 * @author Byoung Gon, Kim
 * @since 0.1
 */
public class DateUtils {

    /**
     *  ? ? ? ?  Enumeration ?
     */
    public static enum TYPE {
        BEFORE, AFTER
    }

    /**
     *    ?
     */
    public static String[] DATE_FORMAT = { "yyyyMMdd", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss",
            "yyyyMMdd" };

    /**
     * ? ?
     */
    public static int DAYS_OF_MONTH[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    /**
     * ?  ? ? ?. ?  ? .
     *
     * @param date1 ?  
     * @param date2 ? ? 
     * @return ?  <tt>true</tt>
     * @throws IllegalArgumentException  <tt>null? </tt>
     */
    public static boolean isSameDay(Date date1, Date date2) {
        return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2);
    }

    /**
     * ?  ? ? ?. ?  ? .
     *
     * @param cal1 ?  
     * @param cal2 ? ? 
     * @return ?  <tt>true</tt>
     * @throws IllegalArgumentException  <tt>null? </tt>
     */
    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        return org.apache.commons.lang.time.DateUtils.isSameDay(cal1, cal2);
    }

    /**
     * ?  ?  ?? ?.
     *
     * @param date1 ?  
     * @param date2 ? ? 
     * @return  ?? <tt>true</tt>
     * @throws IllegalArgumentException  <tt>null? </tt>
     */
    public static boolean isSameInstant(Date date1, Date date2) {
        return org.apache.commons.lang.time.DateUtils.isSameInstant(date1, date2);
    }

    /**
     * ?  ?  ?? ?.
     *
     * @param cal1 ?  
     * @param cal2 ? ? 
     * @return true  ?? <tt>true</tt>
     * @throws IllegalArgumentException  <tt>null? </tt>
     */
    public static boolean isSameInstant(Calendar cal1, Calendar cal2) {
        return org.apache.commons.lang.time.DateUtils.isSameInstant(cal1, cal2);
    }

    /**
     * ?  ?   ?? ?. ?  , , , , , , ?, ERA  ? .
     *
     * @param cal1 ?  
     * @param cal2 ? ? 
     * @return true  ?? <tt>true</tt>
     * @throws IllegalArgumentException  <tt>null? </tt>
     */
    public static boolean isSameLocalTime(Calendar cal1, Calendar cal2) {
        return org.apache.commons.lang.time.DateUtils.isSameLocalTime(cal1, cal2);
    }

    /**
     *    ? ? ? ?  .
     *
     * @param str            (<tt>null</tt>? )
     * @param parsePatterns ?   ? . <tt>null</tt>? . {@link SimpleDateFormat}? .
     * @return ? 
     * @throws IllegalArgumentException ? ?  ? ? <tt>null</tt>? 
     * @throws ParseException ? ? ? 
     */
    public static Date parseDate(String str, String[] parsePatterns) throws ParseException {
        return org.apache.commons.lang.time.DateUtils.parseDate(str, parsePatterns);
    }

    /**
     * ?  ??  . ? ? ?  ?  ?.
     *
     * @param date   (<tt>null</tt>? )
     * @param amount  (?? )
     * @return     ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date addYears(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addYears(date, amount);
    }

    /**
     * ?  ??  . ? ? ?  ?  ?.
     *
     * @param date   (<tt>null</tt>? )
     * @param amount  (?? )
     * @return     ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date addMonths(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addMonths(date, amount);
    }

    /**
     * ?  ?? (week) . ? ? ?  ?  ?.
     *
     * @param date   (<tt>null</tt>? )
     * @param amount  (?? )
     * @return (week)    ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date addWeeks(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addWeeks(date, amount);
    }

    /**
     * ?  ?? ? . ? ? ?  ?  ?.
     *
     * @param date   (<tt>null</tt>? )
     * @param amount  (?? )
     * @return ?    ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date addDays(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addDays(date, amount);
    }

    /**
     * ?  ?? ? . ? ? ?  ?  ?.
     *
     * @param date   (<tt>null</tt>? )
     * @param amount  (?? )
     * @return ?    ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date addHours(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addHours(date, amount);
    }

    /**
     * ?  ?? ? . ? ? ?  ?  ?.
     *
     * @param date   (<tt>null</tt>? )
     * @param amount  (?? )
     * @return ?    ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date addMinutes(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addMinutes(date, amount);
    }

    /**
     * ?  ??  . ? ? ?  ?  ?.
     *
     * @param date   (<tt>null</tt>? )
     * @param amount  (?? )
     * @return     ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date addSeconds(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addSeconds(date, amount);
    }

    /**
     * ?  ??  . ? ? ?  ?  ?.
     *
     * @param date   (<tt>null</tt>? )
     * @param amount  (?? )
     * @return     ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date addMilliseconds(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addMilliseconds(date, amount);
    }

    /**
     * ?   ? . ? ? ?  ?  ?.
     *
     * @param date          (<tt>null</tt>? )
     * @param calendarField {@link Calendar}? 
     * @param amount         (?? )
     * @return    ?
     * @throws IllegalArgumentException  <tt>null</tt>? 
     */
    public static Date add(Date date, int calendarField, int amount) {
        return org.apache.commons.lang.time.DateUtils.add(date, calendarField, amount);
    }

    /**
     * YYYY-MM-DDTHH:MI:SS ? ?(:2007-02-13T10:25:00) XMLGregorianCalendar ? ?.
     *
     * @param stringTypeDate YYYY-MM-DDTHH:MI:SS ? ?(:2007-02-13T10:25:00)
     * @return XMLGregorianCalendar
     */
    public static XMLGregorianCalendar toXMLGregorianCalendar(String stringTypeDate)
            throws DatatypeConfigurationException {
        String yyyy = stringTypeDate.substring(0, 4);
        String mm = stringTypeDate.substring(5, 7);
        String dd = stringTypeDate.substring(8, 10);
        String hh = stringTypeDate.substring(11, 13);
        String mi = stringTypeDate.substring(14, 16);
        String ss = stringTypeDate.substring(17, 19);

        int iyyyy = Integer.parseInt(yyyy);
        int imm = Integer.parseInt(mm);
        int idd = Integer.parseInt(dd);
        int ihh = Integer.parseInt(hh);
        int imi = Integer.parseInt(mi);
        int iss = Integer.parseInt(ss);

        DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
        return dataTypeFactory.newXMLGregorianCalendar(iyyyy, imm, idd, ihh, imi, iss, 0, 0);
    }

    /**
     *  ? ? ?   .
     *
     * @param date    
     * @param before ? ?
     * @return  ? ? ?  
     */
    public static Date before(Date date, int before) {
        return org.apache.commons.lang.time.DateUtils.addDays(date, -before);
    }

    /**
     *  ? ? ? ?  .
     *
     * @param date   
     * @param after ? ?
     * @return  ? ? ?  
     */
    public static Date after(Date date, int after) {
        return org.apache.commons.lang.time.DateUtils.addDays(date, after);
    }

    /**
     *  ?    ?? ? ?   .  ?  ? . ?    .
     *
     * @param baseDate ?
     * @param type       (before, after)
     * @param duration ?  ?(;  20)
     * @return ? 
     * @throws ParseException       
     */
    public static Date calculateDate(Date baseDate, TYPE type, int duration) throws ParseException {
        switch (type) {
        case BEFORE:
            return DateUtils.before(baseDate, duration);
        case AFTER:
            return DateUtils.after(baseDate, duration);
        default:
            return DateUtils.before(baseDate, duration);
        }
    }

    /**
     *  ?    ?? ? ?   .
     *
     * @param date     ?
     * @param type       (before, after)
     * @param duration ?  ?(;  20)
     * @return ? 
     * @throws ParseException       
     */
    public static String calculateDate(String date, TYPE type, int duration) throws ParseException {
        Date baseDate = DateUtils.parseDate(date, DATE_FORMAT);
        Date result = DateUtils.calculateDate(baseDate, type, duration);
        return parseDate(result, DATE_FORMAT[0]);
    }

    /**
     *  ?    ?? ? ?   .
     *
     * @param date        ?
     * @param strType       (before, after)
     * @param strDuration ? ? ?  ?(;  "20")
     * @return ? 
     * @throws ParseException       
     */
    public static String calculateDate(String date, String strType, String strDuration) throws ParseException {
        int duration = Integer.parseInt(strDuration);

        if ("BEFORE".equals(strType)) {
            return calculateDate(date, TYPE.BEFORE, duration);
        } else if ("AFTER".equals(strType)) {
            return calculateDate(date, TYPE.AFTER, duration);
        } else {
            return null;
        }

    }

    /**
     *  ? ? ?? ?  .
     *
     * @param date    
     * @param pattern  (; YYYYMMDD)
     * @return ? 
     */
    public static String parseDate(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.US);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return formatter.format(calendar.getTime());
    }

    /**
     * ? ?  . ?  ? ? .
     * <p>
     * <pre>
     * long date = DateUtils.getDiffDays("20080501",DateUtils.getCurrentYyyymmdd())
     * </pre>
     * </p>
     *
     * @param startStr  
     * @param endStr   ?? 
     * @return ?
     */
    public static long getDiffDays(String startStr, String endStr) {
        GregorianCalendar start = getGregorianCalendar(startStr);
        GregorianCalendar end = getGregorianCalendar(endStr);
        return (start.getTime().getTime() - end.getTime().getTime()) / 86400000;
    }

    public static long getDiffDays(Date start, Date end) {
        return (start.getTime() - end.getTime()) / 86400000;
    }

    public static long getDiffHours(String startStr, String endStr) {
        GregorianCalendar start = getGregorianCalendar(startStr);
        GregorianCalendar end = getGregorianCalendar(endStr);
        return (start.getTime().getTime() - end.getTime().getTime()) / (60 * 1000);
    }

    public static long getDiffHours(Date start, Date end) {
        return (start.getTime() - end.getTime()) / (60 * 60 * 1000);
    }

    /**
     * ? ? ?  . ?  ? ? .
     *
     * @param from  
     * @param to   ?? 
     * @return 
     */
    public static long getDiffSeconds(Date from, Date to) {
        return (from.getTime() - to.getTime()) / 1000;
    }

    /**
     * "yyyyMMdd" ? ??  {@link Calendar} ? .
     * <p>
     * <pre>
     * Calendar cal = DateUtils.getGregorianCalendar(DateUtil.getCurrentYyyymmdd());
     * </pre>
     * </p>
     *
     * @param yyyymmdd 
     * @return GregorianCalendar
     */
    public static GregorianCalendar getGregorianCalendar(String yyyymmdd) {
        int yyyy = Integer.parseInt(yyyymmdd.substring(0, 4));
        int mm = Integer.parseInt(yyyymmdd.substring(4, 6));
        int dd = Integer.parseInt(yyyymmdd.substring(6, 8));
        GregorianCalendar calendar = new GregorianCalendar(yyyy, mm - 1, dd, 0, 0, 0);
        return calendar;
    }

    /**
     *  ? ?? ?  .
     *
     * @param yyyyMM 
     * @return ?? ? ?
     */
    public static String getStartMonthDayOfDate(String yyyyMM) {
        return yyyyMM + "01";
    }

    /**
     *  ?  ?? ?  .
     *
     * @param yyyyMM 
     * @return ? ? ?
     */
    public static String getEndMonthDayOfDate(String yyyyMM) {
        int mm = Integer.parseInt(yyyyMM.substring(4, 6));
        return yyyyMM + DAYS_OF_MONTH[mm - 1];
    }

    /**
     *   "yyyyMMdd"  .
     * <p>
     * <pre>
     * String today = DateUtils.getCurrentYyyymmdd();
     * </pre>
     * </p>
     *
     * @return yyyyMMdd
     */
    public static String getCurrentYyyymmdd() {
        return getCurrentDateTime().substring(0, 8);
    }

    /**
     *   "yyyyMM"  .
     * <p>
     * <pre>
     * String today = DateUtils.getCurrentYyyymm();
     * </pre>
     * </p>
     *
     * @return yyyyMM
     */
    public static String getCurrentYyyymm() {
        return getCurrentDateTime().substring(0, 6);
    }

    /**
     *   ?? "yyyyMMddhhmmss"  .
     * <p>
     * <pre>
     * String today = DateUtils.getCurrentDateTime();
     * </pre>
     * </p>
     *
     * @return yyyyMMddhhmmss ?? ? 
     */
    public static String getCurrentDateTime() {
        Date today = new Date();
        Locale currentLocale = new Locale("KOREAN", "KOREA");
        String pattern = "yyyyMMddHHmmss";
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
        return formatter.format(today);
    }

    /**
     * java.util.Date  java.util.GregorianCalendar  .
     *
     * @param date 
     * @return GregorianCalendar
     */
    public static GregorianCalendar dateToGregorianCalendar(Date date) {
        if (date == null)
            return null;
        GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT+09:00"), Locale.KOREAN);
        cal.setTime(date);
        return cal;
    }

    /**
     * java.util.Date  javax.xml.datatype.XMLGregorianCalendar  .
     *
     * @param date 
     * @return XMLGregorianCalendar
     * @throws DatatypeConfigurationException
     */
    public static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date) throws DatatypeConfigurationException {
        if (date == null)
            return null;
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateToGregorianCalendar(date));
    }

    /**
     *    ? ? ?.
     *
     * @param end    
     * @param start  
     * @return "H:M:S" ?? 
     */
    public static String formatDiffTime(Date end, Date start) {
        long timeDiff = end.getTime() - start.getTime();
        return formatTime(timeDiff);
    }

    /**
     *  ? "H:M:S" ? ?.
     *
     * @param diffLongTime 
     * @return "H:M:S" ?? 
     */
    public static String formatTime(long diffLongTime) {
        StringBuffer buf = new StringBuffer();
        long hours = diffLongTime / (60 * 60 * 1000);
        long rem = (diffLongTime % (60 * 60 * 1000));
        long minutes = rem / (60 * 1000);
        rem = rem % (60 * 1000);
        long seconds = rem / 1000;

        if (hours != 0) {
            buf.append(hours);
            buf.append(" ");
        }
        if (minutes != 0) {
            buf.append(minutes);
            buf.append(" ");
        }
        // ?  0? .
        buf.append(seconds);
        buf.append("");
        return buf.toString();
    }

    /**
     * ? ? ? . ?  ? ? .
     *
     * @param from  
     * @param to   ?? 
     * @return Long   ?
     */
    public static long getDiff(Date from, Date to) {
        return (from.getTime() - to.getTime());
    }

    /**
     * ? ? .
     *
     * @param month  int
     * @return String  ?.
     */
    public static String getMonthString(int month) {
        String monthString = "";
        switch (month) {
        case 1:
            monthString = "January";
            break;
        case 2:
            monthString = "February";
            break;
        case 3:
            monthString = "March";
            break;
        case 4:
            monthString = "April";
            break;
        case 5:
            monthString = "May";
            break;
        case 6:
            monthString = "June";
            break;
        case 7:
            monthString = "July";
            break;
        case 8:
            monthString = "August";
            break;
        case 9:
            monthString = "September";
            break;
        case 10:
            monthString = "October";
            break;
        case 11:
            monthString = "November";
            break;
        case 12:
            monthString = "December";
            break;
        default:
            monthString = "Invalid month";
            break;
        }
        return monthString;
    }

    /**
     * sql ?? ?? .
     *
     */
    public static Date sqlDateToUtilDate(java.sql.Date sqlDate) {
        Date utilDate = new Date(sqlDate.getTime());
        return utilDate;
    }
}