时间处理(今天,昨天,本周,本月)


public
static List<Date> today() throws ParseException { List<Date> listDate = new ArrayList<>(); //今天 Date date = new Date(); //明天 Date date2 = new Date(date.getTime() + 24 * 3600 * 1000); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String formatDate = dateFormat.format(date); String formatDate2 = dateFormat.format(date2); Date today = dateFormat.parse(formatDate); Date tomorrow = dateFormat.parse(formatDate2); listDate.add(today); listDate.add(tomorrow); return listDate; } /** * 昨天 * * @return * @throws ParseException */ public static List<Date> yestoday() throws ParseException { List<Date> listDate = new ArrayList<>(); //今天 Date date = new Date(); //昨天 Date date2 = new Date(date.getTime() - 24 * 3600 * 1000); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String formatDate = dateFormat.format(date); String formatDate2 = dateFormat.format(date2); Date today = dateFormat.parse(formatDate); Date yestoday = dateFormat.parse(formatDate2); listDate.add(today); listDate.add(yestoday); return listDate; } /** * 本周 * * @return * @throws ParseException */ public static List<Date> week() throws ParseException { List<Date> listDate = new ArrayList<>(); //获取周一的值 LocalDate today = LocalDate.now(); LocalDate toweekMonday = today.with(DayOfWeek.MONDAY); LocalDate sunDay = today.with(DayOfWeek.SUNDAY); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zonedDateTime = toweekMonday.atStartOfDay(zoneId); Date monday = Date.from(zonedDateTime.toInstant()); ZonedDateTime zonedDateTime2 = sunDay.atStartOfDay(zoneId); Date sunday = Date.from(zonedDateTime2.toInstant()); //转化date类型 listDate.add(monday); listDate.add(sunday); return listDate; } /** * 本月 * @return * @throws ParseException */ public static List<Date> month() throws ParseException { List<Date> listDate = new ArrayList<>(); /** * 本月第一天 */ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); GregorianCalendar gc2 = (GregorianCalendar) Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); gc2.setTime(new Date()); gc2.set(Calendar.DAY_OF_MONTH, 1); //设置该月的第一天 String b_day_first = df.format(gc2.getTime()); StringBuffer str_b = new StringBuffer().append(b_day_first).append(" 00:00:00");//拼接 时分秒 b_day_first = str_b.toString(); /** * 本月最后一天 */ calendar2.add(Calendar.MONTH, 1); //加一个月 calendar2.set(Calendar.DATE, 1); //设置为该月第一天 calendar2.add(Calendar.DATE, -1); //再减一天即为上个月最后一天 String b_day_last = df.format(calendar2.getTime()); StringBuffer endStrb = new StringBuffer().append(b_day_last).append("23:59:59"); //拼接 时,分,秒 b_day_last = endStrb.toString(); Date monthFirst = df.parse(b_day_first); Date monthLast = df.parse(b_day_last); listDate.add(monthFirst); listDate.add(monthLast); return listDate; }

 

转载于:https://www.cnblogs.com/hxzDzvBlog/p/9212421.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值