LocalDateTime.MIN to Date, LocalDateTime.MAX to Date -> java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow

2023. 1. 6. 20:58ยท Today I Learned/2023
๋ชฉ์ฐจ
  1. LocalDateTime.MIN to Date, LocalDateTime.MAX to Date -> java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow
  2. ๐Ÿ“’ LocalDate(LocalDateTime) to Date
  3. ๐Ÿง LocalDateTime.MAX -> Instant -> Date ์—๋Ÿฌ ๋ฐœ์ƒ ์ด์œ (MIN๋„ ๋™์ผํ•œ ์—๋Ÿฌ)
  4. ๐Ÿ™‹โ€โ™€๏ธ LocalDateTime์˜ MIN๊ณผ MAX ๊ฐ’
  5. ๐Ÿ™‹โ€โ™€๏ธ  Instant์˜ MIN๊ณผ MAX ๊ฐ’
  6. ๊ฒฐ๋ก 
  7. ๐Ÿ‘ Reference
๋ฐ˜์‘ํ˜•

LocalDateTime.MIN to Date, LocalDateTime.MAX to Date -> java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow

  1. java.util.Date ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ์ด์œ ๊ฐ€ ์ˆ˜๋„ ์—†์ด ๋งŽ๋‹ค๋Š” ์ ์„ ์•Œ์ง€๋งŒ, ์–ด์ฉ” ์ˆ˜ ์—†์ด Date ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ๋‹ค. (๋‚˜์˜ ๊ฒฝ์šฐ๋Š” ํšŒ์‚ฌ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ผ์‹œ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ Date๋ฅผ ์‚ฌ์šฉ์ค‘์ด๊ณ (ใ…Žใ…Ž), ๊ทธ ์™ธ์—๋„ ์ด์œ ๊ฐ€ ์žˆ์—ˆ๋‹ค)
  2. ์•„๋ฌดํŠผ, ๊ทธ๋ž˜์„œ LocalDateTime์„ Date๋กœ ๋ณ€ํ™˜ํ•˜๋‹ค๋ณด๋‹ˆ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ด ์ด๋ฅผ ๊ณต์œ ํ•˜๊ณ ์ž ํ•œ๋‹ค.

 

๐Ÿ“’ LocalDate(LocalDateTime) to Date

// Timestamp์˜ valueOf ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
// Timestamp๊ฐ€ Date์„ ์ƒ์† ๋ฐ›์Œ
public Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
    return java.sql.Timestamp.valueOf(dateToConvert);
}

// Date.from ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
// LocalDate๋ฅผ Instant ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๊ณ  Date.from(Instant instant) ์ด์šฉํ•ด์„œ ๋ณ€ํ™˜
Date convertToDateViaInstant(LocalDateTime dateToConvert) {
    return java.util.Date
      .from(dateToConvert.atZone(ZoneId.systemDefault())
      .toInstant());
}
  • LocalDate(LocalDateTime)์„ Date๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํฌ๊ฒŒ ๋‘๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    1. Timestamp์˜ valueOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด Timestamp ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ด๋•Œ Timestamp๋Š” Date๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Date๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    2. Instant ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ ํ›„ Date.from ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด Date ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ์ด๋•Œ 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ „ํ™˜ ์‹œ java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 
  • ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด 1๋ฒˆ์งธ ๋ฐฉ๋ฒ•๋„ ์ •์ƒ์ ์œผ๋กœ ๊ฐ’์ด ๋ณ€ํ™˜๋˜๋Š”๊ฑด ์•„๋‹ˆ๋‹ค. (์˜ค๋ฅ˜๋งŒ ์•ˆ๋‚ ๋ฟ์ด์ง€ overflow๊ฐ€ ๋ฐœ์ƒํ•ด ๊ฐ’์ด ์ด์ƒํ•˜๊ฒŒ ์ €์žฅ๋œ๋‹ค.)

 

๐Ÿง LocalDateTime.MAX -> Instant -> Date ์—๋Ÿฌ ๋ฐœ์ƒ ์ด์œ (MIN๋„ ๋™์ผํ•œ ์—๋Ÿฌ)

@Test
public void convertToDateViaInstant_MAX() {
    LocalDateTime MAX_LOCAL_DATE_TIME = LocalDateTime.MAX;
    System.out.println("MAX_LOCAL_DATE_TIME = " + MAX_LOCAL_DATE_TIME); // 1

    Instant instant = MAX_LOCAL_DATE_TIME.atZone(ZoneId.systemDefault()).toInstant();
    System.out.println("instant = " + instant); // 2

    Date date = Date.from(instant);
    System.out.println("date = " + date); // 3
}

// 1
MAX_LOCAL_DATE_TIME = +999999999-12-31T23:59:59.999999999

// 2
instant = +999999999-12-31T14:59:59.999999999Z

// 3
java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow
  • ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด์•˜์„ ๋•Œ, ์ผ๋‹จ LocalDateTime.MAX -> Instant ๊ฐ์ฒด๋กœ์˜ ๋ณ€ํ™˜์€ ์„ฑ๊ณต์ ์œผ๋กœ ๋ณด์ธ๋‹ค.
  • Instant -> Date๋กœ ๋ณ€ํ™˜ ํ–ˆ์„ ๋•Œ long overflow๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ์ด๋Š”, Date๊ฐ€ Instant๋ณด๋‹ค ๋” ์ข์€ ๋ฒ”์œ„์˜ ์ผ์‹œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Date.from docs์— ๋ณด๋ฉด Exception์ด ๋ฐœ์ƒํ• ๊ฑฐ๋ผ๊ณ  ์„ค๋ช…๋„ ๋˜์–ด์žˆ๋‹ค. 
    • https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#from-java.time.Instant-

 

๐Ÿ™‹โ€โ™€๏ธ LocalDateTime์˜ MIN๊ณผ MAX ๊ฐ’

/**
 * The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.
 * This is the local date-time of midnight at the start of the minimum date.
 * This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
 * This could be used by an application as a "far past" date-time.
 */
public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);
/**
 * The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
 * This is the local date-time just before midnight at the end of the maximum date.
 * This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.
 * This could be used by an application as a "far future" date-time.
 */
public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);
  • MIN = -99999999๋…„ 01์›” 01์ผ 00:00:00
  • MAX = 99999999๋…„ 12์›” 31์ผ 23:59:59.999999999

 

๐Ÿ™‹โ€โ™€๏ธ  Instant์˜ MIN๊ณผ MAX ๊ฐ’

/**
 * The minimum supported {@code Instant}, '-1000000000-01-01T00:00Z'.
 * This could be used by an application as a "far past" instant.
 * <p>
 * This is one year earlier than the minimum {@code LocalDateTime}.
 * This provides sufficient values to handle the range of {@code ZoneOffset}
 * which affect the instant in addition to the local date-time.
 * The value is also chosen such that the value of the year fits in
 * an {@code int}.
 */
public static final Instant MIN = Instant.ofEpochSecond(MIN_SECOND, 0);
/**
 * The maximum supported {@code Instant}, '1000000000-12-31T23:59:59.999999999Z'.
 * This could be used by an application as a "far future" instant.
 * <p>
 * This is one year later than the maximum {@code LocalDateTime}.
 * This provides sufficient values to handle the range of {@code ZoneOffset}
 * which affect the instant in addition to the local date-time.
 * The value is also chosen such that the value of the year fits in
 * an {@code int}.
 */
public static final Instant MAX = Instant.ofEpochSecond(MAX_SECOND, 999_999_999);
  • MIN = -1000000000๋…„ 01์›”  01์ผ T00:00Z
  • MAX = 1000000000๋…„ 12์›” 31์ผ T23:59:59.999999999Z

 

๊ฒฐ๋ก 

  • Date ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง์ž~; ^^
  • ๋งŒ์•ฝ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด, LocalDate ๋“ฑ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋“ค๊ณผ ์ผ์‹œ ์ €์žฅ ๋ฒ”์œ„๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ์ ์„ ์ƒ๊ฐํ•˜๊ณ  ์‚ฌ์šฉํ•˜์ž!

 

๐Ÿ‘ Reference

๋”๋ณด๊ธฐ

https://www.baeldung.com/java-date-to-localdate-and-localdatetime

https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#Date--

 

Date (Java Platform SE 8 )

The class Date represents a specific instant in time, with millisecond precision. Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed t

docs.oracle.com

https://stackoverflow.com/questions/35149884/convert-localdate-max-to-date

 

๋ฐ˜์‘ํ˜•
  1. LocalDateTime.MIN to Date, LocalDateTime.MAX to Date -> java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow
  2. ๐Ÿ“’ LocalDate(LocalDateTime) to Date
  3. ๐Ÿง LocalDateTime.MAX -> Instant -> Date ์—๋Ÿฌ ๋ฐœ์ƒ ์ด์œ (MIN๋„ ๋™์ผํ•œ ์—๋Ÿฌ)
  4. ๐Ÿ™‹โ€โ™€๏ธ LocalDateTime์˜ MIN๊ณผ MAX ๊ฐ’
  5. ๐Ÿ™‹โ€โ™€๏ธ  Instant์˜ MIN๊ณผ MAX ๊ฐ’
  6. ๊ฒฐ๋ก 
  7. ๐Ÿ‘ Reference
'Today I Learned/2023' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [@Builder] ์ƒ์†ํ•œ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋„ ์‚ฌ์šฉํ•ด์„œ ๋นŒ๋” ์ƒ์„ฑํ•˜๊ธฐ, @SuperBuilder
  • Java Switch NPE ํ”ผํ•˜๊ธฐ
  • [Mapstruct] LocalDateTime <-> Date - Timezone ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•
  • git remote branch
YURI๐Ÿ•๐Ÿ“๐Ÿถ
YURI๐Ÿ•๐Ÿ“๐Ÿถ
๋ฐ˜์‘ํ˜•
YURI๐Ÿ•๐Ÿ“๐Ÿถ
๐Ÿ•
YURI๐Ÿ•๐Ÿ“๐Ÿถ
์ „์ฒด
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (96)
    • Today I Learned (0)
      • 2022 (16)
      • 2023 (6)
      • 2024 (0)
    • Project (0)
    • Study (41)
      • OOP (1)
      • Java (2)
      • Spring (22)
      • Kafka (3)
      • Web (1)
      • Network (4)
      • MSA (2)
      • ETC (6)
    • Algorithm (30)
      • ๐Ÿ‘ ๋ฌธ์ œ (30)
    • Book (1)
    • Daily Life (0)

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

hELLO ยท Designed By ์ •์ƒ์šฐ.v4.2.0
YURI๐Ÿ•๐Ÿ“๐Ÿถ
LocalDateTime.MIN to Date, LocalDateTime.MAX to Date -> java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.