gjjl_DateUtil(依赖gjjl_StringUtil)

 package com.charging.utils;

 

 import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

 

 public class DateUtil

 {

   protected static final Log log = LogFactory.getLog(DateUtil.class);

 

   public static final String Format_Date = "yyyy-MM-dd";

   public static final String Format_Date2 = "MM月dd日";

   public static final String Format_Time = "HH:mm:ss";

   public static final String Format_Time2 = "HH:mm";

   public static final String Format_DateTime = "yyyy-MM-dd HH:mm:ss";

   public static final String Format_DateTime2 = "yyyy-MM-dd HH:mm";

   public static final String Format_DateTime3 = "yyyyMMddHHmmss";

   public static final String Format_DateTime4 = "MM月dd日 HH:mm";

   public static final String Format_Start = "-01-01";

   public static final String Format_End = "12-31";

 

   /**

    * 由当天时间得到当天日期的时间类型

    * @param time

    * @param format

    * @return

    */

   public static Date getDayByTime(String time,String format){

       Date thisTime = parseDateTime(time, format);

       String thisDate = toString(thisTime);

       return parseDateTime(thisDate);

   }

   /**

    * 由当天时间得到当天日期的时间类型

    * @param time

    * @param format

    * @return

    */

   public static Date getDayByTime(Date thisTime){

       String thisDate = toString(thisTime);

       return parseDateTime(thisDate);

   }

 

   /**

    *得到两日期相减的值 

   */

   public static Long getTwoDatesMinus(Date beginDate, Date endDate){

       Date lastDate = DateUtil.addDay(endDate, 1);

       Long number = lastDate.getTime()-beginDate.getTime();

       number = number/(24*60*60*1000);

       return number;

   }

   /**

    * 得到日期当天的分钟数

    */

   public static Integer getDateMinute(Date time){

       String thisDateString = DateUtil.toString(time);

       Date thisDate = DateUtil.parseDateTime(thisDateString);

       Long number = time.getTime()-thisDate.getTime();

       Long minute = number/(60*1000);

       return Integer.valueOf(minute.toString());

   }

   /**

    * 将日期与时间拼接起来(日期应为yyyy-MM-dd)

    */

   public static Date togetherDateAndTime(Date thisDate,Date thisTime){

       Integer minute = getDateMinute(thisTime);

       Date newTime = DateUtil.addMinute(thisDate, minute);

       return newTime;

   }

 

   public static String getCurrentDate()

   {

     return new SimpleDateFormat("yyyy-MM-dd").format(new Date());

   }

 

   public static String getCurrentDate(String format)

   {

     SimpleDateFormat t = new SimpleDateFormat(format);

     return t.format(new Date());

   }

 

   public static String getCurrentTime()

   {

     return new SimpleDateFormat("HH:mm:ss").format(new Date());

   }

 

   public static String getCurrentTime(String format)

   {

     SimpleDateFormat t = new SimpleDateFormat(format);

     return t.format(new Date());

   }

 

   public static String getCurrentDateTime()

   {

     String format = "yyyy-MM-dd HH:mm:ss";

     return getCurrentDateTime(format);

   }

 

   public static int getDayOfWeek()

   {

     Calendar cal = Calendar.getInstance();

     return cal.get(7);

   }

 

   public static int getDayOfWeek(Date date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(date);

     return cal.get(7);

   }

 

   public static int getDayOfMonth()

   {

     Calendar cal = Calendar.getInstance();

     return cal.get(5);

   }

 

   public static int getDayOfMonth(Date date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(date);

     return cal.get(5);

   }

 

   public static int getMaxDayOfMonth(Date date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(date);

     return cal.getActualMaximum(5);

   }

   public static String getEndDayOfMonth(Date date){

     Calendar cal = Calendar.getInstance();

     cal.setTime(date);

     cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));

     return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

 

   }

   public static String getEndDayOfMonth(String date){

         Calendar cal = Calendar.getInstance();

         cal.setTime(parse(date));

         cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));

         return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

 

       }

   public static String getFirstDayOfMonth(String date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(parse(date));

     cal.set(5, 1);

     return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

   }

   public static String getFirstDayOfMonth(Date date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(date);

     cal.set(5, 1);

     return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

   }

 

   public static int getDayOfYear()

   {

     Calendar cal = Calendar.getInstance();

     return cal.get(6);

   }

 

   public static int getDayOfYear(Date date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(date);

     return cal.get(6);

   }

 

   public static int getDayOfWeek(String date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(parse(date));

     return cal.get(7);

   }

 

   public static int getDayOfMonth(String date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(parse(date));

     return cal.get(5);

   }

 

   public static int getDayOfYear(String date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(parse(date));

     return cal.get(6);

   }

 

   public static String getCurrentDateTime(String format)

   {

     SimpleDateFormat t = new SimpleDateFormat(format);

     return t.format(new Date());

   }

 

   public static String toString(Date date)

   {

     if (date == null) {

       return "";

     }

     return new SimpleDateFormat("yyyy-MM-dd").format(date);

   }

 

   public static String toDateTimeString(Date date)

   {

     if (date == null) {

       return "";

     }

     return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

   }

 

   public static String toString(Date date, String format)

   {

     if(date == null){

         return "";

     }

     SimpleDateFormat t = new SimpleDateFormat(format);

     return t.format(date);

   }

 

   public static String toTimeString(Date date)

   {

     if (date == null) {

       return "";

     }

     return new SimpleDateFormat("HH:mm:ss").format(date);

   }

 

   public static int compare(String date1, String date2)

   {

     return compare(date1, date2, "yyyy-MM-dd");

   }

 

   /**

    * 第一个参数是否大于第二个参数(大于true,小于等于false)

    */

   public static boolean compareDate(Date date1, Date date2){

       if(date1.compareTo(date2)==1){

           return true;

       }else{

           return false;

       }

   }

 

   public static int compareTime(String time1, String time2)

   {

     return compareTime(time1, time2, "HH:mm:ss");

   }

 

   public static int compare(String date1, String date2, String format)

   {

     Date d1 = parse(date1, format);

     Date d2 = parse(date2, format);

     return d1.compareTo(d2);

   }

   /**

    * 第一个参数是否大于第二个参数(大于true,小于等于false)

    */

   public static boolean compareTimeByFormat(String date1, String date2,String format){

       Date d1 = parse(date1, format);

       Date d2 = parse(date2, format);

       return compareDate(d1, d2);

   }

 

   public static int compareTime(String time1, String time2, String format)

   {

     String[] arr1 = time1.split(":");

     String[] arr2 = time2.split(":");

     if (arr1.length < 2) {

       throw new RuntimeException("错误的时间值:" + time1);

     }

     if (arr2.length < 2) {

       throw new RuntimeException("错误的时间值:" + time2);

     }

     int h1 = Integer.parseInt(arr1[0]);

     int m1 = Integer.parseInt(arr1[1]);

     int h2 = Integer.parseInt(arr2[0]);

     int m2 = Integer.parseInt(arr2[1]);

     int s1 = 0; int s2 = 0;

     if (arr1.length == 3) {

       s1 = Integer.parseInt(arr1[2]);

     }

     if (arr2.length == 3) {

       s2 = Integer.parseInt(arr2[2]);

     }

     if ((h1 < 0) || (h1 > 23) || (m1 < 0) || (m1 > 59) || (s1 < 0) || (s1 > 59)) {

       throw new RuntimeException("错误的时间值:" + time1);

     }

     if ((h2 < 0) || (h2 > 23) || (m2 < 0) || (m2 > 59) || (s2 < 0) || (s2 > 59)) {

       throw new RuntimeException("错误的时间值:" + time2);

     }

     if (h1 != h2) {

       return h1 > h2 ? 1 : -1;

     }

     if (m1 == m2) {

       if (s1 == s2) {

         return 0;

       }

       return s1 > s2 ? 1 : -1;

     }

 

     return m1 > m2 ? 1 : -1;

   }

 

   public static boolean isTime(String time)

   {

     String[] arr = time.split(":");

     if (arr.length < 2)

       return false;

     try

     {

       int h = Integer.parseInt(arr[0]);

       int m = Integer.parseInt(arr[1]);

       int s = 0;

       if (arr.length == 3) {

         s = Integer.parseInt(arr[2]);

       }

       if ((h < 0) || (h > 23) || (m < 0) || (m > 59) || (s < 0) || (s > 59))

         return false;

     }

     catch (Exception e) {

       return false;

     }

     return true;

   }

 

   public static boolean isDate(String date)

   {

     String[] arr = date.split("-");

     if (arr.length < 3)

       return false;

     try

     {

       int y = Integer.parseInt(arr[0]);

       int m = Integer.parseInt(arr[1]);

       int d = Integer.parseInt(arr[2]);

       if ((y < 0) || (m > 12) || (m < 0) || (d < 0) || (d > 31))

         return false;

     }

     catch (Exception e) {

       return false;

     }

     return true;

   }

 

   public static boolean isWeekend(Date date)

   {

     Calendar cal = Calendar.getInstance();

     cal.setTime(date);

     int t = cal.get(7);

 

     return (t == 7) || (t == 1);

   }

 

   public static boolean isWeekend(String str)

   {

     return isWeekend(parse(str));

   }

 

   public static Date parse(String str)

   {

     if (StringUtil.isEmpty(str))

       return null;

     try

     {

       return new SimpleDateFormat("yyyy-MM-dd").parse(str);

     } catch (ParseException e) {

       log.error(e);

     }return null;

   }

 

   public static Date parse(String str, String format)

   {

     if (StringUtil.isEmpty(str))

       return null;

     try

     {

       SimpleDateFormat t = new SimpleDateFormat(format);

       return t.parse(str);

     } catch (ParseException e) {

       log.error(e);

     }return null;

   }

 

   public static Date parseDateTime(String str)

   {

     if (StringUtil.isEmpty(str)) {

       return null;

     }

     if (str.length() <= 10)

       return parse(str);

     try

     {

       return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);

     } catch (ParseException e) {

       log.error(e);

     }return null;

   }

 

   public static Date parseDateTime(String str, String format)

   {

     if (StringUtil.isEmpty(str))

       return null;

     try

     {

       SimpleDateFormat t = new SimpleDateFormat(format);

       return t.parse(str);

     } catch (ParseException e) {

       log.error(e);

     }return null;

   }

 

   public static Date addSecond(Date date, int count){

       return new Date(date.getTime() + 1000L * count);

   }

 

   public static Date addMinute(Date date, int count)

   {

     return new Date(date.getTime() + 60000L * count);

   }

 

   public static Date addHour(Date date, int count)

   {

     return new Date(date.getTime() + 3600000L * count);

   }

 

   public static Date addDay(Date date, int count)

   {

     return new Date(date.getTime() + 86400000L * count);

   }

 

   public static Date addWeek(Date date, int count)

   {

     Calendar c = Calendar.getInstance();

     c.setTime(date);

     c.add(3, count);

     return c.getTime();

   }

 

   public static Date addMonth(Date date, int count)

   {

     Calendar c = Calendar.getInstance();

     c.setTime(date);

     c.add(2, count);

     return c.getTime();

   }

 

   public static Date addYear(Date date, int count)

   {

     Calendar c = Calendar.getInstance();

     c.setTime(date);

     c.add(1, count);

     return c.getTime();

   }

 

    public static long subDateTime(Date date1, Date date2, int ds, int math) {

        long s = date1.getTime() - date2.getTime();

        switch (ds) {

        case Calendar.SECOND:

            s = mathFun(new Double(s) / 1000, math);

            break;

        case Calendar.MINUTE:

            s = mathFun(new Double(s) / (1000*60), math);

            break;

        case Calendar.HOUR:

            s = mathFun(new Double(s) / (1000*60*60), math);

            break;

        case Calendar.DATE:

            s = mathFun(new Double(s) / (1000*60*60*24), math);

            break;

        default:

            break;

        }

        return s;

    }

 

    private static long mathFun(double n, int math){

        if(math>0){

            return new Double(Math.ceil(n)).longValue();

        }else if(math==0){

            return Math.round(n);

        }else{

            return new Double(Math.floor(n)).longValue();

        }

    }

 

   public static String toDisplayDateTime(String date)

   {

     if (StringUtil.isEmpty(date))

       return null;

     try

     {

       if (isDate(date)) {

         return toDisplayDateTime(parse(date));

       }

       SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

       Date d = t.parse(date);

       return toDisplayDateTime(d);

     }

     catch (ParseException e)

     {

       log.error(e);

     }return "不是标准格式时间!";

   }

 

   public static String convertChineseNumber(String strDate)

   {

     strDate = StringUtil.replaceEx(strDate, "一十一", "11");

     strDate = StringUtil.replaceEx(strDate, "一十二", "12");

     strDate = StringUtil.replaceEx(strDate, "一十三", "13");

     strDate = StringUtil.replaceEx(strDate, "一十四", "14");

     strDate = StringUtil.replaceEx(strDate, "一十五", "15");

     strDate = StringUtil.replaceEx(strDate, "一十六", "16");

     strDate = StringUtil.replaceEx(strDate, "一十七", "17");

     strDate = StringUtil.replaceEx(strDate, "一十八", "18");

     strDate = StringUtil.replaceEx(strDate, "一十九", "19");

     strDate = StringUtil.replaceEx(strDate, "二十一", "21");

     strDate = StringUtil.replaceEx(strDate, "二十二", "22");

     strDate = StringUtil.replaceEx(strDate, "二十三", "23");

     strDate = StringUtil.replaceEx(strDate, "二十四", "24");

     strDate = StringUtil.replaceEx(strDate, "二十五", "25");

     strDate = StringUtil.replaceEx(strDate, "二十六", "26");

     strDate = StringUtil.replaceEx(strDate, "二十七", "27");

     strDate = StringUtil.replaceEx(strDate, "二十八", "28");

     strDate = StringUtil.replaceEx(strDate, "二十九", "29");

     strDate = StringUtil.replaceEx(strDate, "十一", "11");

     strDate = StringUtil.replaceEx(strDate, "十二", "12");

     strDate = StringUtil.replaceEx(strDate, "十三", "13");

     strDate = StringUtil.replaceEx(strDate, "十四", "14");

     strDate = StringUtil.replaceEx(strDate, "十五", "15");

     strDate = StringUtil.replaceEx(strDate, "十六", "16");

     strDate = StringUtil.replaceEx(strDate, "十七", "17");

     strDate = StringUtil.replaceEx(strDate, "十八", "18");

     strDate = StringUtil.replaceEx(strDate, "十九", "19");

     strDate = StringUtil.replaceEx(strDate, "十", "10");

     strDate = StringUtil.replaceEx(strDate, "二十", "20");

     strDate = StringUtil.replaceEx(strDate, "三十", "20");

     strDate = StringUtil.replaceEx(strDate, "三十一", "31");

     strDate = StringUtil.replaceEx(strDate, "零", "0");

     strDate = StringUtil.replaceEx(strDate, "○", "0");

     strDate = StringUtil.replaceEx(strDate, "一", "1");

     strDate = StringUtil.replaceEx(strDate, "二", "2");

     strDate = StringUtil.replaceEx(strDate, "三", "3");

     strDate = StringUtil.replaceEx(strDate, "四", "4");

     strDate = StringUtil.replaceEx(strDate, "五", "5");

     strDate = StringUtil.replaceEx(strDate, "六", "6");

     strDate = StringUtil.replaceEx(strDate, "七", "7");

     strDate = StringUtil.replaceEx(strDate, "八", "8");

     strDate = StringUtil.replaceEx(strDate, "九", "9");

     return strDate;

   }

 

   public static String getWeekOfDate(Date date){

       return getWeekOfDate(date, 1);

   }

 

   public static String getWeekOfDate(Date date,int Type) { 

       String[] weekDaysName = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; 

       String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" }; 

       Calendar calendar = Calendar.getInstance(); 

       calendar.setTime(date); 

       int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; 

       switch (Type) {

        case 1:

            return weekDaysName[intWeek]; 

        case 2:

            return weekDaysCode[intWeek]; 

        default:

            return weekDaysCode[intWeek]; 

        }

   } 

 

   public static String toDisplayDateTime(Date date)

   {

     long minite = (System.currentTimeMillis() - date.getTime()) / 60000L;

     if (minite < 60L) {

       return toString(date, "MM-dd") + " " + minite + "分钟前";

     }

     if (minite < 1440L) {

       return toString(date, "MM-dd") + " " + minite / 60L + "小时前";

     }

     return toString(date, "MM-dd") + " " + minite / 1440L + "天前";

   }

 

    @SuppressWarnings("unchecked")

    public static Map format(long ms) {//将毫秒数换算成x天x时x分x秒x毫秒

           Map map = new HashMap<String,Object>();

        int ss = 1000;

        int mi = ss * 60;

        int hh = mi * 60;

        int dd = hh * 24;

 

        long day = ms / dd;

        long hour = (ms - day * dd) / hh;

        long minute = (ms - day * dd - hour * hh) / mi;

        long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

 

        String strDay =  "" + day;

        String strHour =  "" + hour;

        String strMinute =  "" + minute;

        String strSecond = "" + second;

        String strMilliSecond =  "" + milliSecond;

        map.put("day", strDay);

        map.put("hour", strHour);

        map.put("minute", strMinute);

        map.put("second", strSecond);

        map.put("milliSecond", strMilliSecond);

        return map;

    }

 

    public static String getElapsedTime(Map map){

        String time = "";

        if(!"0".equals(map.get("day").toString())){

            time += map.get("day").toString()+"天";

        }

        if(!"0".equals(map.get("hour").toString())){

            time += map.get("hour").toString()+"时";        

        }

        if(!"0".equals(map.get("minute").toString())){

            time += map.get("minute").toString()+"分";

        }

        if(!"0".equals(map.get("second").toString())){

            time += map.get("second").toString()+"秒";        

        }

        if(!"0".equals(map.get("milliSecond").toString())){

            time += map.get("milliSecond").toString()+"毫秒";

        }

 

        System.out.println("map---------------"+map);

        System.out.println("time---------------"+time);

        return time;

    }

 

 

    public static String getCronExpression(String periodType,String period,String minute,String hour,String day,String month){

        StringBuffer sb = new StringBuffer();

         if (periodType.equals("minute")) {

             sb.append("0");

             sb.append(" */");

             sb.append(period);

             sb.append(" * * * ?");

         }else if (periodType.equals("hour")) {

             sb.append("0");

             sb.append(" ");

             sb.append(minute);

             sb.append(" */");

             sb.append(period);

             sb.append(" * * ?");

         }else if (periodType.equals("day")) {

             sb.append("0");

             sb.append(" ");

             sb.append(minute);

             sb.append(" ");

             sb.append(hour);

             sb.append(" */");

             sb.append(period);

             sb.append(" * ?");

         }else if (periodType.equals("month")) {

             sb.append("0");

             sb.append(" ");

             sb.append(minute);

             sb.append(" ");

             sb.append(hour);

             sb.append(" ");

             sb.append(day);

             sb.append(" */");

             sb.append(period);

             sb.append(" ?");

         }

         return sb.toString();

    }

   public static void main(String[] args) {

       Date d1=parseDateTime("2012-1-7", "yyyy-MM-d");

//       Date d2=parseDateTime("2011-1-1", "yyyy-MM-d");

       Date d2=new Date();

 

    log.info(subDateTime(d1, d2, Calendar.DATE, 1));

   }

 }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值