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
| package com.imaxun.demo.framework.common.util;
import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAdjusters; import java.util.Date;
public class DateTimeUtils { public static final DateTimeFormatter dateTimeDefaultFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static final DateTimeFormatter dateDefaultFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static final DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM"); public static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); public static final ZoneOffset defaultZoneOffset = ZoneOffset.systemDefault() .getRules().getOffset(Instant.now());
public static LocalDate now() { return LocalDate.now(); }
public static LocalDateTime nowTime() { return LocalDateTime.now(); }
public static long nowEpochSecond() { return Instant.now().getEpochSecond(); }
public static long nowEpochMillisecond() { return Instant.now().toEpochMilli(); }
public static long toEpochSecond(LocalDateTime time, ZoneOffset zoneOffset) { return time.toEpochSecond(zoneOffset); }
public static long toEpochSecond(LocalDateTime time) { return toEpochSecond(time, defaultZoneOffset); }
public static long toEpochSecondByDay(LocalDate time, ZoneOffset zoneOffset) { return toEpochSecond(LocalDateTime.of(time, LocalTime.parse("00:00:00")), zoneOffset); }
public static long toEpochSecondByDay(LocalDate time) { return toEpochSecondByDay(time, defaultZoneOffset); }
public static long toEpochMillisecondByDay(LocalDate time, ZoneOffset zoneOffset) { return toEpochMillisecond(LocalDateTime.of(time, LocalTime.parse("00:00:00")), zoneOffset); }
public static long toEpochMillisecondByDay(LocalDate time) { return toEpochMillisecond(LocalDateTime.of(time, LocalTime.parse("00:00:00")), defaultZoneOffset); }
public static long toEpochMillisecond(LocalDateTime time, ZoneOffset zoneOffset) { return time.toInstant(zoneOffset).toEpochMilli(); }
public static long toEpochMillisecond(LocalDateTime time) { return toEpochMillisecond(time, defaultZoneOffset); }
public static LocalDateTime ofEpochSecond(long second, ZoneOffset zoneOffset) { return second <= 0 ? null : LocalDateTime.ofEpochSecond(second, 0, zoneOffset); }
public static LocalDate ofEpochSecondToDate(long second, ZoneOffset zoneOffset) { return second <= 0 ? null : LocalDateTime.ofEpochSecond(second, 0, zoneOffset).toLocalDate(); }
public static LocalDateTime ofEpochSecond(long second) { return ofEpochSecond(second, defaultZoneOffset); }
public static LocalDate ofEpochSecondToDate(long second) { return ofEpochSecondToDate(second, defaultZoneOffset); }
public static LocalDateTime ofEpochMillisecond(long millisecond, ZoneOffset zoneOffset) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(millisecond), zoneOffset); }
public static LocalDateTime ofEpochMillisecond(long millisecond) { return ofEpochMillisecond(millisecond, defaultZoneOffset); }
public static LocalDate ofEpochMillisecondToDate(long millisecond, ZoneOffset zoneOffset) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(millisecond), zoneOffset).toLocalDate(); }
public static LocalDate ofEpochMillisecondToDate(long millisecond) { return ofEpochMillisecondToDate(millisecond, defaultZoneOffset); }
public static LocalDateTime parse(CharSequence text, String pattern) { return LocalDateTime.parse(text, DateTimeFormatter.ofPattern(pattern)); }
public static LocalDateTime parse(CharSequence text) { return LocalDateTime.parse(text, dateTimeDefaultFormatter); }
public static LocalDateTime parsePeriod(CharSequence text) { return parsePeriodDate(text).atStartOfDay(); }
public static LocalDate parseDate(CharSequence text, String pattern) { return LocalDate.parse(text, DateTimeFormatter.ofPattern(pattern)); }
public static LocalDate parseDate(CharSequence text) { return LocalDate.parse(text, dateDefaultFormatter); }
public static LocalDate parsePeriodDate(CharSequence text) { return LocalDate.parse(text, new DateTimeFormatterBuilder() .appendPattern("yyyy-MM") .parseDefaulting(ChronoField.DAY_OF_MONTH, 1) .toFormatter()); }
public static String toString(LocalDateTime time, String pattern) { if (time == null) return null; return DateTimeFormatter.ofPattern(pattern).format(time); }
public static String toString(LocalDateTime time) { if (time == null) return null; return toString(time, dateTimeDefaultFormatter.format(time)); }
public static String toString(LocalDate time, String pattern) { if (time == null) return null; return DateTimeFormatter.ofPattern(pattern).format(time); }
public static String toString(LocalDate time) { if (time == null) return null; return toString(time, dateDefaultFormatter.format(time)); }
public static String toMonth(LocalDateTime localDateTime) { String str = localDateTime.format(monthFormatter); return str; }
public static String toMonth(LocalDate localDate) { String str = localDate.format(monthFormatter); return str; }
public static String toTime(LocalDateTime localDateTime) { String str = localDateTime.format(timeFormatter); return str; }
public static LocalDateTime max(LocalDateTime src, LocalDateTime des) { return src.isAfter(des) ? src : des; }
public static LocalDateTime min(LocalDateTime src, LocalDateTime des) { return src.isBefore(des) ? src : des; }
public static LocalDate max(LocalDate src, LocalDate des) { return src.isAfter(des) ? src : des; }
public static LocalDate min(LocalDate src, LocalDate des) { return src.isBefore(des) ? src : des; }
public static LocalDate firstDateInMonth(LocalDate date) { return date.with(TemporalAdjusters.firstDayOfMonth()); }
public static LocalDate lastDateInMonth(LocalDate date) { return date.with(TemporalAdjusters.lastDayOfMonth()); }
public static LocalDateTime firstDateTimeInMonth(LocalDateTime date) { return date.with(TemporalAdjusters.firstDayOfMonth()); }
public static LocalDateTime lastDateTimeInMonth(LocalDateTime date) { return date.with(TemporalAdjusters.lastDayOfMonth()); }
public static Date localDateTime2Date(LocalDateTime localDateTime) { ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = localDateTime.atZone(zoneId); return Date.from(zdt.toInstant()); }
public static Date localDate2Date(LocalDate localDate) { return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); }
public static LocalDateTime date2LocalDateTime(Date date) { ZoneId zoneId = ZoneId.systemDefault(); return LocalDateTime.ofInstant(date.toInstant(), zoneId); }
public static LocalDate date2LocalDate(Date date) { ZoneId zoneId = ZoneId.systemDefault(); return LocalDateTime.ofInstant(date.toInstant(), zoneId).toLocalDate(); } }
|