从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转Date。下面是时间类互相转换大全,包含Instant、LocalDate、LocalDateTime、LocalTime、ZonedDateTime和Date的相互转换,时间转换大全,下面是一个工具类,仅供参考:
具体包含:
LocalDateTime转Date,LocalDate转Date,LocalTime转Date,Instant转Date,epochMilli毫秒转Date,ZonedDateTime转Date,Date转LocalDateTime,LocalDate转LocalDateTime,LocalTime转LocalDateTime,Instant转LocalDateTime,epochMilli毫秒转LocalDateTime,temporal转LocalDateTime,ZonedDateTime转LocalDateTime,Date转LocalDate,LocalDateTime转LocalDate,Instant转LocalDate,temporal转LocalDate,ZonedDateTime转LocalDate,Date转LocalTime,LocalDateTime转LocalTime,Instant转LocalTime,temporal转LocalTime,ZonedDateTime转LocalTime,Date转Instant,LocalDateTime转Instant,LocalDate转Instant,LocalTime转Instant,epochMilli毫秒转Instant,temporal转Instant,ZonedDateTime转Instant,Date转毫秒值,LocalDateTime转毫秒值,LocalDate转毫秒值,Instant转毫秒值,ZonedDateTime转毫秒值,Date转ZonedDateTime,LocalDateTime转ZonedDateTime,LocalDate转ZonedDateTime,LocalTime转ZonedDateTime,Instant转ZonedDateTime,epochMilli毫秒转ZonedDateTime,temporal转ZonedDateTime.
1package com.xkzhangsan.time.converter; 2 3import java.time.Instant; 4import java.time.LocalDate; 5import java.time.LocalDateTime; 6import java.time.LocalTime; 7import java.time.ZoneId; 8import java.time.ZonedDateTime; 9import java.time.temporal.TemporalAccessor; 10import java.util.Date; 11import java.util.Objects; 12 13/** 14 * 日期转换 15 * 包含Date、LocalDate、LocalDateTime、LocalTime、Instant和ZonedDateTime的互相转换 16 * 17 * 注意,ZonedDateTime相关的转换,尤其是其他时间转ZonedDateTime,要注意时间和对应时区一致。 18* @ClassName: DateTimeConverterUtil 19* @Description: DateTime Converter 20* @author xkzhangsan 21* @date 2019年12月1日 22* 23 */ 24public class DateTimeConverterUtil { 25 26 private DateTimeConverterUtil(){ 27 } 28 29 /** 30 * LocalDateTime转Date 31 * @param localDateTime 32 * @return 33 */ 34 public static Date toDate(LocalDateTime localDateTime) { 35 Objects.requireNonNull(localDateTime, "localDateTime"); 36 return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); 37 } 38 39 /** 40 * LocalDate转Date 41 * @param localDate 42 * @return 43 */ 44 public static Date toDate(LocalDate localDate) { 45 Objects.requireNonNull(localDate, "localDate"); 46 return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); 47 } 48 49 /** 50 * LocalTime转Date 51 * 以当天的日期+LocalTime组成新的LocalDateTime转换为Date 52 * @param localTime 53 * @return 54 */ 55 public static Date toDate(LocalTime localTime) { 56 Objects.requireNonNull(localTime, "localTime"); 57 return Date.from(LocalDate.now().atTime(localTime).atZone(ZoneId.systemDefault()).toInstant()); 58 } 59 60 /** 61 * Instant转Date 62 * @param instant 63 * @return 64 */ 65 public static Date toDate(Instant instant) { 66 return Date.from(instant); 67 } 68 69 /** 70 * epochMilli毫秒转Date 71 * @param epochMilli 72 * @return 73 */ 74 public static Date toDate(long epochMilli){ 75 Objects.requireNonNull(epochMilli, "epochMilli"); 76 return new Date(epochMilli); 77 } 78 79 /** 80 * ZonedDateTime转Date 81 * 注意时间对应的时区和默认时区差异 82 * @param zonedDateTime 83 * @return 84 */ 85 public static Date toDate(ZonedDateTime zonedDateTime) { 86 Objects.requireNonNull(zonedDateTime, "zonedDateTime"); 87 return Date.from(zonedDateTime.toInstant()); 88 } 89 90 /** 91 * Date转LocalDateTime 92 * @param date 93 * @return 94 */ 95 public static LocalDateTime toLocalDateTime(Date date) { 96 Objects.requireNonNull(date, "date"); 97 return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(); 98 } 99 100 /** 101 * LocalDate转LocalDateTime 102 * @param localDate 103 * @return 104 */ 105 public static LocalDateTime toLocalDateTime(LocalDate localDate) { 106 Objects.requireNonNull(localDate, "localDate"); 107 return localDate.atStartOfDay(); 108 } 109 110 /** 111 * LocalTime转LocalDateTime 112 * 以当天的日期+LocalTime组成新的LocalDateTime 113 * @param localTime 114 * @return 115 */ 116 public static LocalDateTime toLocalDateTime(LocalTime localTime) { 117 Objects.requireNonNull(localTime, "localTime"); 118 return LocalDate.now().atTime(localTime); 119 } 120 121 /** 122 * Instant转LocalDateTime 123 * @param instant 124 * @return 125 */ 126 public static LocalDateTime toLocalDateTime(Instant instant) { 127 return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); 128 } 129 130 /** 131 * epochMilli毫秒转LocalDateTime 132 * @param epochMilli 133 * @return 134 */ 135 public static LocalDateTime toLocalDateTime(long epochMilli) { 136 Objects.requireNonNull(epochMilli, "epochMilli"); 137 return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault()); 138 } 139 140 /** 141 * temporal转LocalDateTime 142 * @param temporal 143 * @return 144 */ 145 public static LocalDateTime toLocalDateTime(TemporalAccessor temporal) { 146 return LocalDateTime.from(temporal); 147 } 148 149 /** 150 * ZonedDateTime转LocalDateTime 151 * 注意时间对应的时区和默认时区差异 152 * @param zonedDateTime 153 * @return 154 */ 155 public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime) { 156 Objects.requireNonNull(zonedDateTime, "zonedDateTime"); 157 return zonedDateTime.toLocalDateTime(); 158 } 159 160 /** 161 * Date转LocalDate 162 * @param date 163 * @return 164 */ 165 public static LocalDate toLocalDate(Date date) { 166 return toLocalDateTime(date).toLocalDate(); 167 } 168 169 /** 170 * LocalDateTime转LocalDate 171 * @param localDateTime 172 * @return 173 */ 174 public static LocalDate toLocalDate(LocalDateTime localDateTime) { 175 Objects.requireNonNull(localDateTime, "localDateTime"); 176 return localDateTime.toLocalDate(); 177 } 178 179 /** 180 * Instant转LocalDate 181 * @param instant 182 * @return 183 */ 184 public static LocalDate toLocalDate(Instant instant) { 185 return toLocalDateTime(instant).toLocalDate(); 186 } 187 188 /** 189 * temporal转LocalDate 190 * @param temporal 191 * @return 192 */ 193 public static LocalDate toLocalDate(TemporalAccessor temporal) { 194 return LocalDate.from(temporal); 195 } 196 197 /** 198 * ZonedDateTime转LocalDate 199 * 注意时间对应的时区和默认时区差异 200 * @param zonedDateTime 201 * @return 202 */ 203 public static LocalDate toLocalDate(ZonedDateTime zonedDateTime) { 204 Objects.requireNonNull(zonedDateTime, "zonedDateTime"); 205 return zonedDateTime.toLocalDate(); 206 } 207 208 /** 209 * Date转LocalTime 210 * @param date 211 * @return 212 */ 213 public static LocalTime toLocalTime(Date date) { 214 return toLocalDateTime(date).toLocalTime(); 215 } 216 217 /** 218 * LocalDateTime转LocalTime 219 * @param localDateTime 220 * @return 221 */ 222 public static LocalTime toLocalTime(LocalDateTime localDateTime) { 223 Objects.requireNonNull(localDateTime, "localDateTime"); 224 return localDateTime.toLocalTime(); 225 } 226 227 /** 228 * Instant转LocalTime 229 * @param instant 230 * @return 231 */ 232 public static LocalTime toLocalTime(Instant instant) { 233 return toLocalDateTime(instant).toLocalTime(); 234 } 235 236 /** 237 * temporal转LocalTime 238 * @param temporal 239 * @return 240 */ 241 public static LocalTime toLocalTime(TemporalAccessor temporal) { 242 return LocalTime.from(temporal); 243 } 244 245 /** 246 * ZonedDateTime转LocalTime 247 * 注意时间对应的时区和默认时区差异 248 * @param zonedDateTime 249 * @return 250 */ 251 public static LocalTime toLocalTime(ZonedDateTime zonedDateTime) { 252 Objects.requireNonNull(zonedDateTime, "zonedDateTime"); 253 return zonedDateTime.toLocalTime(); 254 } 255 256 /** 257 * Date转Instant 258 * @param date 259 * @return 260 */ 261 public static Instant toInstant(Date date) { 262 Objects.requireNonNull(date, "date"); 263 return date.toInstant(); 264 } 265 266 /** 267 * LocalDateTime转Instant 268 * @param localDateTime 269 * @return 270 */ 271 public static Instant toInstant(LocalDateTime localDateTime) { 272 Objects.requireNonNull(localDateTime, "localDateTime"); 273 return localDateTime.atZone(ZoneId.systemDefault()).toInstant(); 274 } 275 276 /** 277 * LocalDate转Instant 278 * @param localDate 279 * @return 280 */ 281 public static Instant toInstant(LocalDate localDate) { 282 return toLocalDateTime(localDate).atZone(ZoneId.systemDefault()).toInstant(); 283 } 284 285 /** 286 * LocalTime转Instant 287 * 以当天的日期+LocalTime组成新的LocalDateTime转换为Instant 288 * @param localTime 289 * @return 290 */ 291 public static Instant toInstant(LocalTime localTime) { 292 return toLocalDateTime(localTime).atZone(ZoneId.systemDefault()).toInstant(); 293 } 294 295 /** 296 * epochMilli毫秒转Instant 297 * @param epochMilli 298 * @return 299 */ 300 public static Instant toInstant(long epochMilli) { 301 Objects.requireNonNull(epochMilli, "epochMilli"); 302 return Instant.ofEpochMilli(epochMilli); 303 } 304 305 /** 306 * temporal转Instant 307 * @param temporal 308 * @return 309 */ 310 public static Instant toInstant(TemporalAccessor temporal) { 311 return Instant.from(temporal); 312 } 313 314 /** 315 * ZonedDateTime转Instant 316 * 注意时间对应的时区和默认时区差异 317 * @param zonedDateTime 318 * @return 319 */ 320 public static Instant toInstant(ZonedDateTime zonedDateTime) { 321 Objects.requireNonNull(zonedDateTime, "zonedDateTime"); 322 return zonedDateTime.toInstant(); 323 } 324 325 /** 326 * Date转毫秒值 327 * 从1970-01-01T00:00:00Z开始的毫秒值 328 * @param date 329 * @return 330 */ 331 public static long toEpochMilli(Date date){ 332 Objects.requireNonNull(date, "date"); 333 return date.getTime(); 334 } 335 336 /** 337 * LocalDateTime转毫秒值 338 * 从1970-01-01T00:00:00Z开始的毫秒值 339 * @param localDateTime 340 * @return 341 */ 342 public static long toEpochMilli(LocalDateTime localDateTime){ 343 return toInstant(localDateTime).toEpochMilli(); 344 } 345 346 /** 347 * LocalDate转毫秒值 348 * 从1970-01-01T00:00:00Z开始的毫秒值 349 * @param localDate 350 * @return 351 */ 352 public static long toEpochMilli(LocalDate localDate){ 353 return toInstant(localDate).toEpochMilli(); 354 } 355 356 /** 357 * Instant转毫秒值 358 * 从1970-01-01T00:00:00Z开始的毫秒值 359 * @param instant 360 * @return 361 */ 362 public static long toEpochMilli(Instant instant){ 363 Objects.requireNonNull(instant, "instant"); 364 return instant.toEpochMilli(); 365 } 366 367 /** 368 * ZonedDateTime转毫秒值,注意时间对应的时区和默认时区差异 369 * 从1970-01-01T00:00:00Z开始的毫秒值 370 * @param zonedDateTime 371 * @return 372 */ 373 public static long toEpochMilli(ZonedDateTime zonedDateTime) { 374 Objects.requireNonNull(zonedDateTime, "zonedDateTime"); 375 return zonedDateTime.toInstant().toEpochMilli(); 376 } 377 378 /** 379 * Date转ZonedDateTime,时区为系统默认时区 380 * @param date 381 * @return 382 */ 383 public static ZonedDateTime toZonedDateTime(Date date) { 384 Objects.requireNonNull(date, "date"); 385 return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime() 386 .atZone(ZoneId.systemDefault()); 387 } 388 389 /** 390 * LocalDateTime转ZonedDateTime,时区为系统默认时区 391 * @param localDateTime 392 * @return 393 */ 394 public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime) { 395 Objects.requireNonNull(localDateTime, "localDateTime"); 396 return localDateTime.atZone(ZoneId.systemDefault()); 397 }
/**
* LocalDateTime转ZonedDateTime,时区为zoneId对应时区
* 注意,需要保证localDateTime和zoneId是对应的,不然会出现错误
*
* @param localDateTime
* @param zoneId
* @return
*/
public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime, String zoneId) {
Objects.requireNonNull(localDateTime, "localDateTime");
Objects.requireNonNull(zoneId, "zoneId");
return localDateTime.atZone(ZoneId.of(zoneId));
}
1/** 2 * LocalDate转ZonedDateTime,时区为系统默认时区 3 * @param localDate 4 * @return such as 2020-02-19T00:00+08:00[Asia/Shanghai] 5 */ 6 public static ZonedDateTime toZonedDateTime(LocalDate localDate) { 7 Objects.requireNonNull(localDate, "localDate"); 8 return localDate.atStartOfDay().atZone(ZoneId.systemDefault()); 9 } 10 11 /** 12 * LocalTime转ZonedDateTime 13 * 以当天的日期+LocalTime组成新的ZonedDateTime,时区为系统默认时区 14 * @param localTime 15 * @return 16 */ 17 public static ZonedDateTime toZonedDateTime(LocalTime localTime) { 18 Objects.requireNonNull(localTime, "localTime"); 19 return LocalDate.now().atTime(localTime).atZone(ZoneId.systemDefault()); 20 } 21 22 /** 23 * Instant转ZonedDateTime,时区为系统默认时区 24 * @param instant 25 * @return 26 */ 27 public static ZonedDateTime toZonedDateTime(Instant instant) { 28 return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).atZone(ZoneId.systemDefault()); 29 } 30 31 /** 32 * epochMilli毫秒转ZonedDateTime,时区为系统默认时区 33 * @param epochMilli 34 * @return 35 */ 36 public static ZonedDateTime toZonedDateTime(long epochMilli) { 37 Objects.requireNonNull(epochMilli, "epochMilli"); 38 return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault()) 39 .atZone(ZoneId.systemDefault()); 40 } 41 42 /** 43 * temporal转ZonedDateTime,时区为系统默认时区 44 * @param temporal 45 * @return 46 */ 47 public static ZonedDateTime toZonedDateTime(TemporalAccessor temporal) { 48 return LocalDateTime.from(temporal).atZone(ZoneId.systemDefault()); 49 } 50 51}
测试代码
1package com.xkzhangsan.time.test; 2 3import java.time.Instant; 4import java.time.LocalDate; 5import java.time.LocalDateTime; 6import java.time.LocalTime; 7import java.time.ZonedDateTime; 8import java.util.Date; 9 10import org.junit.Test; 11 12import com.xkzhangsan.time.converter.DateTimeConverterUtil; 13 14public class ConverterTest { 15 16 @Test 17 public void dateConverterTest(){ 18 System.out.println("===================dateConverterTest====================="); 19 Date date = new Date(); 20 System.out.println(DateTimeConverterUtil.toLocalDateTime(date)); 21 System.out.println(DateTimeConverterUtil.toLocalDate(date)); 22 System.out.println(DateTimeConverterUtil.toLocalTime(date)); 23 System.out.println(DateTimeConverterUtil.toInstant(date)); 24 } 25 26 @Test 27 public void localDateTimeConverterTest(){ 28 System.out.println("===================localDateTimeConverterTest====================="); 29 LocalDateTime ldt = LocalDateTime.now(); 30 System.out.println(ldt); 31 System.out.println(DateTimeConverterUtil.toDate(ldt)); 32 System.out.println(DateTimeConverterUtil.toLocalDate(ldt)); 33 System.out.println(DateTimeConverterUtil.toLocalTime(ldt)); 34 System.out.println(DateTimeConverterUtil.toInstant(ldt)); 35 System.out.println(DateTimeConverterUtil.toZonedDateTime(ldt)); 36 } 37 38 @Test 39 public void localDateConverterTest(){ 40 System.out.println("===================localDateConverterTest====================="); 41 LocalDate ld = LocalDate.now(); 42 System.out.println(ld); 43 System.out.println(DateTimeConverterUtil.toDate(ld)); 44 System.out.println(DateTimeConverterUtil.toLocalDateTime(ld)); 45 System.out.println(DateTimeConverterUtil.toInstant(ld)); 46 } 47 48 @Test 49 public void localTimeConverterTest(){ 50 System.out.println("===================localTimeConverterTest====================="); 51 LocalTime lt = LocalTime.now(); 52 System.out.println(lt); 53 System.out.println(DateTimeConverterUtil.toDate(lt)); 54 System.out.println(DateTimeConverterUtil.toLocalDateTime(lt)); 55 System.out.println(DateTimeConverterUtil.toLocalTime(lt)); 56 System.out.println(DateTimeConverterUtil.toInstant(lt)); 57 } 58 59 60 @Test 61 public void instantConverterTest(){ 62 System.out.println("===================instantConverterTest====================="); 63 Instant instant = Instant.now(); 64 System.out.println(instant); 65 System.out.println(DateTimeConverterUtil.toDate(instant)); 66 System.out.println(DateTimeConverterUtil.toLocalDateTime(instant)); 67 System.out.println(DateTimeConverterUtil.toLocalDate(instant)); 68 } 69 70 @Test 71 public void zonedDateTimeConverterTest(){ 72 System.out.println("===================zonedDateTimeConverterTest====================="); 73 System.out.println("===================ToOther====================="); 74 ZonedDateTime zonedDateTime = ZonedDateTime.now(); 75 System.out.println(zonedDateTime); 76 System.out.println(DateTimeConverterUtil.toDate(zonedDateTime)); 77 System.out.println(DateTimeConverterUtil.toLocalDateTime(zonedDateTime)); 78 System.out.println(DateTimeConverterUtil.toLocalDate(zonedDateTime)); 79 System.out.println(DateTimeConverterUtil.toLocalTime(zonedDateTime)); 80 System.out.println(DateTimeConverterUtil.toInstant(zonedDateTime)); 81 System.out.println("===================toZonedDateTime====================="); 82 System.out.println(zonedDateTime); 83 System.out.println(DateTimeConverterUtil.toZonedDateTime(new Date())); 84 System.out.println(DateTimeConverterUtil.toZonedDateTime(LocalDateTime.now())); 85 System.out.println(DateTimeConverterUtil.toZonedDateTime(LocalDate.now())); 86 System.out.println(DateTimeConverterUtil.toZonedDateTime(LocalTime.now())); 87 System.out.println(DateTimeConverterUtil.toZonedDateTime(Instant.now())); 88 } 89}
输出:
1===================dateConverterTest===================== 22020-02-19T16:10:04.366 32020-02-19 416:10:04.366 52020-02-19T08:10:04.366Z 6===================localTimeConverterTest===================== 716:10:04.458 8Wed Feb 19 16:10:04 CST 2020 92020-02-19T16:10:04.458 1016:10:04.458 112020-02-19T08:10:04.458Z 12===================localDateConverterTest===================== 132020-02-19 14Wed Feb 19 00:00:00 CST 2020 152020-02-19T00:00 162020-02-18T16:00:00Z 17===================zonedDateTimeConverterTest===================== 18===================ToOther===================== 192020-02-19T16:10:04.464+08:00[Asia/Shanghai] 20Wed Feb 19 16:10:04 CST 2020 212020-02-19T16:10:04.464 222020-02-19 2316:10:04.464 242020-02-19T08:10:04.464Z 25===================toZonedDateTime===================== 262020-02-19T16:10:04.464+08:00[Asia/Shanghai] 272020-02-19T16:10:04.465+08:00[Asia/Shanghai] 282020-02-19T16:10:04.465+08:00[Asia/Shanghai] 292020-02-19T00:00+08:00[Asia/Shanghai] 302020-02-19T16:10:04.466+08:00[Asia/Shanghai] 312020-02-19T16:10:04.466+08:00[Asia/Shanghai] 32===================localDateTimeConverterTest===================== 332020-02-19T16:10:04.466 34Wed Feb 19 16:10:04 CST 2020 352020-02-19 3616:10:04.466 372020-02-19T08:10:04.466Z 382020-02-19T16:10:04.466+08:00[Asia/Shanghai] 39===================instantConverterTest===================== 402020-02-19T08:10:04.467Z 41Wed Feb 19 16:10:04 CST 2020 422020-02-19T16:10:04.467 432020-02-19