博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java的时间操作玩法实例若干
阅读量:4087 次
发布时间:2019-05-25

本文共 21706 字,大约阅读时间需要 72 分钟。

      众所周知,时间日期在业务中十分重要,也是几乎所有开发人员必须处理的。除了一些框架和语言提供的时间日期处理工具之外,一些操时间日期的代码也是广大开发人员必备的。因此我们常常将其作为工具类放在项目中。

     下面是一些时间日期处理的常见方法,可以为开发者使用,亦可为学习者参考。

   

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.commons.lang.StringUtils;/** * time utility * @author */public class DateUtil {	/**	 * Check style rule: utility classes should not have public constructor	 */	private DateUtil() {	}	// Time Format	private static final String FORMAT_YYYY_MM_DD_T_HH_MM_SS = "yyyy-MM-dd'T'HH:mm:ss";	private static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";	/**	 * 获取指定日期的所在周的第一天的日期 	 * @param date	 * @return	 */	public static Date getFirstDayOfWeek(Date date) {		Calendar c = new GregorianCalendar();		c.setFirstDayOfWeek(Calendar.MONDAY);		c.setTime(date);		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());		return c.getTime();	}	/**	 * 取得指定日期所在周的最后一天	 * 	 * @param date	 * @return	 */	public static Date getLastDayOfWeek(Date date) {		Calendar c = new GregorianCalendar();		c.setFirstDayOfWeek(Calendar.MONDAY);		c.setTime(date);		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday		return c.getTime();	}	/**	 * Return default datePattern (yyyy-MM-dd)	 * 	 * @return a string representing the date pattern on the UI	 */	public static String getDatePattern() {		String defaultDatePattern = FORMAT_YYYY_MM_DD;		return defaultDatePattern;	}	public static String getDateTimePattern() {		return DateUtil.getDatePattern() + " HH:mm:ss.S";	}	/**	 * Clear time, only keep yyyy-MM-dd 清除时分秒,只保存年月日	 * 	 * @return	 */	public static Date getCleanDate(Date date) {		Date cleanDate = null;		try {			String strDate = DateUtil.getDate(date);			cleanDate = DateUtil.convertStringToDate(strDate);		} catch (ParseException e) {			e.printStackTrace();		}		return cleanDate;	}	/**	 * This method attempts to convert date to format yyyy-MM-dd.	 * 	 * @param aDate	 *            date from database as a string	 * @return formatted string for the ui	 */	public static String getDate(Date aDate) {		SimpleDateFormat df;		String returnValue = "";		if (aDate != null) {			df = new SimpleDateFormat(getDatePattern());			returnValue = df.format(aDate);		}		return (returnValue);	}	/**	 * This method generates a string representation of a date/time in the	 * format you specify on input	 * 	 * @param aMask	 *            the date pattern the string is in	 * @param strDate	 *            a string representation of a date	 * @return a converted Date object	 * @throws ParseException	 *             when String doesn't match the expected format	 * @see java.text.SimpleDateFormat	 */	public static Date convertStringToDate(String aMask, String strDate)			throws ParseException {		if (strDate == null) {			return null;		}		SimpleDateFormat df;		Date date;		df = new SimpleDateFormat(aMask);		try {			date = df.parse(strDate);		} catch (ParseException pe) {			throw new ParseException(pe.getMessage(), pe.getErrorOffset());		}		return (date);	}	/**	 * This method returns the current date time in the format: MM/dd/yyyy	 * HH:MM:SS	 * 	 * @param theTime	 *            the current time	 * @return the current date/time	 */	public static String getTimeNow(Date theTime) {		return getDateTime(FORMAT_YYYY_MM_DD_T_HH_MM_SS, theTime);	}	/**	 * This method returns the current date with clean time: yyyy-MM-dd	 * 	 * @return the current date	 * @throws ParseException	 *             when String doesn't match the expected format	 */	public static Calendar getToday() {		Date today = currentDateTime();		SimpleDateFormat df = new SimpleDateFormat(getDatePattern());		// This seems like quite a hack (date -> string -> date),		// but it works ;-)		String todayAsString = df.format(today);		Calendar cal = new GregorianCalendar();		try {			cal.setTime(convertStringToDate(todayAsString));		} catch (ParseException e) {			throw new RuntimeException(					"unexpcepted exception, should not happen", e);		}		return cal;	}	public static Date currentDateTime() {		return new Date();	}	/** 返回今天的日期,不带时分秒 */	public static Date currentDate() {		try {			return convertStringToDate(convertDateToString(new Date()));		} catch (ParseException e) {			e.printStackTrace();		}		return null;	}	/** 返回不带时分秒的日期 */	public static Date cleanTimeDate(Date date) {		try {			return convertStringToDate(convertDateToString(date));		} catch (Exception e) {			e.printStackTrace();		}		return null;	}	/**	 * This method generates a string representation of a date's date/time in	 * the format you specify on input	 * 	 * @param aMask	 *            the date pattern the string is in	 * @param aDate	 *            a date object	 * @return a formatted string representation of the date	 * @see java.text.SimpleDateFormat	 */	public static String getDateTime(String aMask, Date aDate) {		SimpleDateFormat df = null;		String returnValue = "";		if (aDate == null) {			System.out.println("aDate is null!");		} else {			df = new SimpleDateFormat(aMask);			returnValue = df.format(aDate);		}		return (returnValue);	}	public static Date convertDateToDate(String aMask, Date date) {		if (date == null) {			System.out.println("date is null!");			return null;		}		String createTime = DateUtil.getDateTime(aMask, date);		try {			return convertStringToDate(aMask, createTime);		} catch (ParseException e) {			e.printStackTrace();			return null;		}	}	/**	 * This method generates a string representation of a date based on the	 * System Property 'dateFormat' in the format you specify on input	 * 	 * @param aDate	 *            A date to convert	 * @return a string representation of the date	 */	public static String convertDateToString(Date aDate) {		return getDateTime(getDatePattern(), aDate);	}	/**	 * This method converts a String to a date using the datePattern	 * 	 * @param strDate	 *            the date to convert (in format MM/dd/yyyy)	 * @return a date object	 * @throws ParseException	 *             when String doesn't match the expected format	 */	public static Date convertStringToDate(final String strDate)			throws ParseException {		return convertStringToDate(getDatePattern(), strDate);	}	/**	 * 取一天的开始	 * 	 * @param aMask	 * @param strDate	 * @return	 * @throws ParseException	 */	public static Date getDaysBegin(String aMask, String strDate) {		Date date = null;		try {			date = convertStringToDate(aMask, strDate);			date = daysBegin(date);		} catch (ParseException e) {			e.printStackTrace();		}		return date;	}	public static Date daysBegin(Date date) {		Calendar cal = new GregorianCalendar();		cal.setTime(date);		cal.set(Calendar.HOUR, 0);		cal.set(Calendar.MINUTE, 0);		cal.set(Calendar.SECOND, 0);		date = cal.getTime();		return date;	}	/**	 * 取一天的结束	 * 	 * @param aMask	 * @param strDate	 * @return	 * @throws ParseException	 */	public static Date getDaysEnd(String aMask, String strDate) {		Date date = null;		try {			date = convertStringToDate(aMask, strDate);			date = daysEnd(date);		} catch (ParseException e) {			e.printStackTrace();		}		return date;	}	public static Date daysEnd(Date date) {		Calendar cal = Calendar.getInstance();		cal.setTime(date);		cal.set(Calendar.HOUR, 23);		cal.set(Calendar.MINUTE, 59);		cal.set(Calendar.SECOND, 59);		date = cal.getTime();		return date;	}	/** 获得每个月的第一天是星期几 */	public static int getWeek(final int y, final int m) {		Calendar cal = Calendar.getInstance();// 获得当前日期时间的方法		cal.set(Calendar.YEAR, y); // 设置改为你输入的年		cal.set(Calendar.MONTH, m - 1);		cal.set(Calendar.DATE, 1);		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;// 获得每个月第一天是星期几		return (w);// 返回星期几	}	/** 获得每个月天数 */	public static int getDay(final int y, final int m) {		int day;		if ((y % 100 == 0) || (y % 4 == 0 && y % 100 != 0)) {			int[] days = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };			day = days[m - 1];		} else {			int[] days1 = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };			day = days1[m - 1];		}		return day;	}	/**	 * 获取一段时间内的天	 * 	 * @param ksrq	 *            格式:2012-05-01	 * @param jsrq	 *            格式:2012-06-01	 * @return	 */	@SuppressWarnings("static-access")	public static List
getDays(String ksrq, String jsrq) { List
list = new ArrayList
(); try { Date d_ksrq = convertStringToDate(FORMAT_YYYY_MM_DD, ksrq); Date d_jsrq = convertStringToDate(FORMAT_YYYY_MM_DD, jsrq); list.add(getDateTime(FORMAT_YYYY_MM_DD, d_ksrq)); while (d_jsrq.after(d_ksrq)) { Calendar cal = Calendar.getInstance(); cal.setTime(d_ksrq); cal.add(cal.DATE, 1); d_ksrq = cal.getTime(); list.add(getDateTime(FORMAT_YYYY_MM_DD, d_ksrq)); } } catch (ParseException e) { e.printStackTrace(); } return list; } /** * 获取一段时间内的天 * * @param ksrq * 格式:2012-05-01 * @param jsrq * 格式:2012-06-01 * @return */ @SuppressWarnings("static-access") public static List
getDays(Date d_ksrq, Date d_jsrq) { List
list = new ArrayList
(); try { list.add(getDateTime(FORMAT_YYYY_MM_DD, d_ksrq)); while (d_jsrq.after(d_ksrq)) { Calendar cal = Calendar.getInstance(); cal.setTime(d_ksrq); cal.add(cal.DATE, 1); d_ksrq = cal.getTime(); list.add(getDateTime(FORMAT_YYYY_MM_DD, d_ksrq)); } } catch (Exception e) { e.printStackTrace(); } return list; } /** * 获取一段时间内的天 * * @param ksrq * 格式:2012-05-01 * @param jsrq * 格式:2012-06-01 * @return List
*/ @SuppressWarnings("static-access") public static List
getDayList(Date d_ksrq, Date d_jsrq) { List
list = new ArrayList
(); try { list.add(d_ksrq); while (d_jsrq.after(d_ksrq)) { Calendar cal = Calendar.getInstance(); cal.setTime(d_ksrq); cal.add(cal.DATE, 1); d_ksrq = cal.getTime(); list.add(d_ksrq); } } catch (Exception e) { e.printStackTrace(); } return list; } /** 根据一段日期获取其中所有星期几的日期,星期以1~7表示 */ public static List
getWeekDay(String dateFromStr, String dateToStr, int dayOfWeek) throws Exception { Date sd = DateUtil.convertStringToDate(FORMAT_YYYY_MM_DD, dateFromStr); Date ed = DateUtil.convertStringToDate(FORMAT_YYYY_MM_DD, dateToStr); if (dayOfWeek == 7) { dayOfWeek = 0; } Calendar c = Calendar.getInstance(); c.setTime(sd); int day = c.get(Calendar.DAY_OF_WEEK) - 1; List
tmp = new ArrayList
(); if (day != dayOfWeek) { int dif = dayOfWeek < day ? (dayOfWeek - day + 7) : (dayOfWeek - day); c.add(Calendar.DATE, dif); } while (!c.getTime().after(ed)) { tmp.add(DateUtil.getDateTime(FORMAT_YYYY_MM_DD, c.getTime())); c.add(Calendar.DATE, 7); } return tmp; } public static List
getWeekDay(String dateFromStr, String dateToStr, int[] dayOfWeeks) throws ParseException { Date sd = DateUtil.convertStringToDate(FORMAT_YYYY_MM_DD, dateFromStr); Date ed = DateUtil.convertStringToDate(FORMAT_YYYY_MM_DD, dateToStr); Set
daySet = new HashSet
(); for (int i = 0; i < dayOfWeeks.length; i++) { if (dayOfWeeks[i] == 7) { daySet.add(0); } else if (dayOfWeeks[i] < 0 || dayOfWeeks[i] > 6) { continue; } else { daySet.add(dayOfWeeks[i]); } } Calendar start = Calendar.getInstance(); start.setTime(sd); List
tmp = new ArrayList
(); while (!start.getTime().after(ed)) { if (daySet.contains(start.get(Calendar.DAY_OF_WEEK) - 1)) { tmp.add(DateUtil.getDateTime(FORMAT_YYYY_MM_DD, start.getTime())); } start.add(Calendar.DATE, 1); } return tmp; } public static List
getWeekDay(Date sd, Date ed, int[] dayOfWeeks) throws ParseException { Set
daySet = new HashSet
(); for (int i = 0; i < dayOfWeeks.length; i++) { if (dayOfWeeks[i] == 7) { daySet.add(0); } else if (dayOfWeeks[i] < 0 || dayOfWeeks[i] > 6) { continue; } else { daySet.add(dayOfWeeks[i]); } } Calendar start = Calendar.getInstance(); start.setTime(sd); List
tmp = new ArrayList
(); while (!start.getTime().after(ed)) { if (daySet.contains(start.get(Calendar.DAY_OF_WEEK) - 1)) { tmp.add(DateUtil.getDateTime(FORMAT_YYYY_MM_DD, start.getTime())); } start.add(Calendar.DATE, 1); } return tmp; } public static List
getMonthWeekDay(String month, int[] dayOfWeeks) throws ParseException { Date startDate = DateUtil.convertStringToDate(month); Calendar first = Calendar.getInstance(); first.setTime(startDate); first.set(Calendar.DAY_OF_MONTH, 1); int maxDays = first.getActualMaximum(Calendar.DAY_OF_MONTH); Calendar end = Calendar.getInstance(); end.setTime(startDate); end.set(Calendar.DAY_OF_MONTH, maxDays); return getWeekDay(DateUtil.getDate(first.getTime()), DateUtil.getDate(end.getTime()), dayOfWeeks); } public static List
getMonthWeekDay(String[] months, int[] dayOfWeeks) throws ParseException { List
result = new ArrayList
(); for (String month : months) { if (StringUtils.isNotBlank(month)) { result.addAll(getMonthWeekDay(month, dayOfWeeks)); } } return result; } /** * 计算两个日期之间的天数 * * @param startTime * @param endTime * @param format * @return */ public static Integer dateDiff(Date startTime, Date endTime) { try { Float nd = new Float(1000 * 24 * 60 * 60);// 一天的毫秒数 Float diff = new Float(endTime.getTime() - startTime.getTime()); Float day = diff / nd;// 计算差多少天 return day.intValue(); } catch (Exception e) { return 0; } } /** * 两个日期之间相差多少天 默认格式yyyy-MM-dd */ public static Long dateDiff(String startTime, String endTime) { return dateDiff(startTime, endTime, getDatePattern()); } /** * 两个日期之间相差多少天 * * @param startTime * @param endTime * @param format * @return */ public static Long dateDiff(String startTime, String endTime, String format) { // 按照传入的格式生成一个simpledateformate对象 SimpleDateFormat sd = new SimpleDateFormat(format); long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数 // long nh = 1000 * 60 * 60;// 一小时的毫秒数 // long nm = 1000 * 60;// 一分钟的毫秒数 // long ns = 1000;// 一秒钟的毫秒数 long diff; try { // 获得两个时间的毫秒时间差异 diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime(); long day = diff / nd;// 计算差多少天 // long hour = diff % nd / nh;// 计算差多少小时 // long min = diff % nd % nh / nm;// 计算差多少分钟 // long sec = diff % nd % nh % nm / ns;// 计算差多少秒 return day; } catch (ParseException e) { e.printStackTrace(); } return null; } public static Date[] getCalendar(Date date) { Calendar first = Calendar.getInstance(); first.setTime(date); first.set(Calendar.DAY_OF_MONTH, 1); first.add(Calendar.DATE, -(first.get(Calendar.DAY_OF_WEEK) - 1)); Calendar last = Calendar.getInstance(); last.setTime(date); last.set(Calendar.DAY_OF_MONTH, getDay(last.get(Calendar.YEAR), last.get(Calendar.MONTH) + 1)); last.add(Calendar.DATE, (7 - last.get(Calendar.DAY_OF_WEEK))); Date[] dates = new Date[] { first.getTime(), last.getTime() }; return dates; } public static Date[] get42DayCalendar(Date date) { Calendar first = Calendar.getInstance(); first.setTime(date); first.set(Calendar.DAY_OF_MONTH, 1); first.add(Calendar.DATE, -(first.get(Calendar.DAY_OF_WEEK) - 1)); Calendar last = Calendar.getInstance(); last.setTime(first.getTime()); last.add(Calendar.DATE, 41); Date[] dates = new Date[] { first.getTime(), last.getTime() }; return dates; } public static int getDayOfYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.DAY_OF_YEAR); } public static Date nextDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DATE, +1); return c.getTime(); } public static Date previousMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MONTH, -1); return c.getTime(); } public static Date nextMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MONTH, +1); return c.getTime(); } public static Date lastMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MONTH, -1); return c.getTime(); } public static Date cloneDate(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.getTime(); } public static String convertDateTimeToString(Date date) { String pattern = FORMAT_YYYY_MM_DD + " HH:mm:ss"; return getDateTime(pattern, date); } /** * 生成从开始日期到结束日期内的日期列表,如果星期标识不为空的时候,只包含flag为true的日期 * * @param startDateStr * @param endDateStr * @return */ @SuppressWarnings("rawtypes") public static List
createDateList(String startDateStr, String endDateStr, Map weekFlag) { List
dateList = new ArrayList
(); try { Date startDate = convertStringToDate(FORMAT_YYYY_MM_DD_T_HH_MM_SS, startDateStr); Date endDate = convertStringToDate(FORMAT_YYYY_MM_DD_T_HH_MM_SS, endDateStr); int days = dateDiff(startDate, endDate); final String weekNames[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; for (int i = 0; i <= days; i++) { Calendar c = Calendar.getInstance(); c.setTime(startDate); c.add(Calendar.DATE, i); Date varDate = c.getTime(); if (weekFlag != null) { String weekName = weekNames[c.get(Calendar.DAY_OF_WEEK) - 1]; boolean include = (Boolean) weekFlag.get(weekName); if (include) { dateList.add(varDate); } } else { dateList.add(varDate); } } } catch (ParseException e) { e.printStackTrace(); } return dateList; } /** * 获取当前年月+11个月 * * */ public static List
getOneYearDate() { List
list = new ArrayList
(); Calendar c = Calendar.getInstance(); list.add(c.getTime()); for (int i = 0; i < 11; i++) { c.add(Calendar.MONTH, +1); list.add(c.getTime()); } return list; } /** * 取一年后的日期 * * @return */ public static Date getAfterYearTime() { Date date = currentDateTime(); long afterTime = (date.getTime() / 1000) + 60 * 60 * 24 * 365; date.setTime(afterTime * 1000); return date; } public static List
splitTimeByDays(Date start, Date end, int days) { return splitTimeByHours(start, end, 24 * days); } public static List
splitTimeByHours(Date start, Date end, int hours) { List
dl = new ArrayList
(); while (start.compareTo(end) < 0) { Date _end = addHours(start, hours); if (_end.compareTo(end) > 0) { _end = end; } Date[] dates = new Date[] { (Date) start.clone(), (Date) _end.clone() }; dl.add(dates); start = _end; } return dl; } public static Date addMinutes(Date date, int amount) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MINUTE, amount); return c.getTime(); } /** * * @param date * @param hhmm * @return */ public static Date setDateHHmmss(Date date, int hour, int minute, int second) { Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); return c.getTime(); } public static Date addHours(Date date, int amount) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.HOUR_OF_DAY, amount); return c.getTime(); } public static Date addDays(Date date, int amount) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH, amount); return c.getTime(); } public static Date addMonths(Date date, int amount) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MONTH, amount); return c.getTime(); } /** * 获取指定时间所在月的最后一天 * @return */ public static String getMonthLastDay(Date start) { Calendar calendar = Calendar.getInstance(); calendar.setTime(start); int endday = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DATE, endday); return convertDateToString(calendar.getTime()); } /** * 根据年和月获取所在月的最后一天的日期 * * @return */ public static String getMonthLastDay(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, 1); int endday = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DATE, endday); return convertDateToString(calendar.getTime()); } /** * 根据年和月获取所在月的最后一天的日期 * * @return */ public static Date getMonthLastDayByYM(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, 1); int endday = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DATE, endday); return calendar.getTime(); } /** * 判断日期是否是周末 * * @param date * @return */ public static boolean isWeekend(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.setFirstDayOfWeek(Calendar.MONDAY); int week = calendar.get(Calendar.DAY_OF_WEEK); //SUNDAY is 1,SATURDAY is 7. //if (week == 1 || week == 7) if (week == Calendar.SATURDAY || week == Calendar.SUNDAY) { return true; } else { return false; } } /** * 一天的开始 * * @param date * @return */ public static Date getStartOfDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); return calendar.getTime(); } /** * 判断日期是星期几 2:星期一;3:星期二...7:星期六;1:星期天 */ public static int getWeekday(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int weekday = calendar.get(Calendar.DAY_OF_WEEK); return weekday; } /** * 判断日期是否在某一时间段内,包含起始天 * * @param start * @param end * @param target * @return 存在返回true */ public static boolean judge(Date start, Date end, Date target) { if (target.before(start)) { return false; } if (target.after(end)) { return false; } return true; } public static Date addSecond(Date date, int amount) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.SECOND, amount); return c.getTime(); }}

      下面是一些简单的测试输出

      

import java.util.Date;/** * time utility test * @author  */public class DateUtilTest {	public static void main(String[] args) {		//System.out.println(":"+DateUtil);		System.out.println("当前日期所在周的第一天的日期是:"+DateUtil.convertDateTimeToString(DateUtil.getFirstDayOfWeek(new Date())));		System.out.println("当前日期所在周的最后一天的日期是:"+DateUtil.convertDateTimeToString(DateUtil.getLastDayOfWeek(new Date())));		System.out.println("明年这个月的最后一天:"+DateUtil.getMonthLastDay(DateUtil.getAfterYearTime()));		System.out.println("根据年月获取当月的最后一天:"+DateUtil.getMonthLastDay(2008, 2));		System.out.println("根据年月获取当月的最后一天:"+DateUtil.getMonthLastDay(2009, 2));		System.out.println("今天"+((DateUtil.isWeekend(new Date())==true)?"是":"不是")+"周末.");	}}

你可能感兴趣的文章
【JAVA数据结构】先进先出队列
查看>>
String类的intern方法随笔
查看>>
【泛型】一个简易的对象间转换的工具类(DO转VO)
查看>>
1.随机函数,计算机运行的基石
查看>>
MouseEvent的e.stageX是Number型,可见as3作者的考虑
查看>>
移植Vim配色方案到Eclipse
查看>>
谈谈加密和混淆吧[转]
查看>>
TCP的几个状态对于我们分析所起的作用SYN, FIN, ACK, PSH,
查看>>
网络游戏客户端的日志输出
查看>>
关于按钮的mouseOver和rollOver
查看>>
Netty框架
查看>>
Socket经验记录
查看>>
对RTMP视频流进行BitmapData.draw()出错的解决办法
查看>>
FMS 客户端带宽计算、带宽限制
查看>>
SecurityError Error 2148 SWF 不能访问本地资源
查看>>
Qt 静态编译后的exe太大, 可以这样压缩.
查看>>
3D游戏常用技巧Normal Mapping (法线贴图)原理解析——基础篇
查看>>
乘法逆元
查看>>
Objective-C 基础入门(一)
查看>>
Objective-C 基础入门(三) 读写文件与回调
查看>>