|
| 1 | +/* |
| 2 | + * Copyright 2016-2026 TinyZ |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.ogcs.utilities; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.time.LocalDate; |
| 21 | +import java.time.LocalDateTime; |
| 22 | +import java.time.ZoneOffset; |
| 23 | +import java.time.format.DateTimeFormatter; |
| 24 | +import java.util.LinkedList; |
| 25 | + |
| 26 | +/** |
| 27 | + * 日期时间工具 |
| 28 | + * <p> |
| 29 | + * <p>基于JDK 1.8</p> |
| 30 | + * |
| 31 | + * @author TinyZ |
| 32 | + * @since 2.0 |
| 33 | + */ |
| 34 | +public final class TimeV8Util { |
| 35 | + |
| 36 | + /** |
| 37 | + * 缺省时区: +08:00 东八区 |
| 38 | + */ |
| 39 | + private static String defaultZoneOffset = "+08:00"; |
| 40 | + private static final String DATE_TIME_STR = "yyyy-MM-dd HH:mm:ss"; |
| 41 | + private static final String DATE_STR = "yyyy-MM-dd"; |
| 42 | + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_STR); |
| 43 | + private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_STR); |
| 44 | + |
| 45 | + static { |
| 46 | + defaultZoneOffset = System.getProperty("time.timezone", defaultZoneOffset); |
| 47 | + } |
| 48 | + |
| 49 | + private TimeV8Util() { |
| 50 | + // no-op |
| 51 | + } |
| 52 | + |
| 53 | + // DATE |
| 54 | + |
| 55 | + public static String date() { |
| 56 | + return date(LocalDate.now()); |
| 57 | + } |
| 58 | + |
| 59 | + public static String date(LocalDate date) { |
| 60 | + return date.format(DATE_FORMATTER); |
| 61 | + } |
| 62 | + |
| 63 | + public static String date(LocalDate date, String pattern) { |
| 64 | + return date.format(DateTimeFormatter.ofPattern(pattern)); |
| 65 | + } |
| 66 | + |
| 67 | + public static long parseDate(String date) { |
| 68 | + return parseDate(date, DATE_STR, defaultZoneOffset); |
| 69 | + } |
| 70 | + |
| 71 | + public static long parseDate(String date, String pattern, String zone) { |
| 72 | + return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern)) |
| 73 | + .atStartOfDay() |
| 74 | + .toEpochSecond(ZoneOffset.of(zone)) * 1000L; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 计算日期间隔(天数) |
| 79 | + * |
| 80 | + * @param date0 起始日期 |
| 81 | + * @param date1 终止日期 |
| 82 | + * @return 间隔天数, 当时间大于起始时间 返回正数,否则返回负数 |
| 83 | + */ |
| 84 | + public static int dayInterval(String date0, String date1) { |
| 85 | + ZoneOffset offset = ZoneOffset.of(defaultZoneOffset); |
| 86 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_STR); |
| 87 | + return (int) Duration.between( |
| 88 | + LocalDate.parse(date0, formatter).atStartOfDay().toInstant(offset), |
| 89 | + LocalDate.parse(date1, formatter).atStartOfDay().toInstant(offset) |
| 90 | + ).toDays(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * 计算日期间隔(天数) |
| 95 | + * |
| 96 | + * @param time0 起始时间戳(微秒) |
| 97 | + * @param time1 终止时间戳(微秒) |
| 98 | + * @return 间隔天数, 当时间大于起始时间 返回正数,否则返回负数 |
| 99 | + */ |
| 100 | + public static int dayInterval(long time0, long time1) { |
| 101 | + ZoneOffset offset = ZoneOffset.of(defaultZoneOffset); |
| 102 | + return (int) (Duration.between( |
| 103 | + LocalDateTime |
| 104 | + .ofEpochSecond(time0 / 1000L, 0, offset) |
| 105 | + .withHour(0).withMinute(0).withSecond(0), |
| 106 | + LocalDateTime |
| 107 | + .ofEpochSecond(time1 / 1000L, 0, offset) |
| 108 | + .withHour(0).withMinute(0).withSecond(0) |
| 109 | + ).toDays()); |
| 110 | + } |
| 111 | + |
| 112 | + public static LinkedList<Long> getTimeList(long time0, long time1) { |
| 113 | + return getTimeList(time0, time1, defaultZoneOffset); |
| 114 | + } |
| 115 | + |
| 116 | + public static LinkedList<Long> getTimeList(long time0, long time1, String zone) { |
| 117 | + ZoneOffset offset = ZoneOffset.of(zone); |
| 118 | + LocalDateTime var0 = LocalDateTime.ofEpochSecond(time0 / 1000L, 0, offset) |
| 119 | + .withHour(0).withMinute(0).withSecond(0); |
| 120 | + LocalDateTime var1 = LocalDateTime.ofEpochSecond(time1 / 1000L, 0, offset) |
| 121 | + .withHour(0).withMinute(0).withSecond(0); |
| 122 | + LinkedList<Long> dayList = new LinkedList<>(); |
| 123 | + if (var1.isBefore(var0)) { |
| 124 | + return dayList; |
| 125 | + } |
| 126 | + LocalDateTime tmp = var0; |
| 127 | + while (tmp.isBefore(var1) || tmp.isEqual(var1)) { |
| 128 | + dayList.add(tmp.toLocalDate().atStartOfDay().toEpochSecond(offset) * 1000L); |
| 129 | + tmp = tmp.plusDays(1); |
| 130 | + } |
| 131 | + return dayList; |
| 132 | + } |
| 133 | + |
| 134 | + public static LinkedList<String> getDayList(long time0, long time1) { |
| 135 | + return getDayList(time0, time1, defaultZoneOffset); |
| 136 | + } |
| 137 | + |
| 138 | + public static LinkedList<String> getDayList(long time0, long time1, String zone) { |
| 139 | + ZoneOffset offset = ZoneOffset.of(zone); |
| 140 | + LocalDateTime var0 = LocalDateTime.ofEpochSecond(time0 / 1000L, 0, offset) |
| 141 | + .withHour(0).withMinute(0).withSecond(0); |
| 142 | + LocalDateTime var1 = LocalDateTime.ofEpochSecond(time1 / 1000L, 0, offset) |
| 143 | + .withHour(0).withMinute(0).withSecond(0); |
| 144 | + LinkedList<String> dayList = new LinkedList<>(); |
| 145 | + if (var1.isBefore(var0)) { |
| 146 | + return dayList; |
| 147 | + } |
| 148 | + LocalDateTime tmp = var0; |
| 149 | + while (tmp.isBefore(var1) || tmp.isEqual(var1)) { |
| 150 | + dayList.add(date(tmp.toLocalDate())); |
| 151 | + tmp = tmp.plusDays(1); |
| 152 | + } |
| 153 | + return dayList; |
| 154 | + } |
| 155 | + |
| 156 | + // DATE TIME |
| 157 | + |
| 158 | + public static String dateTime() { |
| 159 | + return dateTime(LocalDateTime.now()); |
| 160 | + } |
| 161 | + |
| 162 | + public static String dateTime(LocalDateTime dateTime) { |
| 163 | + return dateTime.format(DATE_TIME_FORMATTER); |
| 164 | + } |
| 165 | + |
| 166 | + public static String dateTime(LocalDateTime dateTime, String pattern) { |
| 167 | + return dateTime.format(DateTimeFormatter.ofPattern(pattern)); |
| 168 | + } |
| 169 | + |
| 170 | + public static long parseDateTime(String dateTime) { |
| 171 | + return parseDateTime(dateTime, DATE_TIME_STR, defaultZoneOffset); |
| 172 | + } |
| 173 | + |
| 174 | + public static long parseDateTime(String dateTime, String pattern) { |
| 175 | + return parseDateTime(dateTime, pattern, defaultZoneOffset); |
| 176 | + } |
| 177 | + |
| 178 | + public static long parseDateTime(String dateTime, String pattern, String zone) { |
| 179 | + return LocalDateTime. |
| 180 | + parse(dateTime, DateTimeFormatter.ofPattern(pattern)). |
| 181 | + toEpochSecond(ZoneOffset.of(zone)) * 1000L; |
| 182 | + } |
| 183 | +} |
0 commit comments