1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
| import java.sql.Timestamp; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date;
public class DateTimeUtils {
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmss"); public static final DateTimeFormatter YEAR_MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM"); public static final DateTimeFormatter SHORT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static final DateTimeFormatter SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static final DateTimeFormatter LONG_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS"); public static final DateTimeFormatter ABC_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd"); public static final DateTimeFormatter ABC_TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); public static final DateTimeFormatter ABC_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); public static final DateTimeFormatter ABC_SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");
public static String dateStrConvertDateStrFromLocalDateTime(String dateStr,DateTimeFormatter oldDateTimeFormatter,DateTimeFormatter newDateTimeFormatter){ LocalDateTime localDateTime = LocalDateTime.parse(dateStr, oldDateTimeFormatter); return localDateTime.format(newDateTimeFormatter); }
public static String dateStrConvertDateStrFromLocalDate(String dateStr,DateTimeFormatter oldDateTimeFormatter,DateTimeFormatter newDateTimeFormatter){ LocalDate localDate = LocalDate.parse(dateStr, oldDateTimeFormatter); return localDate.format(newDateTimeFormatter); }
public static Date dateStrConvertDate(String dateStr,DateTimeFormatter dateTimeFormatter){ LocalDateTime parse = LocalDateTime.parse(dateStr, dateTimeFormatter); Instant instant = Instant.ofEpochSecond(parse.toEpochSecond(ZoneOffset.ofHours(8))); return Date.from(instant); }
public static String dateConvertDateStr(Date date,DateTimeFormatter dateTimeFormatter){ LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime(); return localDateTime.format(dateTimeFormatter); }
public static LocalDate dateConvertLocalDate(Date date){ return date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDate(); }
public static LocalDateTime dateConvertLocalDateTime(Date date){ return date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime(); }
public static Date localDateConvertDate(LocalDate localDate){ Instant instant = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant(); return Date.from(instant); }
public static Date localDateTimeConvertDate(LocalDateTime localDateTime){ Instant instant = Instant.ofEpochSecond(localDateTime.toEpochSecond(ZoneOffset.ofHours(8))); return Date.from(instant); }
public static LocalDate getCurrentLocalDate() { return LocalDate.now(); }
public static LocalTime getCurrentLocalTime() { return LocalTime.now(); }
public static LocalDateTime getCurrentLocalDateTime() { return LocalDateTime.now(); }
public static String getCurrentDateStr() { return LocalDate.now().format(DATE_FORMATTER); }
public static String getCurrentShortDateStr() { return LocalDate.now().format(SHORT_DATE_FORMATTER); }
public static String getCurrentMonthStr() { return LocalDate.now().format(YEAR_MONTH_FORMATTER); }
public static String getCurrentDateTimeStr() { return LocalDateTime.now().format(DATETIME_FORMATTER); }
public static String getCurrentLongDateTimeStr(){ return LocalDateTime.now().format(LONG_DATETIME_FORMATTER); }
public static String getCurrentShortDateTimeStr() { return LocalDateTime.now().format(SHORT_DATETIME_FORMATTER); }
public static String getCurrentTimeStr() { return LocalTime.now().format(TIME_FORMATTER); }
public static String getCurrentDateTimeStr(String pattern) { return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern)); }
public static LocalDate parseLocalDate(String dateStr, String pattern) { return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern)); }
public static LocalDateTime parseLocalDateTime(String dateTimeStr, String pattern) { return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern)); }
public static LocalTime parseLocalTime(String timeStr, String pattern) { return LocalTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern)); }
public static String formatLocalDate(LocalDate date, String pattern) { return date.format(DateTimeFormatter.ofPattern(pattern)); }
public static String formatLocalDateTime(LocalDateTime datetime, String pattern) { return datetime.format(DateTimeFormatter.ofPattern(pattern)); }
public static String formatLocalTime(LocalTime time, String pattern) { return time.format(DateTimeFormatter.ofPattern(pattern)); }
public static LocalDate formatLocalDate(String dateStr,DateTimeFormatter dateTimeFormatter) { return LocalDate.parse(dateStr, dateTimeFormatter); }
public static LocalDateTime formatLocalDateTime(String dateTimeStr,DateTimeFormatter dateTimeFormatter) { return LocalDateTime.parse(dateTimeStr, dateTimeFormatter); }
public static LocalTime formatLocalTime(String longDateTimeStr,DateTimeFormatter dateTimeFormatter){ return LocalTime.parse(longDateTimeStr, dateTimeFormatter); }
public static long periodHours(LocalDateTime startDateTime,LocalDateTime endDateTime){ return Duration.between(startDateTime, endDateTime).get(ChronoUnit.SECONDS); }
public static long periodDays(LocalDate startDate, LocalDate endDate) { return startDate.until(endDate, ChronoUnit.DAYS); }
public static long periodWeeks(LocalDate startDate, LocalDate endDate) { return startDate.until(endDate, ChronoUnit.WEEKS); }
public static long periodMonths(LocalDate startDate, LocalDate endDate) { return startDate.until(endDate, ChronoUnit.MONTHS); }
public static long periodYears(LocalDate startDate, LocalDate endDate) { return startDate.until(endDate, ChronoUnit.YEARS); }
public static boolean isToday(LocalDate date) { return getCurrentLocalDate().equals(date); }
public static Long localDateTimeToEpochMilli(LocalDateTime dateTime) { return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); }
public static LocalDateTime epochMilliToLocalDateTime(Long timeMillis) { return (new Timestamp(timeMillis)).toLocalDateTime(); }
public static Timestamp localDateTimeToTimestamp(LocalDateTime dateTime) { return new Timestamp(dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); }
public static LocalDateTime localDateTimeToTimestamp(Timestamp timestamp) { return timestamp.toLocalDateTime(); }
public static boolean isLeapYear(LocalDate localDate){ return localDate.isLeapYear(); }
public static boolean isBefore(LocalDateTime localDateTimeA,LocalDateTime localDateTimeB){ return localDateTimeA.isBefore(localDateTimeB); }
public static boolean isAfter(LocalDateTime localDateTimeA,LocalDateTime localDateTimeB){ return localDateTimeA.isAfter(localDateTimeB); }
public static LocalDateTime getContractEndTimeByYear(LocalDateTime startTime,long years){ return startTime.plusYears(years).minusSeconds(1); }
public static LocalDateTime getContractEndTimeByMonth(LocalDateTime startTime,long monthValues){ return startTime.plusMonths(monthValues).minusSeconds(1); } }
|