Skip to content

Commit

Permalink
Fix LocalDate and LocalDateTime conversion to Date
Browse files Browse the repository at this point in the history
  • Loading branch information
radovanradic committed Nov 24, 2024
1 parent 79a6244 commit 19c738b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ public void register(MutableConversionService conversionService) {
addTemporalToDateConverter(conversionService, ZonedDateTime.class, ZonedDateTime::toInstant);
// these two are a bit icky, but required for yaml parsing compatibility
// TODO Micronaut 4 Consider deletion
addTemporalToDateConverter(conversionService, LocalDate.class, ld -> ld.atTime(0, 0).toInstant(ZoneOffset.UTC));
addTemporalToDateConverter(conversionService, LocalDateTime.class, ldt -> ldt.toInstant(ZoneOffset.UTC));
addTemporalToDateConverter(conversionService, LocalDate.class, ld -> ld.atStartOfDay(ZoneId.systemDefault()).toInstant());
addTemporalToDateConverter(conversionService, LocalDateTime.class, ldt -> ldt.atZone(ZoneId.systemDefault()).toInstant());
}

private <T extends TemporalAccessor> void addTemporalStringConverters(MutableConversionService conversionService, Class<T> temporalType, DateTimeFormatter isoFormatter, TemporalQuery<T> query) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.micronaut.runtime.converters

import io.micronaut.context.ApplicationContext
import io.micronaut.core.convert.ConversionService
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

import java.text.DateFormat
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

class TimeConverterRegistrarSpec extends Specification {

@AutoCleanup
@Shared
ApplicationContext context = ApplicationContext.run()

static final String DATE_FORMAT = "yyyy-MM-dd"
static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm"
static final String TEST_TIME_ZONE = "America/New_York"

void 'test converting LocalDate and LocalDateTime'() {
given:
def timeZone = TimeZone.default
TimeZone.default = TimeZone.getTimeZone(TEST_TIME_ZONE)
def conversionService = context.getBean(ConversionService)
def localDate = LocalDate.of(2024, 1, 1)
def localDateTime = LocalDateTime.of(2024, 1, 1, 2, 30)
def localDateStr = createDateTimeFormatter(DATE_FORMAT).format(localDate)
def localDateTimeStr = createDateTimeFormatter(DATE_TIME_FORMAT).format(localDateTime)
when:
def date = conversionService.convert(localDate, Date).orElse(null)
def dateTime = conversionService.convert(localDateTime, Date).orElse(null)
then:
def dateStr = createDateFormat(DATE_FORMAT).format(date)
dateStr == localDateStr
def dateTimeStr = new SimpleDateFormat(DATE_TIME_FORMAT).format(dateTime)
dateTimeStr == localDateTimeStr
cleanup:
TimeZone.default = timeZone
}

static DateTimeFormatter createDateTimeFormatter(String pattern) {
return DateTimeFormatter.ofPattern(pattern)
}

static DateFormat createDateFormat(String pattern) {
return new SimpleDateFormat(pattern)
}
}

0 comments on commit 19c738b

Please sign in to comment.