org.openflamingo.uploader.util.DateUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.openflamingo.uploader.util.DateUtils.java

Source

/**
 * Flamingo HDFS File Uploader - a tool to upload from datasource to datasource and schedule jobs
 *
 * Copyright (C) 2011-2012 Cloudine.
 *
 * This file is part of Flamingo HDFS File Uploader.
 *
 * Flamingo HDFS File Uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Flamingo HDFS File Uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openflamingo.uploader.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 Edward 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 java.text.SimpleDateFormat}? .
     * @return ? 
     * @throws IllegalArgumentException ? ?  ? ? <tt>null</tt>? 
     * @throws java.text.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 java.util.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);

        javax.xml.datatype.DatatypeFactory dataTypeFactory = javax.xml.datatype.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 java.text.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 java.text.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 java.text.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;
    }

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

    /**
     * "yyyyMMdd" ? ??  {@link java.util.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 java.util.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 javax.xml.datatype.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();
    }
}