当前位置:科普知识站>IT科技>

java获取本周日期

IT科技 阅读(2.61W)

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

很多朋友都想知道java获取本周日期?下面就一起来了解一下吧~

//获得本周一日期        Date timesWeekmorning = getTimesWeekmorning();        String format1 = format.format(timesWeekmorning);

        //获得本周日日期        Date timesWeeknight = getTimesWeeknight();        String format2 =format.format(timesWeeknight);
        // 获得本月第一天日期        Date timesMonthmorning = getTimesMonthmorning();        String format3 =format.format(timesMonthmorning);
        // 获得本月最后一天日期        Date timesMonthnight = getTimesMonthnight();        String format4 =format.format(timesMonthnight);

java获取本周日期

// 获得本周一0点时间    public static Date getTimesWeekmorning() {        Calendar cal = Calendar.getInstance();        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);        return cal.getTime();    }
    // 获得本周日24点时间    public static Date getTimesWeeknight() {        Calendar cal = Calendar.getInstance();        cal.setTime(getTimesWeekmorning());        cal.add(Calendar.DAY_OF_WEEK, 7);        return cal.getTime();    }
    // 获得本月第一天0点时间    public static Date getTimesMonthmorning() {        Calendar cal = Calendar.getInstance();        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));        return cal.getTime();    }
    // 获得本月最后一天24点时间    public static Date getTimesMonthnight() {        Calendar cal = Calendar.getInstance();        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));        cal.set(Calendar.HOUR_OF_DAY, 24);        return cal.getTime();    }