赞
踩
需要引入Hutool
工具类
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.14</version>
</dependency>
</dependencies>
import cn.hutool.core.date.DateUtil; import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] args) { System.out.println(getWeekStartDay()); System.out.println(getWeekEndDay()); System.out.println(getCurrentWeekStartDay()); System.out.println(getCurrentWeekEndDay()); System.out.println(getNeverSevenStartTime()); System.out.println(getNeverSevenEndTime()); System.out.println(getYesterdayStartTime()); System.out.println(getTodayStartTime()); System.out.println(getTodayEndTime()); } /** * 获得上周第一天 周日 00:00:00 */ public static Date getWeekStartDay() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK, 1); calendar.add(Calendar.DATE, -7); return DateUtil.beginOfDay(calendar.getTime()); } /** * 获得上周最后一天 周六 23:59:59 */ public static Date getWeekEndDay() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getWeekStartDay()); calendar.add(Calendar.DATE, 6); return DateUtil.endOfDay(calendar.getTime()); } /** * 获得本周第一天 周日 00:00:00 */ public static Date getCurrentWeekStartDay() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK, 1); return DateUtil.beginOfDay(calendar.getTime()); } /** * 获得本周最后一天 周六 23:59:59 */ public static Date getCurrentWeekEndDay() { Calendar cal = Calendar.getInstance(); cal.setTime(getCurrentWeekStartDay()); cal.add(Calendar.DATE, 6); return DateUtil.endOfDay(cal.getTime()); } /** * 近七天开始时间 * * @return */ public static Date getNeverSevenStartTime() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DATE, -7); return DateUtil.beginOfDay(calendar.getTime()); } /** * 近七天结束时间 * * @return */ public static Date getNeverSevenEndTime() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getNeverSevenStartTime()); calendar.add(Calendar.DATE, 6); return DateUtil.endOfDay(calendar.getTime()); } /** * 昨天开始时间 * * @return */ public static Date getYesterdayStartTime() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DATE, -1); return DateUtil.beginOfDay(calendar.getTime()); } /** * 昨天结束时间 * * @return */ public static Date getYesterdayEndTime() { return DateUtil.endOfDay(getYesterdayStartTime()); } /** * 今天开始时间 * * @return */ public static Date getTodayStartTime() { return DateUtil.beginOfDay(new Date()); } /** * 今天结束时间 * * @return */ public static Date getTodayEndTime() { return DateUtil.endOfDay(new Date()); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。