001/* 002 * Copyright 2017 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.util; 017 018import java.time.Instant; 019import java.time.LocalDate; 020import java.time.LocalDateTime; 021import java.time.ZoneId; 022import java.time.ZonedDateTime; 023import java.util.Date; 024 025/** 026 * Helper for converting dates from the old ({@link Date}) to the new ({@link java.time}) JDK 027 * Special thanks to http://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate#answer-27378709 028 * which inspired this code 029 * 030 * See also http://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date 031 */ 032public final class DateUtils 033{ 034 private DateUtils() 035 { 036 // empty 037 } 038 039 /** 040 * Converts this {@link Date} object to an {@link Instant}. 041 * @param date The date object 042 * @return an instant representing the same point on the time-line as this {@link Date} object 043 */ 044 public static Instant asInstant(Date date) 045 { 046 return date == null ? null : date.toInstant(); 047 } 048 049 /** 050 * Converts this {@link Date} object to an {@link Instant}. 051 * @param date The date object 052 * @param zone The zone 053 * @return an instant representing the same point on the time-line as this {@link Date} object 054 */ 055 public static ZonedDateTime asZonedDateTime(Date date, ZoneId zone) 056 { 057 return date == null ? null : asInstant(date).atZone(zone != null ? zone : ZoneId.systemDefault()); 058 } 059 060 /** 061 * Converts this {@link Date} object to a {@link LocalDate}. 062 * 063 * This returns a {@link LocalDate} with the same year, month and day as this {@link Date}. 064 * @param date The date object 065 * @param zone The zone 066 * @return the {@link LocalDate} part of this {@link Date} 067 */ 068 public static LocalDate asLocalDate(Date date, ZoneId zone) 069 { 070 return asZonedDateTime(date, zone).toLocalDate(); 071 } 072 073 /** 074 * Converts this {@link Date} object to a {@link LocalDate}. 075 * 076 * This returns a {@link LocalDate} with the same year, month and day as this {@link Date}. 077 * @param date The date object 078 * @return the {@link LocalDate} part of this {@link Date} 079 */ 080 public static LocalDate asLocalDate(Date date) 081 { 082 return asLocalDate(date, ZoneId.systemDefault()); 083 } 084 085 /** 086 * Converts this {@link Date} object to a {@link LocalDateTime}. 087 * 088 * This returns a {@link LocalDateTime} with the same year, month, day and time as this {@link Date}. 089 * @param date The date object 090 * @param zone The zone 091 * @return the {@link LocalDateTime} part of this {@link Date} 092 */ 093 public static LocalDateTime asLocalDateTime(Date date, ZoneId zone) 094 { 095 return asZonedDateTime(date, zone).toLocalDateTime(); 096 } 097 098 /** 099 * Converts this {@link Date} object to a {@link LocalDateTime}. 100 * 101 * This returns a {@link LocalDateTime} with the same year, month, day and time as this {@link Date}. 102 * @param date The date object 103 * @return the {@link LocalDateTime} part of this {@link Date} 104 */ 105 public static LocalDateTime asLocalDateTime(Date date) 106 { 107 return asLocalDateTime(date, ZoneId.systemDefault()); 108 } 109 110 /** 111 * Converts this {@link LocalDate} object to a {@link Date}. 112 * 113 * @param localDate The local date object 114 * @return the {@link Date} part of this {@link LocalDate} 115 */ 116 public static Date asDate(LocalDate localDate) 117 { 118 return asDate(localDate, ZoneId.systemDefault()); 119 } 120 121 /** 122 * Converts this {@link LocalDate} object to a {@link Date}. 123 * 124 * @param localDate The local date object 125 * @param zone The zone 126 * @return the {@link Date} part of this {@link LocalDate} 127 */ 128 public static Date asDate(LocalDate localDate, ZoneId zone) 129 { 130 return Date.from(localDate.atStartOfDay().atZone(zone).toInstant()); 131 } 132 133 /** 134 * Converts this {@link LocalDateTime} object to a {@link Date}. 135 * 136 * @param localDateTime The local date time object 137 * @return the {@link Date} part of this {@link LocalDateTime} 138 */ 139 public static Date asDate(LocalDateTime localDateTime) 140 { 141 return asDate(localDateTime, ZoneId.systemDefault()); 142 } 143 144 /** 145 * Converts this {@link LocalDateTime} object to a {@link Date}. 146 * 147 * @param localDateTime The local date time object 148 * @param zone The zone 149 * @return the {@link Date} part of this {@link LocalDateTime} 150 */ 151 public static Date asDate(LocalDateTime localDateTime, ZoneId zone) 152 { 153 return Date.from(localDateTime.atZone(zone).toInstant()); 154 } 155}