-
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
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: johnnysviva@outlook.com, 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: johnnysviva@outlook.com, 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: johnnysviva@outlook.com, 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)); | ||
// } | ||
} |