1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp; 33 34 import java.sql.Timestamp; 35 import java.util.Calendar; 36 import java.util.Date; 37 import java.util.GregorianCalendar; 38 import java.util.TimeZone; 39 40 /** 41 * A set of utilities to assist in bridging the gap to Java 8. 42 * <p> 43 * This class is not found in Java SE 8 but provides methods that are. 44 */ 45 public final class DateTimeUtils { 46 47 /** 48 * Restricted constructor. 49 */ DateTimeUtils()50 private DateTimeUtils() { 51 } 52 53 //----------------------------------------------------------------------- 54 /** 55 * Converts a {@code java.util.Date} to an {@code Instant}. 56 * 57 * @param utilDate the util date, not null 58 * @return the instant, not null 59 */ toInstant(Date utilDate)60 public static Instant toInstant(Date utilDate) { 61 return Instant.ofEpochMilli(utilDate.getTime()); 62 } 63 64 /** 65 * Converts an {@code Instant} to a {@code java.util.Date}. 66 * <p> 67 * Fractions of the instant smaller than milliseconds will be dropped. 68 * 69 * @param instant the instant, not null 70 * @return the util date, not null 71 * @throws IllegalArgumentException if the conversion fails 72 */ toDate(Instant instant)73 public static Date toDate(Instant instant) { 74 try { 75 return new Date(instant.toEpochMilli()); 76 } catch (ArithmeticException ex) { 77 throw new IllegalArgumentException(ex); 78 } 79 } 80 81 //----------------------------------------------------------------------- 82 /** 83 * Converts a {@code Calendar} to an {@code Instant}. 84 * 85 * @param calendar the calendar, not null 86 * @return the instant, not null 87 */ toInstant(Calendar calendar)88 public static Instant toInstant(Calendar calendar) { 89 return Instant.ofEpochMilli(calendar.getTimeInMillis()); 90 } 91 92 /** 93 * Converts a {@code Calendar} to a {@code ZonedDateTime}. 94 * <p> 95 * Note that {@code GregorianCalendar} supports a Julian-Gregorian cutover 96 * date and {@code ZonedDateTime} does not so some differences will occur. 97 * 98 * @param calendar the calendar, not null 99 * @return the instant, not null 100 */ toZonedDateTime(Calendar calendar)101 public static ZonedDateTime toZonedDateTime(Calendar calendar) { 102 Instant instant = Instant.ofEpochMilli(calendar.getTimeInMillis()); 103 ZoneId zone = toZoneId(calendar.getTimeZone()); 104 return ZonedDateTime.ofInstant(instant, zone); 105 } 106 107 /** 108 * Converts a {@code ZonedDateTime} to a {@code Calendar}. 109 * <p> 110 * The resulting {@code GregorianCalendar} is pure Gregorian and uses 111 * ISO week definitions, starting on Monday and with 4 days in a minimal week. 112 * <p> 113 * Fractions of the instant smaller than milliseconds will be dropped. 114 * 115 * @param zdt the zoned date-time, not null 116 * @return the calendar, not null 117 * @throws IllegalArgumentException if the conversion fails 118 */ toGregorianCalendar(ZonedDateTime zdt)119 public static GregorianCalendar toGregorianCalendar(ZonedDateTime zdt) { 120 TimeZone zone = toTimeZone(zdt.getZone()); 121 GregorianCalendar cal = new GregorianCalendar(zone); 122 cal.setGregorianChange(new Date(Long.MIN_VALUE)); 123 cal.setFirstDayOfWeek(Calendar.MONDAY); 124 cal.setMinimalDaysInFirstWeek(4); 125 try { 126 cal.setTimeInMillis(zdt.toInstant().toEpochMilli()); 127 } catch (ArithmeticException ex) { 128 throw new IllegalArgumentException(ex); 129 } 130 return cal; 131 } 132 133 //----------------------------------------------------------------------- 134 /** 135 * Converts a {@code TimeZone} to a {@code ZoneId}. 136 * 137 * @param timeZone the time-zone, not null 138 * @return the zone, not null 139 */ toZoneId(TimeZone timeZone)140 public static ZoneId toZoneId(TimeZone timeZone) { 141 return ZoneId.of(timeZone.getID(), ZoneId.SHORT_IDS); 142 } 143 144 /** 145 * Converts a {@code ZoneId} to a {@code TimeZone}. 146 * 147 * @param zoneId the zone, not null 148 * @return the time-zone, not null 149 */ toTimeZone(ZoneId zoneId)150 public static TimeZone toTimeZone(ZoneId zoneId) { 151 String tzid = zoneId.getId(); 152 if (tzid.startsWith("+") || tzid.startsWith("-")) { 153 tzid = "GMT" + tzid; 154 } else if (tzid.equals("Z")) { 155 tzid = "UTC"; 156 } 157 return TimeZone.getTimeZone(tzid); 158 } 159 160 //----------------------------------------------------------------------- 161 /** 162 * Converts a {@code java.sql.Date} to a {@code LocalDate}. 163 * 164 * @param sqlDate the SQL date, not null 165 * @return the local date, not null 166 */ 167 @SuppressWarnings("deprecation") toLocalDate(java.sql.Date sqlDate)168 public static LocalDate toLocalDate(java.sql.Date sqlDate) { 169 return LocalDate.of(sqlDate.getYear() + 1900, sqlDate.getMonth() + 1, sqlDate.getDate()); 170 } 171 172 /** 173 * Converts a {@code LocalDate} to a {@code java.sql.Date}. 174 * 175 * @param date the local date, not null 176 * @return the SQL date, not null 177 */ 178 @SuppressWarnings("deprecation") toSqlDate(LocalDate date)179 public static java.sql.Date toSqlDate(LocalDate date) { 180 return new java.sql.Date(date.getYear() - 1900, date.getMonthValue() -1, date.getDayOfMonth()); 181 } 182 183 //----------------------------------------------------------------------- 184 /** 185 * Converts a {@code java.sql.Time} to a {@code LocalTime}. 186 * 187 * @param sqlTime the SQL time, not null 188 * @return the local time, not null 189 */ 190 @SuppressWarnings("deprecation") toLocalTime(java.sql.Time sqlTime)191 public static LocalTime toLocalTime(java.sql.Time sqlTime) { 192 return LocalTime.of(sqlTime.getHours(), sqlTime.getMinutes(), sqlTime.getSeconds()); 193 } 194 195 /** 196 * Converts a {@code LocalTime} to a {@code java.sql.Time}. 197 * 198 * @param time the local time, not null 199 * @return the SQL time, not null 200 */ 201 @SuppressWarnings("deprecation") toSqlTime(LocalTime time)202 public static java.sql.Time toSqlTime(LocalTime time) { 203 return new java.sql.Time(time.getHour(), time.getMinute(), time.getSecond()); 204 } 205 206 //----------------------------------------------------------------------- 207 /** 208 * Converts a {@code LocalDateTime} to a {@code java.sql.Timestamp}. 209 * 210 * @param dateTime the local date-time, not null 211 * @return the SQL timestamp, not null 212 */ 213 @SuppressWarnings("deprecation") toSqlTimestamp(LocalDateTime dateTime)214 public static Timestamp toSqlTimestamp(LocalDateTime dateTime) { 215 return new Timestamp( 216 dateTime.getYear() - 1900, 217 dateTime.getMonthValue() - 1, 218 dateTime.getDayOfMonth(), 219 dateTime.getHour(), 220 dateTime.getMinute(), 221 dateTime.getSecond(), 222 dateTime.getNano()); 223 } 224 225 /** 226 * Converts a {@code java.sql.Timestamp} to a {@code LocalDateTime}. 227 * 228 * @param sqlTimestamp the SQL timestamp, not null 229 * @return the local date-time, not null 230 */ 231 @SuppressWarnings("deprecation") toLocalDateTime(Timestamp sqlTimestamp)232 public static LocalDateTime toLocalDateTime(Timestamp sqlTimestamp) { 233 return LocalDateTime.of( 234 sqlTimestamp.getYear() + 1900, 235 sqlTimestamp.getMonth() + 1, 236 sqlTimestamp.getDate(), 237 sqlTimestamp.getHours(), 238 sqlTimestamp.getMinutes(), 239 sqlTimestamp.getSeconds(), 240 sqlTimestamp.getNanos()); 241 } 242 243 /** 244 * Converts an {@code Instant} to a {@code java.sql.Timestamp}. 245 * 246 * @param instant the instant, not null 247 * @return the SQL timestamp, not null 248 */ toSqlTimestamp(Instant instant)249 public static Timestamp toSqlTimestamp(Instant instant) { 250 try { 251 Timestamp ts = new Timestamp(instant.getEpochSecond() * 1000); 252 ts.setNanos(instant.getNano()); 253 return ts; 254 } catch (ArithmeticException ex) { 255 throw new IllegalArgumentException(ex); 256 } 257 } 258 259 /** 260 * Converts a {@code java.sql.Timestamp} to an {@code Instant}. 261 * 262 * @param sqlTimestamp the SQL timestamp, not null 263 * @return the instant, not null 264 */ toInstant(Timestamp sqlTimestamp)265 public static Instant toInstant(Timestamp sqlTimestamp) { 266 return Instant.ofEpochSecond(sqlTimestamp.getTime() / 1000, sqlTimestamp.getNanos()); 267 } 268 269 } 270