com.quhaodian.web.conver.DateConver Maven / Gradle / Ivy
package com.quhaodian.web.conver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DateConver {
private Logger logger = LoggerFactory.getLogger(DateConver.class);
private List commands = new ArrayList<>();
public void addCommand(DateFormatCommand command) {
commands.add(command);
}
public Date conver(String timestr) {
Date result = null;
for (DateFormatCommand command : commands) {
result = command.conver(timestr);
if (result != null) {
logger.info("格式:"+command.toString());
return result;
}
}
return result;
}
public static DateConver getDefault() {
DateConver conver = new DateConver();
conver.addCommand(new DateFormatCommand("yyyyMMddhhmmss"));
conver.addCommand(new DateFormatCommand("yyyy-MM-dd hh:mm:ss"));
conver.addCommand(new DateFormatCommand("yyyy-MM-dd"));
conver.addCommand(new DateFormatCommand("yyyy/MM/dd"));
conver.addCommand(new DateFormatCommand("yyyy年MM月dd日"));
conver.addCommand(new DateFormatCommand("yyyy年MM月"));
conver.addCommand(new DateFormatCommand("yyyy-MM"));
conver.addCommand(new DateFormatCommand("yyyy年"));
conver.addCommand(new DateFormatCommand("yyyy"));
return conver;
}
public static void main(String[] args) {
DateConver conver = getDefault();
System.out.println(conver.conver("2018/11").toLocaleString());
}
}