SpringBoot—使用datetime格式化程序创建一个函数来处理日期和日期时间

webghufk  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(362)

我想 write a function 那个 converts the string into "yyyy-MM-dd HH:mm:ss" .
字符串可以是 date format with out time stamp(eg:12-10-1994) or date with time stamp "1990/10/10 10:12:11".If I give 12-10-1994 -> It returns "1994:10:12 00: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;
 }
yruzcnhs

yruzcnhs1#

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));
        }
    }
}

相关问题