java中关于日期的工具类

这个Java代码示例提供了一个名为`DateCalendarUtils`的工具类,包含了获取当前月第一天、最后一天、本周星期一和下周一的方法,以及计算两个日期间相差天数、判断日期是否在同一周和同一日的功能。这些方法对于处理日期和日历计算非常实用。
摘要由CSDN通过智能技术生成

java中关于日期的工具类

简简单单,代码如下

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

public class DateCalendarUtils {

	/**
	 * 获取当前月第一天
	 * 
	 * @param pase
	 * @return
	 * @throws ParseException
	 */
	public static Date getMonth(Date pase) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf.format(pase);
		Date date = sdf.parse(date1);
		// 获取当前月第一天:
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		Date first = c.getTime();
		return first;
	}

	/**
	 * 获取当前月最后一天
	 *
	 * @param pase
	 * @return
	 * @throws ParseException
	 */
	public static Date getNextMonth(Date pase) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf.format(pase);
		Date date = sdf.parse(date1);
		// 获取当前月最后一天
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date last = ca.getTime();
		long time = last.getTime() + 24 * 60 * 60 * 1000L;
		Date next = new Date(time);
		return next;
	}

	public static Date getMonday(Date now) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf.format(now);
		Date parse = sdf.parse(date1);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parse);
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		long time = parse.getTime();
		long monday = time - (week-1) * 24 * 60 * 60 * 1000;// week-1
		Date date = new Date(monday);
		return date;
	}

	@SuppressWarnings("deprecation")
	public static Date nextMonday(Date now) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf.format(now);
		Date parse = sdf.parse(date1);
		int week = parse.getDay();
		long time = parse.getTime();
		long sunday = time + (7 - week) * 24 * 60 * 60 * 1000; // 8
		Date date = new Date(sunday);
		return date;
	}

	public static int countDays(Date beginTime, Date endTime) {
		long count = endTime.getTime() - beginTime.getTime();
		// 判断是不是同一天
		boolean sameDate = isSameDate(beginTime, endTime);
		if (sameDate) {
			return 1;
		} else {
			long oneDay = 24 * 60 * 60 * 1000;
			long num = count % oneDay;
			int days = 0;
			if (num != 0) {
				days = (int) (1 + count / oneDay);
			} else {
				days = (int) (count / oneDay);
			}
			return days;
		}
	}

	public static boolean isSameDate(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
		boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
		boolean isSameDate = isSameMonth
				&& cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);

		return isSameDate;
	}

	public static boolean isSameWeeks(Date now, Date beginTime)

	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf.format(now);

		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		String date2 = sdf1.format(beginTime);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date d1 = null;
		Date d2 = null;
		try {
			d1 = format.parse(date1);
			d2 = format.parse(date2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(d1);
		cal2.setTime(d2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		// subYear==0,说明是同一年
		if (subYear == 0) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (subYear == 1 && cal2.get(Calendar.MONTH) == 11) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		// 例子:cal1是"2004-12-31",cal2是"2005-1-1"
		else if (subYear == -1 && cal1.get(Calendar.MONTH) == 11) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;

		}
		return false;
	}

	@SuppressWarnings("deprecation")
	public static long getNextMonthTime(long time) {
		Date date = new Date(time);
		int hours = date.getHours();
		int minutes = date.getMinutes();
		int seconds = date.getSeconds();
		int day = date.getDate();
		int month = date.getMonth();
		int year = date.getYear();
		if (month == 11) {
			year++;
		}
		month = (month + 1)%12;
		return new Date(year, month, day, hours, minutes, seconds).getTime();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王之蔑视.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值