java DateUtil 工具类 总结

原创 2017年01月17日 13:20:18

项目中 比较常用的 工具类--日历,封装一下,可以 获取 比如:本周的起始点,本月的起始点,本季度的起始点等

DateUtil.class


主要用到的方法有:

Calendar.add() : 对当前 日历进行 加减 ;SimpleDateFormat.format()  :日期格式化

eg1:

Calendar c = Calendar.getInstance();
try {
int  weekday = c.get(Calendar.DAY_OF_WEEK) - 2 ;
int gapDay =  weekday >= 0 ? weekday : 6;
c.add(Calendar.DATE, -gapDay);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();

直接上代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat longHourSdf  = new SimpleDateFormat("yyyy-MM-dd HH");
	private static SimpleDateFormat longSdf  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获得本周的第一天,周一
	 * 
	 * @return
	 */
	public static Date getCurrentWeekDayStartTime() {
		Calendar c = Calendar.getInstance();
		try {
			int  weekday = c.get(Calendar.DAY_OF_WEEK) - 2 ;
			int gapDay =  weekday >= 0 ? weekday : 6;
			c.add(Calendar.DATE, -gapDay);
			c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}

	/**
	 * 获得本周的最后一天,周日
	 * 
	 * @return
	 */
	public static Date getCurrentWeekDayEndTime() {
		Calendar c = Calendar.getInstance();
		try {
			int weekday = 8 - c.get(Calendar.DAY_OF_WEEK);
			int gapDay = weekday <= 6 ? weekday : 0;
			c.add(Calendar.DATE, gapDay);
			c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}

	/**
	 * 获得本天的开始时间,即2012-01-01 00:00:00
	 * 
	 * @return
	 */
	public static Date getCurrentDayStartTime() {
		Date now = new Date();
		try {
			now = shortSdf.parse(shortSdf.format(now));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获得本天的结束时间,即2012-01-01 23:59:59
	 * 
	 * @return
	 */
	public static Date getCurrentDayEndTime() {
		Date now = new Date();
		try {
			now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获得本小时的开始时间,即2012-01-01 23:59:59
	 * 
	 * @return
	 */
	public static Date getCurrentHourStartTime() {
		Date now = new Date();
		try {
			now = longHourSdf.parse(longHourSdf.format(now));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获得本小时的结束时间,即2012-01-01 23:59:59
	 * 
	 * @return
	 */
	public static Date getCurrentHourEndTime() {
		Date now = new Date();
		try {
			now = longSdf.parse(longHourSdf.format(now) + ":59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}
	/**
	 * 获得上个月第一天的开始时间
	 * @return
	 */
	public static Date getPreviousMonthFirst(){
       Calendar c = Calendar.getInstance();
       Date d=null;   
	    try {
	       c.set(5, 1);
	 	    c.add(2, -1);
			d=shortSdf.parse(shortSdf.format(c.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	    return d;
	}
	/**
	 * 获得上个月最后一天结束时间
	 * @return
	 */
	public static Date getPreviousMonthLast(){
		  Calendar c = Calendar.getInstance();
		  Date d=null;	   
		    try {
		    	 c.add(2, -1);
				  c.set(5, 1);
				  c.roll(5, -1);
				d = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
			} catch (ParseException e) {
				e.printStackTrace();
			}
		    return d;
	}
	

	/**
	 * 获得本月的开始时间,即2012-01-01 00:00:00
	 * 
	 * @return
	 */
	public static Date getCurrentMonthStartTime() {
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.DATE, 1);
			now = shortSdf.parse(shortSdf.format(c.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前月的结束时间,即2012-01-31 23:59:59
	 * 
	 * @return
	 */
	public static Date getCurrentMonthEndTime() {
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.DATE, 1);
			c.add(Calendar.MONTH, 1);
			c.add(Calendar.DATE, -1);
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前年的开始时间,即2012-01-01 00:00:00
	 * 
	 * @return
	 */
	public static Date getCurrentYearStartTime() {
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.MONTH, 0);
			c.set(Calendar.DATE, 1);
			now = shortSdf.parse(shortSdf.format(c.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前年的结束时间,即2012-12-31 23:59:59
	 * 
	 * @return
	 */
	public static Date getCurrentYearEndTime() {
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.MONTH, 11);
			c.set(Calendar.DATE, 31);
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前季度的开始时间,即2012-01-1 00:00:00
	 * 
	 * @return
	 */
	public static Date getCurrentQuarterStartTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 3)
				c.set(Calendar.MONTH, 0);
			else if (currentMonth >= 4 && currentMonth <= 6)
				c.set(Calendar.MONTH, 3);
			else if (currentMonth >= 7 && currentMonth <= 9)
				c.set(Calendar.MONTH, 6);
			else if (currentMonth >= 10 && currentMonth <= 12)
				c.set(Calendar.MONTH, 9);
			c.set(Calendar.DATE, 1);
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前季度的结束时间,即2012-03-31 23:59:59
	 * 
	 * @return
	 */
	public static Date getCurrentQuarterEndTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 3) {
				c.set(Calendar.MONTH, 2);
				c.set(Calendar.DATE, 31);
			} else if (currentMonth >= 4 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 5);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 7 && currentMonth <= 9) {
				c.set(Calendar.MONTH, 8);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 10 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 11);
				c.set(Calendar.DATE, 31);
			}
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}
	/**
     * 得到某年某周的第一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        week = week - 1;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);

        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 得到某年某周的最后一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        week = week - 1;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);
        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);

        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 取得当前日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK,
                      calendar.getFirstDayOfWeek()); // Sunday
        return calendar.getTime();
    }

    /**
     * 取得当前日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
        Date now =null;
        try {
			now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return now;
    }

    /**
     * 取得当前日期所在周的前一周最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfLastWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getLastDayOfWeek(calendar.get(Calendar.YEAR), calendar.get(Calendar.WEEK_OF_YEAR) - 1);
    }

    /**
     * 返回指定日期的月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), 1);
        Date now=null; 
        try {
			now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 00:00:00");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return now;
    }

    /**
     * 返回指定年月的月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month, 1);
        return calendar.getTime();
    }

    /**
     * 返回指定日期的月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        calendar.roll(Calendar.DATE, -1);
        Date now =null;
        try {
			now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return now;
    }

    /**
     * 返回指定年月的月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month, 1);
        calendar.roll(Calendar.DATE, -1);
        Date now =null;
        try {
			now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return now;
    }

    /**
     * 返回指定日期的上个月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfLastMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),
                     calendar.get(Calendar.MONTH) - 1, 1);
        calendar.roll(Calendar.DATE, -1);
        Date now =null;
        try {
			now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return now;
    }

    /**
     * 返回指定日期的季的第一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getFirstDayOfQuarter(Date date) {
       Calendar calendar = Calendar.getInstance();  
       Date d= getFirstDayOfQuarter(calendar.get(Calendar.YEAR),getQuarterOfYear(date));
       calendar.setTime(d);
       Date now=null;
       try {
		d=shortSdf.parse(shortSdf.format(calendar.getTime()));
		now = longSdf.parse(shortSdf.format(d.getTime()) + " 00:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
       return now;
    }

    /**
     * 返回指定年季的季的第一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 1 - 1;
        } else if (quarter == 2) {
            month = 4 - 1;
        } else if (quarter == 3) {
            month = 7 - 1;
        } else if (quarter == 4) {
            month = 10 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getFirstDayOfMonth(year, month);
    }

    /**
     * 返回指定日期的季的最后一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getLastDayOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Date d=getLastDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
        Date now =null;
        try {
			now= longSdf.parse(shortSdf.format(d.getTime()) + " 23:59:59");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return now;
        
    }

    /**
     * 返回指定年季的季的最后一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 3 - 1;
        } else if (quarter == 2) {
            month = 6 - 1;
        } else if (quarter == 3) {
            month = 9 - 1;
        } else if (quarter == 4) {
            month = 12 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        Date d=getLastDayOfMonth(year, month);
        Date now =null;
        try {
			now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return now;
    }

    /**
     * 返回指定日期的上一季的最后一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getLastDayOfLastQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        
        Date d= getLastDayOfLastQuarter(calendar.get(Calendar.YEAR),getQuarterOfYear(date));
        try {
        	calendar.setTime(d);
			d=longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
		} catch (ParseException e) {
			e.printStackTrace();
		}
        return d;
    }

    /**
     * 返回指定年季的上一季的最后一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getLastDayOfLastQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 12 - 1;
        } else if (quarter == 2) {
            month = 3 - 1;
        } else if (quarter == 3) {
            month = 6 - 1;
        } else if (quarter == 4) {
            month = 9 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
       Date d=getLastDayOfMonth(year, month);
       Date now =null;
	     try {
				now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
		 } catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
     return now;
    }

    /**
     * 返回指定日期的季度
     *
     * @param date
     * @return
     */
    public static int getQuarterOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) / 3 + 1;
    }


	/**
	 * 获取前/后半年的开始时间
	 * 
	 * @return
	 */
	public static Date getHalfYearStartTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 0);
			} else if (currentMonth >= 7 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 6);
			}
			c.set(Calendar.DATE, 1);
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;

	}

	/**
	 * 获取前/后半年的结束时间
	 * 
	 * @return
	 */
	public static Date getHalfYearEndTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 5);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 7 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 11);
				c.set(Calendar.DATE, 31);
			}
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}
		public static Date getLastYearStart(){
		 Calendar calendar = Calendar.getInstance();
	      Date date = getCurrentYearStartTime();
	      calendar.setTime(date);
	      calendar.add(Calendar.YEAR, -1);    
	      date = calendar.getTime();
	      return date;
	        
	}
		public static Date getLastYearEnd(){
			 Calendar calendar = Calendar.getInstance();
		      Date date =getCurrentYearEndTime();
		      calendar.setTime(date);
		      calendar.add(Calendar.YEAR, -1);    
		      date = calendar.getTime();
		      return date;
		        
		}
		//获取指定日期的后一天时间
		public static String getSpecifiedDayAfter(String specifiedDay){ 
			Calendar c = Calendar.getInstance(); 
			Date date=null; 
			try { 
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); 
			} catch (ParseException e) { 
			e.printStackTrace(); 
			} 
			c.setTime(date); 
			int day=c.get(Calendar.DATE); 
			c.set(Calendar.DATE,day+1); 

			String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); 
			return dayAfter; 
			} 

	
	/**
	 * 获取开始时间毫秒值
	 */
	public static long getStartTime(String v,long endTime){
		long startTime = 0;
		switch(v){
		case "m5":
			startTime = endTime - 60*1000*5;
			break;
		case "m15":
			startTime = endTime - 60*1000*15;
			break;
		case "m30":	
			startTime = endTime - 60*1000*30;
			break;
		case "h1":	
			startTime = endTime - 60*1000*60;
			break;
		case "h12":	
			startTime = endTime - 60*1000*60*12;
			break;
		case "d1":	
			startTime = endTime - 60*1000*60*24;
			break;
		case "d7":	
			startTime = endTime - 60*1000*60*24*7;
			break;
		case "d15":	
			startTime = endTime - 60*1000*60*24*15;
			break;
		default:startTime = endTime - 60*1000*60*24*30;}
		return startTime;
	}

	public static void main(String[] args) {
			
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

java date 工具类 DateUtil 格式转换

拷贝可用,够大家用了
  • u010913106
  • u010913106
  • 2015年06月29日 19:12
  • 1015

java Date时间格式工具类DateUtil

package com.common; import java.sql.Timestamp; import java.text.DateFormat; import java.text.Parse...
  • w410589502
  • w410589502
  • 2017年01月11日 11:52
  • 3215

DateUtil时间转换工具类

时间格式化工具DateUtil package com.weidai.bwcrm.util; import com.weidai.common.support.lang.util.Asse...
  • u010135118
  • u010135118
  • 2017年09月06日 14:21
  • 171

DateUtil工具类时间戳类型转换

package com.sinosoft.media.sms.util; import java.text.ParseException; import java.text.SimpleDateFor...
  • xuetan121
  • xuetan121
  • 2012年11月26日 10:46
  • 1613

时间处理工具类(DateUtil)

package com.lvmama.comm.utils; import java.text.DateFormat; import java.text.ParseException; im...
  • jakeswang
  • jakeswang
  • 2016年06月23日 15:38
  • 2080

java开发中常用的日期时间工具类 DateUtil

java开发中常会用到的 日期时间工具类。 package org.demo; import java.text.DecimalFormat; import java.text.ParseExce...
  • Liyatao_BeiJing
  • Liyatao_BeiJing
  • 2015年11月20日 18:01
  • 1293

时间段判断工具类InDateUtil

判断时间在某时间段内
  • zrk1000
  • zrk1000
  • 2015年05月06日 20:24
  • 917

DateUtil 日期工具类

package com.ls.util; import java.text.ParseException; import java.text.SimpleDateFormat; import j...
  • yuanquanl
  • yuanquanl
  • 2017年06月06日 16:23
  • 288

Java的DateUtil工具类

public class DateUtil { /**      * 获得一年中的上一周周数      * @return 格式:201801      */     public stat...
  • z5211875
  • z5211875
  • 2018年01月08日 12:02
  • 40

Java 工具类 - 日期 DateUtil

本工具类处理日期的常用操作,极大地减少了一些重复的代码量。包括以下功能 1.格式化日期 2.日期转换 3.日期操作,如加减日期、计算日期差等以下是工具类代码package com.tgb.hz.c...
  • sinat_27403673
  • sinat_27403673
  • 2017年08月17日 17:35
  • 367
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java DateUtil 工具类 总结
举报原因:
原因补充:

(最多只允许输入30个字)