Java的时间操作玩法实例若干

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

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

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<String> getDays(String ksrq, String jsrq) {
		List<String> list = new ArrayList<String>();
		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<String> getDays(Date d_ksrq, Date d_jsrq) {
		List<String> list = new ArrayList<String>();
		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<Date>
	 */
	@SuppressWarnings("static-access")
	public static List<Date> getDayList(Date d_ksrq, Date d_jsrq) {
		List<Date> list = new ArrayList<Date>();
		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<String> 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<String> tmp = new ArrayList<String>();
		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<String> 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<Integer> daySet = new HashSet<Integer>();
		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<String> tmp = new ArrayList<String>();
		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<String> getWeekDay(Date sd, Date ed, int[] dayOfWeeks)
			throws ParseException {
		Set<Integer> daySet = new HashSet<Integer>();
		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<String> tmp = new ArrayList<String>();
		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<String> 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<String> getMonthWeekDay(String[] months, int[] dayOfWeeks)
			throws ParseException {
		List<String> result = new ArrayList<String>();
		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<Date> createDateList(String startDateStr,
			String endDateStr, Map weekFlag) {
		List<Date> dateList = new ArrayList<Date>();
		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<Date> getOneYearDate() {
		List<Date> list = new ArrayList<Date>();
		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<Date[]> splitTimeByDays(Date start, Date end, int days) {
		return splitTimeByHours(start, end, 24 * days);
	}

	public static List<Date[]> splitTimeByHours(Date start, Date end, int hours) {
		List<Date[]> dl = new ArrayList<Date[]>();
		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)?"是":"不是")+"周末.");
	}
}

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

时间: 2024-10-05 04:58:46

Java的时间操作玩法实例若干的相关文章

Java日期时间操作源码示例大全

在研发闲暇时间,将开发过程比较重要的一些代码段做个记录,如下代码内容是关于Java日期时间操作示例大全的代码,应该是对小伙伴们有所用途. 日期类 import java.util.Calendar; public class VeDate { public static Date getNowDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd H

Java日期时间操作的一些方法

1. 获得Calendar实例: Calendar c = Calendar.getInstance(); 2. 定义日期/时间的格式: SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 3. 把日期/时间转换成固定格式,使用SimpleDateFormat的format()方法: String datetime = sdf.format(c.getTime()); 4. 把字符串转换成日期/时间,使用

java日期时间操作

package com.gds.web.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateOpUtil { /**       * @param args       * @throws ParseException        */       public st

营销QQ年收入几十万自动来粉丝终极玩法

时间过的真是快,上半年多已经过去 !上半年一直都是在忙着操作项目!现在已经进入秋天了.感觉天气凉快了不少了! 我一般都是很少时间去看别人的空间别人的微信,其实一直都忙着 忙着就到了晚上.做网络的人都知道,睡觉晚. 晚起!这都是正常不过的!现在各种吸粉方法,真是满天飞!但是我知道,说来说去就那几种玩法而已 腾讯系 百度系 淘宝 还有就是其他各大网站或者BBS了! 其他的基本没有了!!很多做网络的有一个弊端,不断的找神秘的方法!!!不断的想,一分钱不花,就来几百个精准,成交你个几十单.我觉得想象很完

解析新手操作无货源店群,简单暴力玩法完全躺赚,每月稳定收入2000+

2018年的淘宝可谓是大变革,每隔一段时间就有新的规则出现,所以在淘宝行业挣扎求生的人越来越难熬. 最近淘宝针对店群又出了新的应对规则,就在8月1号那天,淘宝官方对于重复铺货模式和裂变的模式店铺做了进一步的打击.那这时我们做无货源店群模式的卖家该何去何从呢?很简单,换种方式迎合淘宝规则,精细化运营精准引流,重新做大做强.这就是最根本的解决办法. 可能有些小白不太清楚什么是无货源店群模式,那我先来科普一下: 1.我们先准备好两个淘宝账户,店铺卖家号和买家购物号: 2.然后通过软件加人工特有的操作手

windows下mongodb基础玩法系列二CURD操作(创建、更新、读取和删除)

windows下mongodb基础玩法系列 windows下mongodb基础玩法系列一介绍与安装 windows下mongodb基础玩法系列二CURD操作(创建.更新.读取和删除) 简单说几句 在mongodb中3元素:db(数据库).collection(集合).document(文档) 其中collection类似于数据库中的表,document类似于行,这样一来我们就将内容对比起来记忆学习了. 数据格式 MongoDB documents是BSON格式(一种类json的一种二进制形式的存

Java学习(十一):Java中的常用时间操作

java中的时间操作不外乎这四种情况:获取当前时间,获取某个时间的某种格式,设置时间和时间的运算. 1.获取当前时间 有两种方式可以获得,第一种,使用java.util.Date类. Date date = new Date(); date.getTime(); 还有一种方式,使用System.currentTimeMillis(); 这两种方式获得的结果是一样的,都是得到一个当前的时间的long型的时间的毫秒值,这个值实际上是当前时间值与1970年一月一号零时零分零秒相差的毫秒数. 2.获取某

Java Calendar 类的时间操作

Java Calendar 类的时间操作 标签: javaCalendar时间Date 2013-07-30 17:53 140401人阅读 评论(7) 收藏 举报 分类: 所有(165) Java 算法(24) 版权声明:本文为博主原创文章,未经博主允许不得转载. JavaCalendar 类时间操作,这也许是创建日历和管理最简单的一个方案,示范代码很简单. 演示了获取时间,日期时间的累加和累减,以及比较. 原文地址:blog.csdn.NET/joyous/article/details/9

java时间操作

1.2种常见时间操作: import java.util.Date; import java.util.Calendar; import java.text.SimpleDateFormat; public class TestDate{ public static void main(String[] args){ Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:m