| | |
| | | } |
| | | |
| | | /* |
| | | * 根据时间获取时间内时间点 |
| | | * 例:time=2021-08-04 就得到这天内每个小时时间点 |
| | | * time=2021-08 就得到这月内每天时间点 |
| | | * */ |
| | | * 根据时间获取时间内时间点 |
| | | * 例:time=2021-08-04 就得到这天内每个小时时间点2021-08-04 00,2021-08-04 01。。。 |
| | | * time=2021-08 就得到这月内每天时间点 |
| | | * */ |
| | | public static List<String> getTimeLag(String time) { |
| | | List<String> result = new ArrayList<>(); |
| | | int length = time.length(); |
| | | Calendar cal = Calendar.getInstance(); |
| | | String end; |
| | | String dateFormat; |
| | | String df; |
| | | int i; |
| | | if (length == 10) {//日 |
| | | end = getDateAddDay(time, 1); |
| | | dateFormat = yyyy_MM_dd_EN; |
| | | dateFormat = yyyy_MM_dd_HH_EN; |
| | | df = yyyy_MM_dd_EN; |
| | | i = Calendar.HOUR_OF_DAY; |
| | | } else if (length == 7) {//月 |
| | | end = getDateAddMonth(time, 1); |
| | | dateFormat = yyyy_MM_EN; |
| | | dateFormat = yyyy_MM_dd_EN; |
| | | df = yyyy_MM_EN; |
| | | i = Calendar.DAY_OF_MONTH; |
| | | } else {//年 |
| | | end = getDateAddYear(time, 1); |
| | | dateFormat =yyyy; |
| | | dateFormat = yyyy_MM_EN; |
| | | df = yyyy; |
| | | i = Calendar.MONTH; |
| | | } |
| | | cal.setTime(getDate(time, dateFormat)); |
| | | for (long d = cal.getTimeInMillis(); d < getDate(end, dateFormat).getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) { |
| | | String format = dateToDateString(new Date(d)); |
| | | cal.setTime(getDate(time, df)); |
| | | for (long d = cal.getTimeInMillis(); d < getDate(end, df).getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) { |
| | | String format = dateToDateString(new Date(d),dateFormat); |
| | | result.add(format); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | //根据date取之前第一个分钟为0或5的时间点。例:2021-08-09 15:32:00->2021-08-09 15:30:00,2021-08-09 15:39:00->2021-08-09 15:35:00 |
| | | public static Date getFiveMinuteDate(Date date) { |
| | | String dateString = dateToDateString(date, yyyy_MM_dd_HH_mm_EN); |
| | | String minute = dateString.substring(15, 16); |
| | | int i = Integer.parseInt(minute); |
| | | if (i > 0 && i < 5) { |
| | | i = 0; |
| | | } else if (i > 5 && i <= 9) { |
| | | i = 5; |
| | | } |
| | | StringBuilder stringBuffer = new StringBuilder(dateString); |
| | | stringBuffer.replace(15, 16, String.valueOf(i)); |
| | | return getDate(stringBuffer.toString(), yyyy_MM_dd_HH_mm_EN); |
| | | } |
| | | } |