获取自然周 自然月 区间
public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");/*** @Description: 获取时间段内所有自然周,参数格式为:yyyy-MM-dd* @Param: [startDate, endDate]* @Return: java.util.List<Test.DateVo>*/public static List<CycleTimeVo> getBetweenWeeks(String startDate, String endDate) throws ParseException {List<String> listWeekOrMonth = new ArrayList<String>();List<CycleTimeVo> dateVoList = new ArrayList<CycleTimeVo>();Date sDate = sdf.parse(startDate);// 定义起始日期Calendar sCalendar = Calendar.getInstance();sCalendar.setFirstDayOfWeek(Calendar.MONDAY);sCalendar.setTime(sDate);Date eDate = sdf.parse(endDate);// 定义结束日期Calendar eCalendar = Calendar.getInstance();eCalendar.setFirstDayOfWeek(Calendar.MONDAY);eCalendar.setTime(eDate);boolean bool = true;while (sCalendar.getTime().getTime() < eCalendar.getTime().getTime()) {if (bool || sCalendar.get(Calendar.DAY_OF_WEEK) == 2 || sCalendar.get(Calendar.DAY_OF_WEEK) == 1) {listWeekOrMonth.add(sdf.format(sCalendar.getTime()));bool = false;}sCalendar.add(Calendar.DAY_OF_MONTH, 1);}listWeekOrMonth.add(sdf.format(eCalendar.getTime()));if (listWeekOrMonth.size() % 2 != 0) {listWeekOrMonth.add(sdf.format(eCalendar.getTime()));}for (int i = 0; i < listWeekOrMonth.size() - 1; i++) {if (i % 2 == 0) {CycleTimeVo dateVo = new CycleTimeVo();dateVo.setStartTimeStr(listWeekOrMonth.get(i));dateVo.setEndTimeStr(listWeekOrMonth.get(i + 1));dateVoList.add(dateVo);}}return dateVoList;}/*** 获取自然月* @param minDate* @param maxDate* @return* @throws ParseException*/public static List<CycleTimeVo> getMonthBetween(String minDate, String maxDate) throws ParseException {Calendar min = Calendar.getInstance();Calendar max = Calendar.getInstance();min.setTime(sdf.parse(minDate));min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);max.setTime(sdf.parse(maxDate));max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);Calendar curr = min;LinkedList<CycleTimeVo> CycleTimeList=new LinkedList();while (curr.before(max)) {CycleTimeVo cycleTimeVo=new CycleTimeVo();cycleTimeVo.setStartTimeStr(sdf.format(curr.getTime()));String monthEnd = getMonthEnd(sdf.format(curr.getTime()));cycleTimeVo.setEndTimeStr(monthEnd);CycleTimeList.add(cycleTimeVo);//result.add(sdf.format(curr.getTime()));curr.add(Calendar.MONTH, 1);}CycleTimeList.getFirst().setStartTimeStr(minDate);CycleTimeList.getLast().setEndTimeStr(maxDate);return CycleTimeList;}public static String getMonthEnd(String time) throws ParseException {SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");Date date= simpleDateFormat.parse(time);Calendar c = Calendar.getInstance();c.setTime(date);//设置为当月最后一天c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));//将小时至23c.set(Calendar.HOUR_OF_DAY, 23);//将分钟至59c.set(Calendar.MINUTE, 59);//将秒至59c.set(Calendar.SECOND,59);//将毫秒至999c.set(Calendar.MILLISECOND, 999);// 获取本月最后一天的时间return simpleDateFormat.format(c.getTime());}
获取自然周 自然月 区间相关推荐
- Date-如何获取自然天,自然周,自然月,自然年?
前言:有次在开发过程中,遇到一个需求.需要获取上count个自然天,自然周,自然月,自然年. 当时第一反应就是有没有工具类可以直接实现这个功能. 经过一番的探索,最终发现了这个工具类:Calendar ...
- Impala SQL实现按天、自然周、月、季度、年份周期统计
在整理完了Oracle.MySQL中如何通过SQL实现按天.自然周.月.季度.年份周期统计时,忽然想起在hive与impala中,SQL的语法也是不尽相同,但又有共通之处. 俗话说,授人予鱼,不若授人 ...
- Oracle中按天、自然周、月、季、年周期统计
在Oracle中,Mysql中以及Hive中,SQL实现同样的功能有时候可能要遵循不同的语法结构,尤其在日期操作方面区别较为明显.接下来,将Oracle中常用的周期统计梳理出来. 1. 按天统计 Or ...
- MySQL中按天、自然周、月、季度、年份统计
在Oracle数据库中,通过to_char()函数来操作日期变量,而在MySQL中,则通过date_format()函数实现日期相关周期的统计.date_format()函数一共有两个参数:date参 ...
- mysql monday event_MySQL获取日期周、月、天,生成序号
MySQL时间格式化: %Y-%m-%d %H:%i:%s.%f SELECT DATE_FORMAT('2020-04-12 15:23:16.463','%Y-%m-%d %H:%i:%s.%f ...
- java localdate获取自然周
ISO8601自然周 自然周的标准很多.其中以ISO8601 为准. 可以看到一年的第一个自然周应当满足: 有第一个星期四 包含1月4号 第一个自然周应当有4个或者4个以上的天数 这个星期开始的时间( ...
- 【scala】获取当前时间的上一个自然周以及自然周集合;获取当前时间的上一个自然月以及自然月的第一天与最后一天
获取当前时间的上一个自然周的开始时间: import java.text.SimpleDateFormat import java.util.{Calendar, Date}def getLastWe ...
- JS获取上周(自然周、最近七天)、上月(自然月、最近一个月)、全年的开始和结束日期
JS获取上周(自然周.最近七天).上月(自然月.最近一个月).全年的开始和结束日期 let now = new Date(); let nowTime = now.getTime(); //当前的时间 ...
- mysql查询最近一个自然周_自然周与自然月的Hive统计SQL
按照周或者月统计活跃数: 周: SELECT week, COUNT(DISTINCT pin), business_type FROM ( SELECT DISTINCT user_log_acct ...
最新文章
- Python之列表list转换成字典dict的的方法【使用内置函数zip和dict快速搞定】
- Linux Kernel TCP/IP Stack — L1 Layer — NIC Controller
- KVM虚拟化笔记(十三)------kvm虚拟机静态迁移
- mysql的数据类型——待写
- javascript基础--数组排序
- python opengl_Python环境搭建之OpenGL
- oracle之单行函数之多表查询
- (王道408考研数据结构)第六章图-第四节4:最短路径之迪杰斯特拉算法(思想、代码、演示、答题规范)
- 【全网最全的博客美化系列教程】08.自定义地址栏Logo
- CentOS8 安装 Docker
- Andriod广播注册接收过程简析
- 集成电路那些事儿:CMOS模拟集成电路版图基础
- 悲伤是一种毒,会上瘾
- [计算机网络】【网络设备】虚拟局域网
- win10 安装硕正
- 【愚公系列】2022年09月 微信小程序-WebGL立体图形的绘制
- CSS实现div竖向排版
- 编写程序,生成一种贯穿10*10字符数组(初始时全为字符'.')的“随机步法”。程序必须随机地从一个元素“走到”另一个元素,每次都向上、向下、向左或向右移动一个元素位置
- rfc3550-rtp:一种实时应用的传输协议
- igxe取东西显示服务器繁忙,igxe服务器繁忙