JAVA获取目标日期对应的年,月,季度,周时间类

这篇博客主要记录了如何使用JAVA编程语言来获取目标日期对应的年份、月份、季度和星期,强调实践操作的重要性。
摘要由CSDN通过智能技术生成

好记性不如烂笔头

public class TimeUtil {
   

    /**
     * 获取 当前年、半年、季度、月、日、小时 开始结束时间
     */

    private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
    private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat _shortSdf = new SimpleDateFormat("yyyyMMdd");

    /**
     * 获得本周的第一天,周一
     * 
     * @return
     */
    public static Date getCurrentWeekDayStartTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得目标日期的,周日
     * 
     * @return
     */
    public Date getCurrentWeekDayEndTime(String targetDate) {
        Date from = DateUtils.parseDate(targetDate, DateUtils.FMT_DATE_);
        Calendar cal = Calendar.getInstance();  
        cal.setTime(from); 
        try {
            int weekday = cal.get(Calendar.DAY_OF_WEEK);
            cal.add(Calendar.DATE, 8 - weekday);
            cal.setTime(_shortSdf.parse(_shortSdf.format(cal.getTime())));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cal.getTime();
    }

     /** 
     * 获得本周的最后一天,周日 
     * 
     * @return 
     */  
    public static Date getCurrentWeekDayEndTime() {  
        Calendar c = Calendar.getInstance();  
        try {  
            int weekday = c.get(Calendar.DAY_OF_WEEK);  
            c.add(Calendar.DATE, 8 - weekday);  
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return c.getTime();  
    }

    /**
     * 获得本天的开始时间
     * 
     * @return
     */
    public static Date getCurrentDayStartTime() {
        Date now = new Date();
        try {
            now = shortSdf.parse(shortSdf.format(now));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获得本天的结束时间
     * 
     * @return
     */
    public static Date getCurrentDayEndTime() {
        Date now = new Date();
        try {
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值