Spring boot 使用Datetime格式化程序创建单个函数来处理日期和日期时间

Spring boot 使用Datetime格式化程序创建单个函数来处理日期和日期时间,spring-boot,java-8,Spring Boot,Java 8,我想编写一个函数,将字符串转换为“yyyy-MM-dd HH:MM:ss” 字符串可以是带有时间戳(例如:12-10-1994)的日期格式,也可以是带有时间戳“1990/10/10 10:12:11”的日期。如果我给出12-10-1994->它将返回“1994:10:12 00:00”。1990/10/10 10:12:11->1990-10-10 10:12:11 我知道可以使用“atStartofDay”将日期转换为日期时间。 但是我想创建一个函数来处理以上两种情况,所有可能的日期都没有时间

我想
编写一个函数
,将字符串转换为“yyyy-MM-dd HH:MM:ss”

字符串可以是带有时间戳(例如:12-10-1994)的
日期格式,也可以是带有时间戳“1990/10/10 10:12:11”的日期。如果我给出12-10-1994->它将返回“1994:10:12 00:00”。1990/10/10 10:12:11->1990-10-10 10:12:11

我知道可以使用“atStartofDay”将日期转换为日期时间。 但是我想创建一个函数来处理以上两种情况,所有可能的日期都没有时间戳和日期时间格式。有人能帮我吗

下面是代码

 public String standardDate(){
       String value="12/12/1994";
       DateTimeFormatter formats=DateTimeFormatter.ofPattern("" +"[uuuuMMdd]"
                       +"[uuuu-MM-dd]"
                       +"[MM/dd/uuuu]"
                       +"[uuuu/MM/dd HH:mm:ss.SSSSSS]"
                       +"[dd-MM-uuuu HH:mm:ss]"
                       ,Locale.US).withResolverStyle.STRICT)
                       DateTimeFormatter formats=DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"
                       ,Locale.US).withResolverStyle.STRICT)
       If(StringUtils.isNotBlank(value)){
           try{
               
               LocalDateTime localDate=LocalDateTime.parse(value,formats);
               value=localDate.format(outputFormat);
               
           }catch("DateTimeParseException", e){;
           log.error(e);
           return null;
           }
   }
   return value;
 }
import java.time.LocalDateTime;
导入java.time.format.DateTimeFormatter;
导入java.time.format.DateTimeParseException;
导入java.time.format.ResolverStyle;
导入java.util.HashMap;
导入java.util.Locale;
导入java.util.Map;
导入java.util.function.function;
公开课考试{
私有静态最终映射解析器=new HashMap();
静止的{
parsers.put(“#####-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-;
parsers.put(“##/##/#########”,s->format(s+“00:00:00”,“MM/dd/uuu HH:MM:ss”);
语法分析器.put(“##############################################;
parsers.put(“##-#-#-#-######:#:#-#-#-#-#-#-##-##-###-##-#-#-#-##-#-#-#-#格式(;
}
公共静态字符串standardDate(字符串值){
DateTimeFormatter outputFormat=模式的DateTimeFormatter.of(“uuu-MM-dd-HH:MM:ss”
,Locale.US)。withResolverStyle(ResolverStyle.STRICT);
字符串模板=value.replaceAll(“\\d”,“#”);
函数解析器=parsers.get(模板);
if(解析器==null){
//日志错误(“未知格式”);
返回null;
}
试试{
LocalDateTime localDate=parser.apply(值);
返回localDate.format(outputFormat);
}捕获(DateTimeParse异常){
//log.error(例如getMessage());
返回null;
}   
}
私有静态LocalDateTime格式(字符串值、字符串模式){
返回LocalDateTime.parse(值,
模式(pattern,Locale.US)的DateTimeFormatter.withResolverStyle(ResolverStyle.STRICT));
}
//试验
公共静态void main(字符串[]args){
字符串示例[]={“1994-12-31”、“12/31/1994”、“1994/12/31 10:20:30.123456”、“31-12-1994 10:20:30”、“坏”};
for(字符串示例){
系统输出打印F(“%-30s%s%n”,ex,standardDate(ex));
}
}
}

您的问题是什么?你发布了你的解决方案,什么有效?什么不是?我的代码是为带有时间戳的日期工作的。但不是为date@crizzis
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

public class Test {
    private static final Map<String, Function<String,LocalDateTime>> parsers = new HashMap<>();
    static {
        parsers.put("####-##-##", s->format(s+" 00:00:00", "uuuu-MM-dd HH:mm:ss"));
        parsers.put("##/##/####", s->format(s+" 00:00:00", "MM/dd/uuuu HH:mm:ss"));     
        parsers.put("####/##/## ##:##:##.######",   s->format(s, "uuuu/MM/dd HH:mm:ss.SSSSSS"));
        parsers.put("##-##-#### ##:##:##",  s->format(s, "dd-MM-uuuu HH:mm:ss"));
    }

    public static String standardDate(String value) {
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"
                , Locale.US).withResolverStyle(ResolverStyle.STRICT);
    
        String template = value.replaceAll("\\d", "#");
        Function<String,LocalDateTime> parser = parsers.get(template);      
        if (parser == null) {
            // log.error("Unknown format");
            return null;
        }
    
        try {       
            LocalDateTime localDate = parser.apply(value);
            return localDate.format(outputFormat);
        } catch (DateTimeParseException e) {            
            // log.error(e.getMessage());
            return null;
        }   
    }

    private static LocalDateTime format(String value, String pattern) {
        return LocalDateTime.parse(value,
                DateTimeFormatter.ofPattern(pattern,  Locale.US).withResolverStyle(ResolverStyle.STRICT));
    }

    // test
    public static void main(String[] args) {
        String examples[] = {"1994-12-31", "12/31/1994", "1994/12/31 10:20:30.123456", "31-12-1994 10:20:30", "bad"};
        for (String ex: examples) {
            System.out.printf("%-30s %s%n", ex, standardDate(ex));
        }
    }
}