前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java判断日期是不是当天、本周、本月、本季度、本年

Java判断日期是不是当天、本周、本月、本季度、本年

作者头像
小颜同学
发布2023-08-22 08:13:58
1.8K0
发布2023-08-22 08:13:58
举报
文章被收录于专栏:原创笔记

这是一个java工具类,用于大多数需要判断日期是不是当天、本周、本月、本季度、本年,以更好的进行下一步判断, 那么如何去写代码吗?请直接看下面:

代码语言:javascript
复制
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 
public class DateStatusUtil {
 
    //判断选择的日期是否是今天
    public static boolean isToday(Date time) {
        return isThisTime(time, "yyyy-MM-dd");
    }
 
	//判断选择的日期是否是本周
    public static boolean isThisWeek(Date time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(time);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }
 
    //判断选择的日期是否是本月
    public static boolean isThisMonth(Date time) {
        return isThisTime(time, "yyyy-MM");
    }
 
    //判断选择的日期是否是本年
    public static boolean isThisYear(Date time) {
        return isThisTime(time, "yyyy");
    }
 
    //判断选择的日期是否是本季度
    public static boolean isThisQuarter(Date time) {
        Date QuarterStart = getCurrentQuarterStartTime();
        Date QuarterEnd = getCurrentQuarterEndTime();
        return time.after(QuarterStart) && time.before(QuarterEnd);
    }
 
    private static boolean isThisTime(Date time, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(time);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }
 
    /**
     * 获得季度开始时间
     * @return
     */
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
 
    /**
     * 当前季度的结束时间
     * @return
     */
    public static Date getCurrentQuarterEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentQuarterStartTime());
        cal.add(Calendar.MONTH, 3);
        return cal.getTime();
    }
 
	public static void main(String[] args) {
 
		try {
			System.out.println("当天:" + DateStatusUtil
					.isToday(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-15 08:23:21")));
			System.out.println("本周:" + DateStatusUtil
					.isThisWeek(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-25 08:23:21")));
			System.out.println("本月:" + DateStatusUtil
					.isThisMonth(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-15 08:23:21")));
			System.out.println("本年:" + DateStatusUtil
					.isThisYear(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2022-07-15 08:23:21")));
			System.out.println("本季度:" + DateStatusUtil
					.isThisQuarter(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-15 08:23:21")));
 
		} catch (Exception pe) {
			System.out.println(pe.getMessage());
		}

	}
 
}

运行后输出:

代码语言:javascript
复制
当天:true
本周:false
本月:true
本年:false
本季度:true
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档