-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($Java): handle LocalDateTime, LocalDate and LocalTime
- Loading branch information
1 parent
388d5da
commit 778e152
Showing
8 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package com.jmsoftware.maf.springcloudstarter.configuration; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; | ||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; | ||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; | ||
import org.springframework.boot.autoconfigure.jackson.JacksonProperties; | ||
import org.springframework.context.annotation.Bean; | ||
|
@@ -16,7 +19,7 @@ | |
* @author Johnny Miller (锺俊), email: [email protected], date: 6/27/2021 3:57 PM | ||
**/ | ||
@RequiredArgsConstructor | ||
public class LocalDateTimeSerializerConfiguration { | ||
public class JacksonConfiguration { | ||
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; | ||
private final JacksonProperties jacksonProperties; | ||
|
||
|
@@ -31,6 +34,14 @@ public LocalDateTimeSerializer localDateTimeSerializer() { | |
|
||
@Bean | ||
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(LocalDateTimeSerializer localDateTimeSerializer) { | ||
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeSerializer); | ||
var pattern = DEFAULT_DATE_FORMAT; | ||
if (StrUtil.isNotBlank(jacksonProperties.getDateFormat())) { | ||
pattern = jacksonProperties.getDateFormat(); | ||
} | ||
val localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern)); | ||
return jackson2ObjectMapperBuilder -> jackson2ObjectMapperBuilder | ||
.serializerByType(LocalDateTime.class, localDateTimeSerializer) | ||
.deserializerByType(LocalDateTime.class, localDateTimeDeserializer) | ||
.serializerByType(Long.class, ToStringSerializer.instance); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...java/com/jmsoftware/maf/springcloudstarter/configuration/TypeConversionConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.jmsoftware.maf.springcloudstarter.configuration; | ||
|
||
import cn.hutool.core.date.LocalDateTimeUtil; | ||
import cn.hutool.core.util.StrUtil; | ||
import com.jmsoftware.maf.springcloudstarter.constant.UniversalDateTime; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.convert.converter.Converter; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
/** | ||
* Description: TypeConvesionConfiguration, change description here. | ||
* | ||
* @author Johnny Miller (锺俊), email: [email protected], date: 6/27/2021 9:36 PM | ||
**/ | ||
@Slf4j | ||
public class TypeConversionConfiguration { | ||
@Bean | ||
public StringToLocalDateConverter localDateConverter() { | ||
return source -> { | ||
if (StrUtil.isBlank(source)) { | ||
return null; | ||
} | ||
return LocalDateTimeUtil.parseDate(source, UniversalDateTime.DATE_FORMAT); | ||
}; | ||
} | ||
|
||
@Bean | ||
public StringToLocalTimeConverter localTimeConverter() { | ||
return source -> { | ||
if (StrUtil.isBlank(source)) { | ||
return null; | ||
} | ||
return LocalTime.parse(source, DateTimeFormatter.ofPattern(UniversalDateTime.TIME_FORMAT)); | ||
}; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnBean(name = "requestMappingHandlerAdapter") | ||
public StringToLocalDateTimeConverter localDateTimeConverter() { | ||
return source -> { | ||
if (StrUtil.isBlank(source)) { | ||
return null; | ||
} | ||
return LocalDateTimeUtil.parse(source, UniversalDateTime.DATE_TIME_FORMAT); | ||
}; | ||
} | ||
|
||
interface StringToLocalDateConverter extends Converter<String, LocalDate> { | ||
} | ||
|
||
interface StringToLocalTimeConverter extends Converter<String, LocalTime> { | ||
} | ||
|
||
interface StringToLocalDateTimeConverter extends Converter<String, LocalDateTime> { | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...arter/src/main/java/com/jmsoftware/maf/springcloudstarter/constant/UniversalDateTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.jmsoftware.maf.springcloudstarter.constant; | ||
|
||
/** | ||
* Description: UniversalDateTime, change description here. | ||
* | ||
* @author Johnny Miller (锺俊), email: [email protected], date: 6/27/2021 9:42 PM | ||
**/ | ||
public interface UniversalDateTime { | ||
String DATE_FORMAT = "yyyy-MM-dd"; | ||
String TIME_FORMAT = "HH:mm:ss"; | ||
String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; | ||
} |
22 changes: 22 additions & 0 deletions
22
...tarter/src/main/java/com/jmsoftware/maf/springcloudstarter/controller/ControllerBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.jmsoftware.maf.springcloudstarter.controller; | ||
|
||
import org.springframework.beans.propertyeditors.CustomDateEditor; | ||
import org.springframework.web.bind.WebDataBinder; | ||
import org.springframework.web.bind.annotation.InitBinder; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
/** | ||
* Description: ControllerBase, change description here. | ||
* | ||
* @author Johnny Miller (锺俊), email: [email protected], date: 6/27/2021 9:20 PM | ||
**/ | ||
public class ControllerBase { | ||
// @InitBinder | ||
// public void initBinder(WebDataBinder binder) { | ||
// final SimpleDateFormat dateFormat = new SimpleDateFormat(DateTimeUniversalFormat.DATE_TIME_PATTERN); | ||
// dateFormat.setLenient(false); | ||
// binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); | ||
// } | ||
} |