Date Util

package com.chintrip.util;

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 com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

/**
 *
 * 日期处理工具类
 * @author 58tc
 *
 */
public class DateTimeUtil {
    
    public static String FORMAT_DEFAULT_MIN = "yyyyMMddHHmmss";
    public static String FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static String FORMAT_DEFAULT_CH = "yyyy年MM月dd日 HH时mm分ss秒";
    public static String FORMAT_DEFAULT_YMD = "yyyy-MM-dd";
    public static String FORMAT_DEFAULT_YM = "yyyy-MM";
    public static String FORMAT_DEFAULT_HM = "yyyy-MM-dd HH:mm";
    public static BiMap<String,String> planeMonth= HashBiMap.create();//双向map
    public static Map<String,String> sundayMap=new HashMap<String,String>();
    static{
        planeMonth.put("01", "JAN");
        planeMonth.put("02", "FEB");
        planeMonth.put("03", "MAR");
        planeMonth.put("04", "APR");
        planeMonth.put("05", "MAY");
        planeMonth.put("06", "JUN");
        planeMonth.put("07", "JUL");
        planeMonth.put("08", "AUG");
        planeMonth.put("09", "SEP");
        planeMonth.put("10", "OCT");
        planeMonth.put("11", "NOV");
        planeMonth.put("12", "DEC");
    }
    public static Date current(){
        return new Date();
    }
    
    public static String dateToStrOfDefaulfFormat(Date date) {
        return dateToStr(date, FORMAT_DEFAULT);
    }

    public static String dateToStrOfYMDFormat(Date date) {
        return dateToStr(date, FORMAT_DEFAULT_YMD);
    }
    
    public static String dateToStrOfYMDHMFormat(Date date) {
        return dateToStr(date, FORMAT_DEFAULT_HM);
    }

    public static Date strToDateOfDefaulfFormat(String dateStr) {
        if(dateStr==null || dateStr.equals("null") ){
            return null;
        }
        return strToDate(dateStr, FORMAT_DEFAULT);
    }

    public static Date strToDateOfYMDFormat(String dateStr) {
        return strToDate(dateStr, FORMAT_DEFAULT_YMD);
    }
    
    public static Integer[] getYMDHMS(Date date){
        Calendar cal = getCalendar(date);
        return new Integer[] {cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH),
                cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)};
    }
    
    public static Integer getWeekOfMonth(Date date){
        Calendar cal = getCalendar(date);
        
        return  cal.get(Calendar.WEEK_OF_MONTH);
    }
    
    
    /**
     * 取本周周一的日期(周一为一周第一天)
     * @param date
     * @return
     */
    public static Date thisWeekMonday(Date date){
        Calendar cal = getCalendar(date);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    
    
    /**
     * 返回星期数(周日返回7)
     * @param date
     * @return
     */
    public static Integer getDayOfWeek(Date date){
        Calendar cal = getCalendar(date);
        int re= cal.get(Calendar.DAY_OF_WEEK);
        if(re==0){
            return 7;
        }else{
            return re-1;
        }
    }
    
    public static Date addSecond(Date date, int num){
        return add(date, Calendar.SECOND, num);
    }
    
    public static Date addMinute(Date date, int num){
        return add(date, Calendar.MINUTE, num);
    }
    
    public static Date addHour(Date date, int num){
        return add(date, Calendar.HOUR_OF_DAY, num);
    }
    
    public static Date addDay(Date date, int num){
        return add(date, Calendar.DAY_OF_MONTH, num);
    }
    
      public static Date addWorkDay(Date src,int adddays){

            Calendar cal = getCalendar(src);
            boolean holidayFlag = false;
            for (int i = 0; i < adddays; i++)
            {
                //把源日期加一天
                cal.add(Calendar.DAY_OF_MONTH, 1);
                holidayFlag =checkHoliday(cal);
                if(holidayFlag)
                {
                   i--;
                }
            }
            return cal.getTime();

        }
    
      public static boolean checkHoliday(Calendar src){

            boolean result = false;

            //先检查是否是周六周日(有些国家是周五周六)
            if (src.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY|| src.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            {
                return true;
            }
            
            return result;

        }
      
    public static Date addMonth(Date date, int num){
        return add(date, Calendar.MONTH, num);
    }
    
    public static Date add(Date date, int field, int num){
        Calendar cal = getCalendar(date);
        cal.add(field, num);
        return cal.getTime();
    }
    
    public static Date firstOfMonth(Date date){
        Date d= first(date, Calendar.DAY_OF_MONTH);
        d=first(d);
        return d;
    }
    
    public static Date lastOfMonth(Date date){
        Date d=last(date, Calendar.DAY_OF_MONTH);
        d=last(d);
        return d;
    }
    
    public static Date firstOfHour(Date date){
        return first(date, Calendar.HOUR_OF_DAY);
    }
    
    
    public static Date first(Date date, int d){
        if(null == date) return date;
        Calendar cal = getCalendar(date);
        cal.set(d, cal.getMinimum(d));//getActualMinimum
        return cal.getTime();
    }
    
    public static Date first(Date date){
        if(null == date) return date;
        Calendar cal = getCalendar(date);
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        return cal.getTime();
    }
    
    public static Date last(Date date, int d){
        if(null == date) return date;
        Calendar cal = getCalendar(date);
        cal.set(d, cal.getActualMaximum(d));
        return cal.getTime();
    }
    
    public static Date last(Date date){
        if(null == date) return date;
        Calendar cal = getCalendar(date);
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        return cal.getTime();
    }
    
    
    public static Calendar getCalendar(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(date);
        return cal;
    }
    
    /**
     * 日期转换为制定的字符串格式
     * @param date
     * @param format
     * @return
     */
    public static String dateToStr(Date date, String format){
        if(null == date) return "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    
    /**
     * 字符串转换为日期类型
     * @param dateStr
     * @param format
     * @return
     */
    public static Date strToDate(String dateStr, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        //sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**  
     * 计算两日期间相差天数.  
     *   
     *
     * @param d1  
     *            开始日期 日期型  
     * @param d2  
     *            结束日期 日期型  
     * @return long 天数  
     */  
    public static long signDaysBetweenTowDate(Date d1, Date d2) {
        return (d2.getTime() - d1.getTime()) / (3600 * 24 * 1000);   
    }   
    
    public static long minuteBetweenTowDate(Date d1, Date d2) {
        return (d2.getTime() - d1.getTime()) / (60 *  1000);   
    }   
    
    public static String signSecondBetweenTowDate(Date d1, Date d2){
         if(d1==null || d2==null){
             return "";
         }
         String resultDate="";
         long l=d2.getTime()-d1.getTime();
         long day=l/(24*60*60*1000);
         long hour=(l/(60*60*1000)-day*24);
         long min=((l/(60*1000))-day*24*60-hour*60);
         long s=(l/1000-day*24*60*60-hour*60*60-min*60);
         if(day>0){
             resultDate+=day+"天";
         }
         if(hour>0){
             resultDate+=hour+"小时";
         }
         if(min>0){
             resultDate+=min+"分";
         }
         resultDate+=s+"秒";
         return resultDate;
    }
    
    public static int monthBetweenTowDate(Date date1, Date date2) {
          Calendar cal1 =  getCalendar(date2);
          Calendar cal2 =  getCalendar(date1);
          int c =
           (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH)
            - cal2.get(Calendar.MONTH);
          return c;
         }
    
    /**
     * @param dateStr
     * 24DEC12格式日期
     * @return
     */
    public static Date getFromPlaneDate(String dateStr)
    {
        if(StringUtil.empty(dateStr)||dateStr.length()!=7){
            return null;
        }
        String monstr=dateStr.substring(2, 5);
        String newmon=null;
        BiMap<String,String> bimap = planeMonth.inverse();   
        newmon=bimap.get(monstr);
        return strToDate(dateStr.substring(0, 2)+newmon+dateStr.substring(5),"ddMMyy");
    }
    
    /**
     * @param dateStr
     * 30JUN格式日期
     * @return
     */
    public static String getFromPlaneDate1(String dateStr)
    {
        if(StringUtil.empty(dateStr)||dateStr.length()!=5){
            return null;
        }
        String monstr=dateStr.substring(2,5);
        String newmon=null;
        BiMap<String,String> bimap = planeMonth.inverse();   
        newmon=bimap.get(monstr);
        return newmon+"-"+dateStr.substring(0,2);
    }
    
    /**
     * @param dateStr
     * yyyy-MM-dd格式日期
     * @return
     * 30JUN2012
     *
     */
    public static String getPlaneDateFull(String dateStr)
    {
      String newmon = "";
      String daystr = dateStr.substring(8, 10);
      String yearstr = dateStr.substring(2, 4);
      String monstr = dateStr.substring(5, 7);
      newmon=planeMonth.get(monstr);
      return daystr + newmon + yearstr;
    }
    
    /**
     * @param dateStr
     * yyyy-MM-dd格式日期
     * @return
     * 30JUN
     *
     */
    public static String getPlaneDateMin(String dateStr)
    {
      String newmon = "";
      String daystr = dateStr.substring(8, 10);
      String monstr = dateStr.substring(5, 7);
      newmon=planeMonth.get(monstr);
      return daystr + newmon ;
    }
    
    
    public static void main(String[] args){
        
//        System.out.println("::::" + dateToStr(new Date(), FORMAT_DEFAULT_MIN));
//        System.out.println(strToDate("2012-11-01", FORMAT_DEFAULT_YMD).getTime());
//        System.out.println("::::" + dateToStr(addDay(current(), -7), FORMAT_DEFAULT));
//        System.out.println(addDay(new Date(),10));
//        System.out.println("getFromPlaneDate:"+getFromPlaneDate("24DEC12").toLocaleString());
        String aa="30JUN";
        Date now=new Date();
        Date d2=strToDate("2013-05-16 15:00:00", FORMAT_DEFAULT);
        System.out.println( firstOfMonth(now));
//        System.out.println(getDayOfWeek(d1));
//        System.out.println(thisWeekMonday(d1).toLocaleString());
    }
    
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值