Java通用时间处理工具

常用的对date处理的日常总结


import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
	private static String timeFormat = "yyyy-MM-dd HH:mm:ss.SSS";
	private static String timeTamp = "yyyyMMddHHmmssSSS";
	private static Logger log = LoggerFactory.getLogger(WengTaskService.class);
	public static Date abtainUTC() {
		Calendar cal = Calendar.getInstance();
		int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(Calendar.DST_OFFSET);
		cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		int mil = cal.get(Calendar.MILLISECOND);
		StringBuffer UTCTimeBuffer = new StringBuffer();
		UTCTimeBuffer.append(year).append("-").append(month).append("-").append(day).append(" ").append(hour)
		.append(":").append(minute).append(":").append(second).append(".").append(mil);
		try {
			DateFormat format = new SimpleDateFormat(timeFormat);
			Date date = format.parse(UTCTimeBuffer.toString());
			return date;
		} catch (ParseException e) {
			log.error("declaration error in DateUtil.btainUTC()",e);
			return null;
		}

	}

	public static String abtainTamp() {
		Date date = abtainUTC();
		DateFormat formatTamp = new SimpleDateFormat(timeTamp);
		return formatTamp.format(date);
	}

	public static String abtainDate() {
		Date date = abtainUTC();
		DateFormat format = new SimpleDateFormat(timeFormat);
		return format.format(date);
	}

	/**
	 * 获取指定格式的当前时间
	 * @param pattern
	 * @return
	 */
	public static String getTimeByPattern(String pattern) {
		DateFormat format = new SimpleDateFormat(pattern);
		return format.format(new Date());
	}

	public static Date getDateByPattern(String pattern) throws ParseException{
		DateFormat format = new SimpleDateFormat(pattern);
		return format.parse(format.format(new Date()));
	}
	/**
	 * 获取指定时间的当月的初始日期--指定格式
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getMonthStartByPattern(Date date, String pattern) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.MONTH, 0);
		ca.set(Calendar.DAY_OF_MONTH, 1);
		Date date1 = ca.getTime();
		DateFormat format = new SimpleDateFormat(pattern);
		return format.format(date1);
	}

	/**
	 * 获取指定日期的前、后N天的日期,指定返回格式
	 * @param date
	 * @param n
	 * @param pattern
	 * @return
	 */
	public static String getDateGapN(Date date, int n, String pattern) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.DATE, n);
		Date date1 = ca.getTime();
		DateFormat format = new SimpleDateFormat(pattern);
		return format.format(date1);
	}

	/**
	 * 获取指定日期的前、后N天的日期,指定返回格式。 参数date格式yyyy-MM-dd
	 * @param date
	 * @param n
	 * @param pattern
	 * @return
	 * @throws ParseException 
	 */
	public static String getDateGapN(String date, int n, String pattern) throws ParseException {
		if (StringUtils.isEmpty(date)) {
			return null;
		}
		if (StringUtils.isEmpty(pattern)) {
			pattern = "yyyy-MM-dd";
		}
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Date date2 = null;
		date2 = format1.parse(date);
		return getDateGapN(date2, n, pattern);
	}

	/**
	 * 获取指定日期的当前月1号 参数date格式yyyy-MM-dd
	 * @param datestr
	 * @param pattern
	 * @return
	 */
	public static String getFirstDayOfMonth(String datestr, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat(pattern);
		ParsePosition pos = new ParsePosition(0);
		Date date = sdf.parse(datestr, pos);
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),
				ZoneId.systemDefault());
		LocalDateTime endOfDay = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
		Date dates = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
		return sdf1.format(dates);
	}

	/**
	 * 获取指定日期的当天数字,2018-11-25 11:25:56 ==> 25
	 * @param date
	 * @return
	 */
	public static int getDateDay(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		return ca.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取指定日期的当天数字,时间格式yyyy-MM-dd
	 * @param date
	 * @return
	 * 
	 */
	public static int getDateDay(String date) {
		// 如果空,则为负
		if (StringUtils.isEmpty(date)) {
			return -1;
		}
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = new Date();
		try {
			date1 = format.parse(date);
		} catch (ParseException e) {
			log.error("declaration error in DateUtil.etDateDay()",e);
		}
		Calendar ca = Calendar.getInstance();
		ca.setTime(date1);
		return ca.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取指定日期的当天数字,时间格式yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String getDateYM(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		Date date1 = ca.getTime();
		DateFormat format = new SimpleDateFormat("yyyy-MM");
		return format.format(date1);
	}

	/**
	 * 获取指定日期的年月字符串数组,长度length(length == 6 即是半年),2018-11-25 11:25:56 ==>
	 * {“”,“”,“”,“”,“”,“2018-11”},
	 * @param date
	 * @param length
	 * @return
	 */
	public static String[] getMonthArray(Date date, int length) {
		String[] resS = new String[length];
		DateFormat format = new SimpleDateFormat("yyyy-MM");
		for (int i = length - 1; i >= 0; i--) {
			Calendar ca = Calendar.getInstance();
			ca.setTime(date);
			ca.add(Calendar.MONTH, -i);
			Date date1 = ca.getTime();
			resS[length - 1 - i] = format.format(date1);
		}
		return resS;
	}

	/**
	 * 获取当前月的上月的最后一天,指定返回格式
	 * @param pattern
	 * @return
	 */
	public static String getDateUpMonthLast(String pattern) {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, 0);
		ca.set(Calendar.DAY_OF_MONTH, 1);
		ca.add(Calendar.DATE, -1);
		Date date1 = ca.getTime();
		DateFormat format = new SimpleDateFormat(pattern);
		return format.format(date1);
	}

	/**
	 * 获取指定日期的上月的最后一天,指定返回格式 参数date格式yyyy-MM-dd
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException 
	 */
	public static String getDateUpMonthLast(String date, String pattern) throws ParseException {
		if (StringUtils.isEmpty(date)) {
			return null;
		}
		if (StringUtils.isEmpty(pattern)) {
			pattern = "yyyy-MM-dd";
		}
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = null;
		date1 = format.parse(date);
		return getDateUpMonthLast(date1, pattern);
	}

	/**
	 * 获取指定日期的上月的最后一天,指定返回格式 参数date格式yyyy-MM-dd
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getDateUpMonthLast(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		if (StringUtils.isEmpty(pattern)) {
			pattern = "yyyy-MM-dd";
		}
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.MONTH, 0);
		ca.set(Calendar.DAY_OF_MONTH, 1);
		ca.add(Calendar.DATE, -1);
		Date date2 = ca.getTime();
		DateFormat format1 = new SimpleDateFormat(pattern);
		return format1.format(date2);
	}

	/**
	 * 获取指定日期的上月的初始日期的前一天,指定返回格式 参数date格式yyyy-MM-dd
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException 
	 */
	public static String getDateUpMonthLastBefore(String date, String pattern) throws ParseException {
		if (StringUtils.isEmpty(date)) {
			return "date is null!";
		}
		if (StringUtils.isEmpty(pattern)) {
			pattern = "yyyy-MM-dd";
		}
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = null;
		date1 = format.parse(date);
		Calendar ca = Calendar.getInstance();
		ca.setTime(date1);
		ca.add(Calendar.MONTH, -1);
		ca.set(Calendar.DAY_OF_MONTH, 1);
		ca.add(Calendar.DATE, -1);
		Date date2 = ca.getTime();
		DateFormat format1 = new SimpleDateFormat(pattern);
		return format1.format(date2);
	}

	/**
	 * 获取半年前(按月)的上月的最后一天,指定返回格式
	 * @param pattern
	 * @return
	 */
	public static String getDateHalfYearLast(String pattern) {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, -5);
		ca.set(Calendar.DAY_OF_MONTH, 1);
		ca.add(Calendar.DATE, -1);
		Date date1 = ca.getTime();
		DateFormat format = new SimpleDateFormat(pattern);
		return format.format(date1);
	}

	/**
	 * 判断时间是否在指定月,时间格式yyyy-MM-dd, mon格式yyyy-MM
	 * @param date
	 * @param mon
	 * @return
	 */
	public static boolean dateIsInMonth(String date, String mon) {
		if ((mon.split("-")[1]).equals(date.split("-")[1])) {
			return true;
		}
		return false;
	}

	/**
	 * 时间字符串转date
	 * @param dateStr
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateByString(String dateStr, String pattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.parse(dateStr);
	}

	/**
	 * date转String
	 * @param date
	 * @param partten
	 * @return
	 */
	public static String getStringByDate(Date date,String partten){
		SimpleDateFormat sdf = new SimpleDateFormat(partten);
		return sdf.format(date);
	}

	/**
	 * 获取当前时间时间戳
	 * @return
	 */
	public static Timestamp getTimestampNow() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 判断起始日期小于等于截止日期
	 * @param startDate
	 *            yyyy-MM-dd
	 * @param endDate
	 *            yyyy-MM-dd
	 */
	public static boolean compareStartEndDate(String startDate, String endDate) {
		String start = startDate.substring(0,4) + startDate.substring(5,7) + startDate.substring(8,10);
		String end = endDate.substring(0,4) + endDate.substring(5,7) + endDate.substring(8,10);
		return Integer.parseInt(start) <= Integer.parseInt(end);
	}

	/**
	 * 获取指定日期的月日,单个月或日期去掉显示0,如:01月02日->1月2日,11月02日->11月2日
	 * @param date
	 * @return
	 */
	public static String getMonthDayByDate(Date date) {
		DateFormat df = new SimpleDateFormat("MM月dd日");
		String dateStr = df.format(date);
		boolean flag1 = dateStr.substring(0, 1).equals("0");
		boolean flag2 = dateStr.substring(3, 4).equals("0");
		if (flag1 && flag2) {
			return dateStr.substring(1, 3) + dateStr.substring(4, 6);
		}
		if (flag1 && !flag2) {
			return dateStr.substring(1, 6);
		}
		if (!flag1 && flag2) {
			return dateStr.substring(0, 3) + dateStr.substring(4, 6);
		}
		return dateStr;
	}
}

 

package com.hexiang.utils; /** * @(#)DateUtil.java * * * @author kidd * @version 1.00 2007/8/8 */ import java.util.*; import java.text.*; import java.sql.Timestamp; public class DateUtils { /** * 时间范围:年 */ public static final int YEAR = 1; /** * 时间范围:季度 */ public static final int QUARTER = 2; /** * 时间范围:月 */ public static final int MONTH = 3; /** * 时间范围:旬 */ public static final int TENDAYS = 4; /** * 时间范围:周 */ public static final int WEEK = 5; /** * 时间范围:日 */ public static final int DAY = 6; /* 基准时间 */ private Date fiducialDate = null; private Calendar cal = null; private DateUtils(Date fiducialDate) { if (fiducialDate != null) { this.fiducialDate = fiducialDate; } else { this.fiducialDate = new Date(System.currentTimeMillis()); } this.cal = Calendar.getInstance(); this.cal.setTime(this.fiducialDate); this.cal.set(Calendar.HOUR_OF_DAY, 0); this.cal.set(Calendar.MINUTE, 0); this.cal.set(Calendar.SECOND, 0); this.cal.set(Calendar.MILLISECOND, 0); this.fiducialDate = this.cal.getTime(); } /** * 获取DateHelper实例 * * @param fiducialDate * 基准时间 * @return Date */ public static DateUtils getInstance(Date fiducialDate) { return new DateUtils(fiducialDate); } /** * 获取DateHelper实例, 使用当前时间作为基准时间 * * @return Date */ public static DateUtils getInstance() { return new DateUtils(null); } /** * 获取年的第一天 * * @param offset * 偏移量 * @return Date */ public Date getFirstDayOfYear(int offset) { cal.setTime(this.fiducialDate); cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + offset); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } /** * 获取年的最后一天 * * @param offset * 偏移量 * @return Date */ public Date getLastDayOfYear(int offset) { cal.setTime(this.fiducialDate); cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + offset); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DAY_OF_MONTH, 31); return cal.getTime(); } /** * 获取季度的第一天 * * @param offset * 偏移量 * @return Date */ public Date getFirstDayOfQuarter(int offset) { cal.setTime(this.fiducialDate); cal.add(Calendar.MONTH, offset * 3); int mon = cal.get(Calendar.MONTH); if (mon >= Calendar.JANUARY && mon = Calendar.APRIL && mon = Calendar.JULY && mon = Calendar.OCTOBER && mon = Calendar.JANUARY && mon = Calendar.APRIL && mon = Calendar.JULY && mon = Calendar.OCTOBER && mon = 21) { day = 21; } else if (day >= 11) { day = 11; } else { day = 1; } if (offset > 0) { day = day + 10 * offset; int monOffset = day / 30; day = day % 30; cal.add(Calendar.MONTH, monOffset); cal.set(Calendar.DAY_OF_MONTH, day); } else { int monOffset = 10 * offset / 30; int dayOffset = 10 * offset % 30; if ((day + dayOffset) > 0) { day = day + dayOffset; } else { monOffset = monOffset - 1; day = day - dayOffset - 10; } cal.add(Calendar.MONTH, monOffset); cal.set(Calendar.DAY_OF_MONTH, day); } return cal.getTime(); } /** * 获取旬的最后一天 * * @param offset * 偏移量 * @return Date */ public Date getLastDayOfTendays(int offset) { Date date = getFirstDayOfTendays(offset + 1); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } /** * 获取周的第一天(MONDAY) * * @param offset * 偏移量 * @return Date */ public Date getFirstDayOfWeek(int offset) { cal.setTime(this.fiducialDate); cal.add(Calendar.DAY_OF_MONTH, offset * 7); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return cal.getTime(); } /** * 获取周的最后一天(SUNDAY) * * @param offset * 偏移量 * @return Date */ public Date getLastDayOfWeek(int offset) { cal.setTime(this.fiducialDate); cal.add(Calendar.DAY_OF_MONTH, offset * 7); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); cal.add(Calendar.DAY_OF_MONTH, 6); return cal.getTime(); } /** * 获取指定时间范围的第一天 * * @param dateRangeType * 时间范围类型 * @param offset * 偏移量 * @return Date */ public Date getFirstDate(int dateRangeType, int offset) { return null; } /** * 获取指定时间范围的最后一天 * * @param dateRangeType * 时间范围类型 * @param offset * 偏移量 * @return Date */ public Date getLastDate(int dateRangeType, int offset) { return null; } /** * 根据日历的规则,为基准时间添加指定日历字段的时间量 * * @param field * 日历字段, 使用Calendar类定义的日历字段常量 * @param offset * 偏移量 * @return Date */ public Date add(int field, int offset) { cal.setTime(this.fiducialDate); cal.add(field, offset); return cal.getTime(); } /** * 根据日历的规则,为基准时间添加指定日历字段的单个时间单元 * * @param field * 日历字段, 使用Calendar类定义的日历字段常量 * @param up * 指定日历字段的值的滚动方向。true:向上滚动 / false:向下滚动 * @return Date */ public Date roll(int field, boolean up) { cal.setTime(this.fiducialDate); cal.roll(field, up); return cal.getTime(); } /** * 把字符串转换为日期 * * @param dateStr * 日期字符串 * @param format * 日期格式 * @return Date */ public static Date strToDate(String dateStr, String format) { Date date = null; if (dateStr != null && (!dateStr.equals(""))) { DateFormat df = new SimpleDateFormat(format); try { date = df.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } } return date; } /** * 把字符串转换为日期,日期的格式为yyyy-MM-dd HH:ss * * @param dateStr * 日期字符串 * @return Date */ public static Date strToDate(String dateStr) { Date date = null; if (dateStr != null && (!dateStr.equals(""))) { if (dateStr.matches("\\d{4}-\\d{1,2}-\\d{1,2}")) { dateStr = dateStr + " 00:00"; } else if (dateStr.matches("\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}")) { dateStr = dateStr + ":00"; } else { System.out.println(dateStr + " 格式不正确"); return null; } DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss"); try { date = df.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } } return date; } /** * 把日期转换为字符串 * * @param date * 日期实例 * @param format * 日期格式 * @return Date */ public static String dateToStr(Date date, String format) { return (date == null) ? "" : new SimpleDateFormat(format).format(date); } public static String dateToStr(Date date) { return (date == null) ? "" : new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date); } /** * 取得当前日期 年-月-日 * * @return Date */ public static String getCurrentDate() { DateFormat f = new SimpleDateFormat("yyyy-MM-dd"); return f.format(Calendar.getInstance().getTime()); } public static void main(String[] args) { DateUtils dateHelper = DateUtils.getInstance(); /* Year */ for (int i = -5; i <= 5; i++) { System.out.println("FirstDayOfYear(" + i + ") = " + dateHelper.getFirstDayOfYear(i)); System.out.println("LastDayOfYear(" + i + ") = " + dateHelper.getLastDayOfYear(i)); } /* Quarter */ for (int i = -5; i <= 5; i++) { System.out.println("FirstDayOfQuarter(" + i + ") = " + dateHelper.getFirstDayOfQuarter(i)); System.out.println("LastDayOfQuarter(" + i + ") = " + dateHelper.getLastDayOfQuarter(i)); } /* Month */ for (int i = -5; i <= 5; i++) { System.out.println("FirstDayOfMonth(" + i + ") = " + dateHelper.getFirstDayOfMonth(i)); System.out.println("LastDayOfMonth(" + i + ") = " + dateHelper.getLastDayOfMonth(i)); } /* Week */ for (int i = -5; i <= 5; i++) { System.out.println("FirstDayOfWeek(" + i + ") = " + dateHelper.getFirstDayOfWeek(i)); System.out.println("LastDayOfWeek(" + i + ") = " + dateHelper.getLastDayOfWeek(i)); } /* Tendays */ for (int i = -5; i <= 5; i++) { System.out.println("FirstDayOfTendays(" + i + ") = " + dateHelper.getFirstDayOfTendays(i)); System.out.println("LastDayOfTendays(" + i + ") = " + dateHelper.getLastDayOfTendays(i)); } } /** * 取当前日期的字符串形式,"XXXX年XX月XX日" * * @return java.lang.String */ public static String getPrintDate() { String printDate = ""; Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); printDate += calendar.get(Calendar.YEAR) + "年"; printDate += (calendar.get(Calendar.MONTH) + 1) + "月"; printDate += calendar.get(Calendar.DATE) + "日"; return printDate; } /** * 将指定的日期字符串转化为日期对象 * * @param dateStr * 日期字符串 * @return java.util.Date */ public static Date getDate(String dateStr, String format) { if (dateStr == null) { return new Date(); } if (format == null) { format = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(format); try { Date date = sdf.parse(dateStr); return date; } catch (Exception e) { return null; } } /** * 从指定Timestamp中得到相应的日期的字符串形式 日期"XXXX-XX-XX" * * @param dateTime * @return 、String */ public static String getDateFromDateTime(Timestamp dateTime) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(dateTime).toString(); } /** * 得到当前时间 return java.sql.Timestamp * * @return Timestamp */ public static Timestamp getNowTimestamp() { long curTime = System.currentTimeMillis(); return new Timestamp(curTime); } }
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值