java 判断时间

java 判断时间,第1张

java 判断时间
// 获取本周周一,周末的日期
public static void main(String[] args) throws ParseException {
        String nt = "2021-1-1";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Calendar firstDayOfWeek = Calendar.getInstance(Locale.getDefault());
        firstDayOfWeek.setFirstDayOfWeek(Calendar.MONDAY);
        int day = firstDayOfWeek.get(Calendar.DAY_OF_WEEK);
        firstDayOfWeek.add(Calendar.DATE, -day+1+1);// 后面的+1是因为从周日开始
        Date firstDayOfWeek1 = firstDayOfWeek.getTime();  // 周一


        Calendar lastDayOfWeek = Calendar.getInstance(Locale.getDefault());
        lastDayOfWeek.setFirstDayOfWeek(Calendar.MONDAY);
        day = lastDayOfWeek.get(Calendar.DAY_OF_WEEK);
        lastDayOfWeek.add(Calendar.DATE, 7-day+1);
        // 本周星期天的日期
        Date lastDayOfWeek1 = lastDayOfWeek.getTime();
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5710272.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存