本文主要是介绍项目经验:自己写的根据时段得到日期的CalendarUtil,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在做水质评价统计的时候,根据前台页面返回的最近一天、最近一星期、最近一月、最近一年计算出日期。
统计时段select控件:
CalendarUtil类代码:
package com.richway.waterevaluate.util;import java.util.Calendar;
import java.util.Date;/*** @author lej*时间处理工具* 2014-5-30下午1:43:02*/
public class CalendarUtil {/*** 得到当天的0:0:0:000*/public static Date getTodayStart(){Calendar calendar = Calendar.getInstance(); //将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND,0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime(); }/*** 得到当天第二天的0:0:0:000*/public static Date getTomorrowStart(){Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, 1);//将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND,0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime(); }/*** 得到当天所在的星期的第一天的0:0:0:000*/public static Date getThisWeekStart(){Calendar calendar = Calendar.getInstance(); // 直接取周一calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND,0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime();}/*** 得到当天所在的星期的下一个星期第一天的0:0:0:000*/public static Date getNextWeekStart(){Calendar calendar = Calendar.getInstance(); // 取下周calendar.add(Calendar.WEEK_OF_YEAR, 1);// 取下周一calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND,0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime();}/*** 得到当天所在的月份的第一天的0:0:0:000*/public static Date getThisMonthStart(){Calendar calendar = Calendar.getInstance(); // 得到当天是这月的第几天int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);// 减去dayOfMonth,得到第一天的日期,因为Calendar用0代表每月的第一天,所以要减一calendar.add(Calendar.DAY_OF_MONTH, -(dayOfMonth-1));//将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND,0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime();}/*** 得到当天所在的月份的下一个月第一天的0:0:0:000*/public static Date getNextMonthStart(){Calendar calendar = Calendar.getInstance(); // 得到当天是这月的第几天int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);// 减去dayOfMonth,得到第一天的日期,因为Calendar用0代表每月的第一天,所以要减一calendar.add(Calendar.DAY_OF_MONTH, -(dayOfMonth-1));//得到下一个月calendar.add(Calendar.MONTH, 1);//将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND,0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime();}/*** 得到当天所在的年份的第一天的0:0:0:000*/public static Date getThisYearStart(){Calendar calendar = Calendar.getInstance(); // 得到当天是今年的第几天int dayOfYEAR = calendar.get(Calendar.DAY_OF_YEAR);// 减去dayOfYEAR,得到第一天的日期,因为Calendar用0代表每月的第一天,所以要减一calendar.add(Calendar.DAY_OF_YEAR, -(dayOfYEAR-1));//将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND,0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime();}/*** 得到当天所在的年份的下一年第一天的0:0:0:000*/public static Date getNextYearStart(){Calendar calendar = Calendar.getInstance(); // 得到当天是今年的第几天int dayOfYEAR = calendar.get(Calendar.DAY_OF_YEAR);// 减去dayOfYEAR,得到第一天的日期,因为Calendar用0代表每月的第一天,所以要减一calendar.add(Calendar.DAY_OF_YEAR, -(dayOfYEAR-1));//得到下一个年calendar.add(Calendar.YEAR, 1);//将小时至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //将分钟至0 calendar.set(Calendar.MINUTE, 0); //将秒至0 calendar.set(Calendar.SECOND,0); //将毫秒至0 calendar.set(Calendar.MILLISECOND, 0);return calendar.getTime();}/*** 得到两个时间对应年份的间隔 */public static int getYearsBetween(Date startTime,Date endTime){Calendar calendar = Calendar.getInstance(); calendar.setTime(startTime);int year1 = calendar.get(Calendar.YEAR); //获取年calendar.setTime(endTime);int year2 = calendar.get(Calendar.YEAR); //获取年int between = year2-year1;return between;}// 当天开始Fri May 30 00:00:00 CST 2014
// 第二天开始Sat May 31 00:00:00 CST 2014
// 星期开始Mon May 26 00:00:00 CST 2014
// 下星期开始Mon Jun 02 00:00:00 CST 2014
// 当月开始Thu May 01 00:00:00 CST 2014
// 下月开始Sun Jun 01 00:00:00 CST 2014
// 当年开始Wed Jan 01 00:00:00 CST 2014
// 下年开始Thu Jan 01 00:00:00 CST 2015}
这篇关于项目经验:自己写的根据时段得到日期的CalendarUtil的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!