A unique experience of stepping on a pit
When there is a date of string type in the current page request, the Java backend needs to convert the string type to the date type. In springboot, the convertor interface is implemented through the custom dateconverterconfig class, which is put into the spring container for conversion.
! Here’s the key point:
when I knock step by step, there is still an exception * * here was an unexpected error (type = bad request, status = 400). * * *. Finally, I found that the converter’s package was wrong
remember: import org.springframework.core.convert.converter.converter;
package com.jiansheng.util;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @Author: Jiansheng
* @DateTime: 2021/6/27 10:35
* @Description:Date
*/
@Component
public class DateConverterConfig implements Converter<String, Date> {
private static final List<String> formarts = new ArrayList<>(4);
static{
formarts.add("yyyy-MM");
formarts.add("yyyy-MM-dd");
formarts.add("yyyy-MM-dd HH:mm");
formarts.add("yyyy-MM-dd HH:mm:ss");
}
@Override
public Date convert(String source) {
String value = source.trim();
if ("".equals(value)) {
return null;
}
if(source.matches("^\\d{4}-\\d{1,2}$")){
return parseDate(source, formarts.get(0));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
return parseDate(source, formarts.get(1));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(2));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(3));
}else {
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
}
}
/**
* Formatting the date
* @param dateStr String Character type date
* @param format String Format
* @return Date Date
*/
public Date parseDate(String dateStr, String format) {
Date date=null;
try {
DateFormat dateFormat = new SimpleDateFormat(format);
date = dateFormat.parse(dateStr);
} catch (Exception e) {
}
return date;
}
}
Jiansheng
Read More:
- [Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`
- [How to Solve Error]java.util.Date cannot be cast to java.sql.Date
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- Explicit and implicit conversion of Java data type
- [Solved] Openfeign Error: error: Failed to parse Date value…
- [Solved] Mapreducer Class Conversion error: java.lang.ClassCastException
- JAVA: How to Read JSON Format Data (Web Game Development)
- Conversion of two data types in Java
- Split log by date in log4j2 of spring boot
- C++ String case conversion and transform, tower, upper, usage
- Java back end receives localdatetime type parameter
- Springboot controls the startup of rabbitmq through configuration files
- Problems and causes of Java’s main function format (public static void main (string args()))
- Initializingbean Interface & Applicationcontextaware Interface in Springboot
- How to Solve Springboot Upload Files Error: The field XXX exceeds its maximum permitted size of 1048576 bytes
- [Solved] SpringBoot Error: org.springframework.http.converter.HttpMessageNotReadableException
- [Solved] Springboot2.x ElasticSearch Error: availableProcessors is already set to [4], rejecting [4]
- How to Solve Springboot YML configurate logging.level error
- Springboot error about swagger startup [How to Solve]
- [Solved] Springboot uses redis to add LocaldateTime Java 8 error