Java 项目经常使用的日期方法class

Java 项目经常使用的日期方法class

package com.school.util;

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

/**
 * 提供一些跟日期相关的通用方法。

* @author nimade */ public abstract class SchoolDateUtils { /** * “今日”的開始时间。 */ public static Date getTodayStart() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND)); return cal.getTime(); } /** * “今日”的结束时间。

*/ public static Date getTodayEnd() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); return cal.getTime(); } /** * “昨日”的開始时间。 */ public static Date getYesterdayStart() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND)); // 昨日 cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } /** * “昨日”的结束时间。

*/ public static Date getYesterdayEnd() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); // 昨日 cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } /** * 近期几天前的时间。

*/ public static Date getRecentDateStart(Integer day) { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); // 曾经的时间 cal.add(Calendar.DAY_OF_MONTH, -day);//由当前时间。推算出几天前的时间 return cal.getTime(); } /** * 未來几天前的时间。 */ public static Date getFutureDateStart(Integer day) { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); // 曾经的时间 cal.add(Calendar.DAY_OF_MONTH, +day);//由当前时间。推算出將來的时间 return cal.getTime(); } /** * “前天”的開始时间。 */ public static Date getQianTianStart() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND)); // 前天 cal.add(Calendar.DAY_OF_MONTH, -2); return cal.getTime(); } /** * “前天”的结束时间。 */ public static Date getQianTianEnd() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); // 前天 cal.add(Calendar.DAY_OF_MONTH, -2); return cal.getTime(); } /** * “本周”的開始时间。 */ public static Date getThisWeekStart() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // 周日 cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND)); return cal.getTime(); } /** * “本周”的结束时间。 */ public static Date getThisWeekEnd() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); // 周六 cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); return cal.getTime(); } /** * “上周”的開始时间。 */ public static Date getLastWeekStart() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // 周日 cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND)); // 上周 cal.add(Calendar.DATE, -7); return cal.getTime(); } /** * “上周”的结束时间。

*/ public static Date getLastWeekEnd() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); // 周六 cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); // 上周 cal.add(Calendar.DATE, -7); return cal.getTime(); } /** * “本月”的開始时间。

*/ public static Date getThisMonthStart() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND)); return cal.getTime(); } /** * “本月”的结束时间。 */ public static Date getThisMonthEnd() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); return cal.getTime(); } /** * “上月”的開始时间。 */ public static Date getLastMonthStart() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND)); // 上月 cal.add(Calendar.MONTH, -1); return cal.getTime(); } /** * “上月”的结束时间。

*/ public static Date getLastMonthEnd() { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, now.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); // 上月 cal.add(Calendar.MONTH, -1); return cal.getTime(); } /****************依据传进来的数字,获取某一年。某一个月的開始日期,与结束日期*********************/ /** * “依据传进来的数字”的開始时间。 */ public static Date getNumMonthStart(Integer year,Integer month) { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); java.util.Date date; try { date = new SimpleDateFormat("yyyy-MM-dd").parse(year.toString()+"-"+month.toString()+"-"+"01"); now.setTime(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month-1); cal.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND)); return cal.getTime(); } /** * "依据传进来的数字"的结束时间。 */ public static Date getNumMonthEnd(Integer year,Integer month) { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); java.util.Date date; try { date = new SimpleDateFormat("yyyy-MM-dd").parse(year.toString()+"-"+month.toString()+"-"+"01"); now.setTime(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month-1); cal.set(Calendar.DAY_OF_MONTH, now.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); return cal.getTime(); } /*** * 获取指定月份的所用日期 */ /** * "依据传进来的数字"的结束时间。

*/ public static List<String> getNumMonthAll(Integer year,Integer month) { Calendar now = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); List<String> monthOfDayList=new ArrayList<String>(); java.util.Date date; try { date = new SimpleDateFormat("yyyy-MM-dd").parse(year.toString()+"-"+month.toString()+"-"+"01"); now.setTime(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month-1); cal.set(Calendar.DAY_OF_MONTH, now.getActualMaximum(Calendar.DAY_OF_MONTH)); int day=now.getActualMaximum(Calendar.DAY_OF_MONTH);//获取指定月份的最大天数 for(int i=1;i<=day;i++){ cal.set(Calendar.DAY_OF_MONTH, i); cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE)); cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND)); Date dateC = cal.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); monthOfDayList.add(sdf.format(dateC)); } return monthOfDayList; } }



posted on 2017-06-27 20:54  ljbguanli  阅读(201)  评论(0编辑  收藏  举报