com.syaku.commons.DateUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.syaku.commons.DateUtils.java

Source

/*
 * DateUtils.java 2008.12.17
 *
 * Copyright (c) 2010, MEI By Seok Kyun. Choi. ()
 * http://syaku.tistory.com
 * 
 * GNU Lesser General Public License
 * http://www.gnu.org/licenses/lgpl.html
 */

package com.syaku.commons;

import java.util.*;
import java.util.regex.*;
import java.text.*;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;

/**
 *   
 * @author Seok Kyun. Choi. ()
 */

public class DateUtils {
    private static Logger log = Logger.getLogger(DateUtils.class);

    private static SimpleDateFormat formatter;
    private static DecimalFormat df;

    /**
    * @method : getDate(parameter,parameter2)
    * @brief :  ,  .     .
    * @parameters {
    parameter : (String) $1 =  , $2 =  , $3 =  , $4 =  , $5 =  , $6 = 
    parameter2 : (String) (-_:./\s)   
        }
    * @return : (String)   || 00:00:00
    */
    public static String getDate(String patten, String date) {
        if (StringUtils.isEmpty(date))
            return date;
        String sysdate_patten = "(^[0-9]*)[-_:.\\/\\s]?([0-9]*)[-_:.\\/\\s]?([0-9]*)[-_:.\\/\\s]?([0-9]*)[-_:.\\/\\s]?([0-9]*)[-_:.\\/\\s]?([0-9]*)(.*)$";
        Pattern date_comp = Pattern.compile(sysdate_patten);
        if (date_comp.matcher(date).find())
            return date.replaceAll(sysdate_patten, patten);
        else
            return getDate(patten, "00:00:00");
    }

    /**
    * @method : setDate(parameter)
    * @brief :   Date  .
    * @parameters {
    parameter : (String) 
        }
    * @return : (Date)
    */
    public static Date setDate(String date) {
        Date retDate = null;
        try {
            date = date("yyyy-MM-dd HH:mm:ss", date);
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            retDate = formatter.parse(date);
        } catch (Exception e) {
            log.error("[#MEI DateUtils.setDate] " + e.toString());
        }

        return retDate;
    }

    public static Date setDateString(String date) {
        Date retDate = null;

        try {
            String sysdate_patten = "(^[A-Za-z]{3}), ([0-9]{2}) ([A-Za-z]{3}) ([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) ([+0-9]{5})$";
            String yyyy = date.replaceAll(sysdate_patten, "$4");
            String MM = date.replaceAll(sysdate_patten, "$3");
            String dd = date.replaceAll(sysdate_patten, "$2");
            String hh = date.replaceAll(sysdate_patten, "$5");
            String mm = date.replaceAll(sysdate_patten, "$6");
            String ss = date.replaceAll(sysdate_patten, "$7");
            String timezone = date.replaceAll(sysdate_patten, "$8");

            String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
                    "Dec" };
            int months_int = months.length;
            for (int i = 0; i < months_int; i++) {
                String month = months[i];
                if (StringUtils.equals(MM, month)) {
                    MM = "" + (i + 1);
                    MM = StringUtils.leftPad(MM, 2, '0');
                    break;
                }
            }

            date = yyyy + MM + dd + hh + mm + ss;
            date = date("yyyy-MM-dd HH:mm:ss", date);
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            retDate = formatter.parse(date);
        } catch (Exception e) {
            log.error("[#MEI DateUtils.setDateString] " + e.toString());
        }

        return retDate;
    }

    /**
    * @method : getTime(parameter,parameter2)
    * @brief :  ,  .     .
    * @parameters {
    parameter : (String) $1 =  , $2 =  , $3 = 
    parameter2 : (String) (-_:./\s)   
        }
    * @return : (String)   || 00:00:00
    */
    public static String getTime(String patten, String time) {
        if (StringUtils.isEmpty(time))
            return time;
        String time_patten = "(^[0-9]*)[-_:.\\/\\s]?([0-9]*)[-_:.\\/\\s]?([0-9]*)(.*)$";
        Pattern time_comp = Pattern.compile(time_patten);
        if (time_comp.matcher(time).find())
            return time.replaceAll(time_patten, patten);
        else
            return getTime(patten, "00:00:00");
    }

    /**
    * @method : setTime(parameter)
    * @brief :   Date  .
    * @parameters {
    parameter : (String) 
        }
    * @return : (Date)
    */
    public static Date setTime(String time) {
        Date retDate = null;
        try {
            time = time("HH:mm:ss", time);
            formatter = new SimpleDateFormat("HH:mm:ss");
            retDate = formatter.parse(time);
        } catch (Exception e) {
            log.error("[#MEI DateUtils.setTime] " + e.toString());
        }
        return retDate;
    }

    /**
    * @method : date(parameter, parameter2)
    * @brief : SimpleDateFormat   .
    * @parameters {
         parameter : (String) SimpleDateFormat  
         parameter2 : (String || Date) SimpleDateFormat  
       }
    * @return : (String) 
    */
    public static String date() {
        return date("yyyy-MM-dd HH:mm:ss");
    }

    public static String date(String format) {
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formatter.format(new Date());
        return date(format, date);
    }

    public static String date(String format, Date date) {
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return date(format, formatter.format(date));
    }

    public static String date(String format, String date) {
        String string = null;

        try {
            if (StringUtils.isEmpty(format))
                format = "yyyy-MM-dd HH:mm:ss";
            if (StringUtils.isEmpty(date))
                return date;

            date = date.replaceAll("[^0-9]+", "");
            date = StringUtils.rightPad(date, 14, '0');
            date = date.replaceAll("(^[0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})",
                    "$1-$2-$3 $4:$5:$6");
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date redate = formatter.parse(date);
            formatter = new SimpleDateFormat(format);
            string = formatter.format(redate);
        } catch (Exception e) {
            log.error("[#MEI DateUtils.date] " + e.toString());
        }

        return string;
    }

    /**
    * @method : time(parameter, parameter2)
    * @brief SimpleDateFormat   .
    * @parameters {
    parameter : (String) SimpleDateFormat  
    parameter2 : (String || Date) SimpleDateFormat  
      }
    * @return : (String) 
    */
    public static String time() {
        return time("HH:mm:ss");
    }

    public static String time(String format) {
        formatter = new SimpleDateFormat("HH:mm:ss");
        String time = formatter.format(new Date());
        return time(format, time);
    }

    public static String time(String format, String time) {
        time = time.replaceAll("[^0-9]+", "");
        time = StringUtils.rightPad(time, 6, '0');
        time = StringUtils.leftPad(time, 14, '0');
        return date(format, time);
    }

    /**
    * @method : timespace(parameter, parameter2,parameter3)
    * @brief    .
    * @parameters {
    parameter : (String) 
    parameter2 : (String) 
    parameter3 : (String) SimpleDateFormat  
      }
    * @return : (String) SimpleDateFormat  
    */
    public static String timespace(String stime, String etime) {
        return timespace(stime, etime, "HH:mm:ss");
    }

    public static String timespace(String stime, String etime, String format) {
        try {
            if (StringUtils.isEmpty(stime) || StringUtils.isEmpty(etime))
                throw new Exception("parameter null");

            stime = stime.replaceAll("[^0-9]+", "");
            etime = etime.replaceAll("[^0-9]+", "");

            int s = Integer.parseInt(stime);
            int e = Integer.parseInt(etime);
            stime = StringUtils.rightPad(stime, 6, '0');
            etime = StringUtils.rightPad(etime, 6, '0');
            stime = date("yyyy-MM-dd HH:mm:ss", "19700101" + stime);
            etime = date("yyyy-MM-dd HH:mm:ss", "19700101" + etime);

            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date sdate = formatter.parse(stime);
            Date edate = formatter.parse(etime);
            // +0900
            Long ret = edate.getTime() - sdate.getTime();
            String ext = formatter.format(ret);

            return date(format, ext);
        } catch (Exception e) {
            return time(format, "000000");
        }
    }

    /**
    * @method : timeAdd(parameter, parameter2,parameter3)
    * @brief     .    60      .
    * @parameters {
    parameter : (String) (-_:./\s)  
    parameter2 : (String) (-_:./\s)  
    parameter3 : (String)  $1 =  , $2 =  , $3 = 
      }
    * @return : (String)   : 00:00:00
    */
    public static String timeAdd(String time, String time2, String patten) {
        String ret = "00:00:00";
        if (StringUtils.isEmpty(patten))
            patten = "$1:$2:$3";

        int sh = Integer.parseInt(getTime("$1", time));
        int sm = Integer.parseInt(getTime("$2", time));
        int ss = Integer.parseInt(getTime("$3", time));
        int eh = Integer.parseInt(getTime("$1", time2));
        int em = Integer.parseInt(getTime("$2", time2));
        int es = Integer.parseInt(getTime("$3", time2));

        try {
            int h = sh + eh;
            int s = ss + es;
            int m = 0;
            if (s > 60) {
                Double mm = Math.floor(s / 60);
                df = new DecimalFormat("0");
                int mmm = Integer.parseInt(df.format(mm));

                s = s - (mmm * 60);
                m = m + mmm;
            }

            m = m + sm + em;
            if (m > 60) {
                Double hh = Math.floor(m / 60);
                df = new DecimalFormat("0");
                int hhh = Integer.parseInt(df.format(hh));

                m = m - (hhh * 60);
                h = h + hhh;
            }

            ret = StringUtils.leftPad(Integer.toString(h), 2, '0') + ":"
                    + StringUtils.leftPad(Integer.toString(m), 2, '0') + ":"
                    + StringUtils.leftPad(Integer.toString(s), 2, '0');
        } catch (Exception e) {
            ret = "00:00:00";
        }

        return getTime(patten, ret);
    }

}