package com.vci.ubcs.starter.web.toolmodel;
|
|
import com.vci.ubcs.starter.exception.VciBaseException;
|
import com.vci.ubcs.starter.web.util.VciDateUtil;
|
import org.springblade.core.tool.utils.StringUtil;
|
|
import java.text.DateFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
public class DateConverter {
|
private DateFormat dateFormat;
|
private int exactDateLength;
|
private Date value;
|
|
public DateConverter() {
|
}
|
|
public DateFormat getDateFormat() {
|
return this.dateFormat;
|
}
|
|
public void setDateFormat(DateFormat dateFormat) {
|
this.dateFormat = dateFormat;
|
}
|
|
public int getExactDateLength() {
|
return this.exactDateLength;
|
}
|
|
public void setExactDateLength(int exactDateLength) {
|
this.exactDateLength = exactDateLength;
|
}
|
|
public Date getValue() {
|
return this.value;
|
}
|
|
public void setValue(Date value) {
|
this.value = value;
|
}
|
|
public void setAsText(String text) throws VciBaseException {
|
if (StringUtil.isBlank(text)) {
|
this.setValue((Date)null);
|
} else {
|
text = text.trim();
|
text = text.replace("年", "-").replace("月", "-").replace("日", "").replace("时", ":").replace("分", ":");
|
String pattern = "\\d{2,4}([^\\d]?)\\d{1,2}\\1\\d{1,2}( \\d{1,2}([^\\d])\\d{1,2})?";
|
Pattern r = Pattern.compile(pattern);
|
Matcher m = r.matcher(text);
|
if (!m.find()) {
|
throw new VciBaseException("不是合格的时间格式字符串,{0}", new String[]{text});
|
}
|
|
String dateSplit = m.group(1);
|
String timeSplit = m.group(3);
|
String formateStr = String.format("yyyy%sMM%sdd", dateSplit, dateSplit);
|
String yearMD;
|
if (StringUtil.isNotBlank(timeSplit)) {
|
yearMD = text.substring(text.indexOf(" "));
|
String[] split = yearMD.split(timeSplit);
|
if (split.length == 2) {
|
formateStr = formateStr + String.format(" HH%smm", timeSplit);
|
}
|
|
if (split.length > 2) {
|
formateStr = formateStr + String.format(" HH%smm%sss", timeSplit, timeSplit);
|
}
|
|
if (yearMD.contains(".")) {
|
formateStr = formateStr + ".SSS";
|
}
|
}
|
|
yearMD = text.contains(" ") ? text.substring(0, text.indexOf(" ")) : text;
|
if (StringUtil.isNotBlank(dateSplit)) {
|
String year = text.substring(0, text.indexOf(dateSplit));
|
if (StringUtil.isNotBlank(year) && year.length() == 2) {
|
formateStr = formateStr.replace("yyyy", "yy");
|
}
|
|
String[] split = yearMD.split(dateSplit);
|
if (split.length == 2) {
|
formateStr = formateStr.replace("dd", "");
|
}
|
} else if (!text.matches("^[0-9]*$")) {
|
if (text.length() == 5) {
|
formateStr = formateStr.replace("yyyyMMdd", "yy" + text.substring(2, 3) + "MM");
|
}
|
|
if (text.length() == 7) {
|
formateStr = formateStr.replace("yyyyMMdd", "yyyy" + text.substring(4, 5) + "MM");
|
}
|
} else if (text.length() == 2 && text.matches("^[0-9]*$")) {
|
formateStr = "yy";
|
} else if (text.length() == 4 && text.matches("^[0-9]*$")) {
|
formateStr = "yyyy";
|
} else if (text.length() == 6 && text.matches("^[0-9]*$")) {
|
formateStr = "yyyyMM";
|
} else if (StringUtil.isNotBlank(yearMD) && yearMD.length() < 6) {
|
formateStr = formateStr.replace("yyyy", "yy");
|
}
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formateStr);
|
|
try {
|
this.setValue(simpleDateFormat.parse(text));
|
} catch (ParseException var13) {
|
formateStr = formateStr.replace("yyyy", "yy");
|
simpleDateFormat = new SimpleDateFormat(formateStr);
|
|
try {
|
this.setValue(simpleDateFormat.parse(text));
|
} catch (ParseException var12) {
|
if (text.length() == 2 && text.matches("^[0-9]*$")) {
|
formateStr = "yy";
|
}
|
|
throw new VciBaseException("不能格式化日期: {0}", new String[]{text}, var13);
|
}
|
}
|
}
|
|
}
|
|
private String fillNano(String text) {
|
String nano = text.substring(text.lastIndexOf(".") + 1);
|
if (nano.length() < 3) {
|
for(int i = 0; i < 3 - nano.length(); ++i) {
|
nano = nano + "0";
|
}
|
}
|
|
return text.substring(0, text.lastIndexOf(".")) + "." + nano;
|
}
|
|
/*public static void main(String[] args) {
|
List<String> list = new ArrayList<String>() {
|
{
|
this.add("2020-11-12 12:12:03");
|
this.add("2020-1-2 2:2:3");
|
this.add("2020-11-12 12:12:03.232");
|
this.add("2020/11/12 12:12:03");
|
this.add("2020/1/2 2:2:3");
|
this.add("2020/11/12 12:12:03.232");
|
this.add("20/11/12 12:12:03");
|
this.add("20/1/2 2:2:3");
|
this.add("20/11/12 12:12:03.232");
|
this.add("2020");
|
this.add("2020-11");
|
this.add("202011");
|
this.add("12:02:12");
|
this.add("2:2:3");
|
this.add("20201112");
|
this.add("202012");
|
this.add("202012");
|
this.add("2020年11月12日 12时12分03秒");
|
this.add("2020年1月2日 2:2:3");
|
this.add("2020年11月12日 12时12分03秒232毫秒");
|
}
|
};
|
list.stream().forEach((t) -> {
|
try {
|
DateConverter dateConverter = new DateConverter();
|
dateConverter.setAsText(t);
|
System.out.println("转换前:" + t + "; 转换后:" + VciDateUtil.date2Str(dateConverter.getValue(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
} catch (Throwable var2) {
|
var2.printStackTrace();
|
}
|
|
});
|
}*/
|
|
public String getAsText(String dateFormat) {
|
Date value = this.getValue();
|
if (StringUtil.isEmpty(dateFormat)) {
|
dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
|
}
|
|
return value != null ? (new SimpleDateFormat(dateFormat)).format(value) : "";
|
}
|
|
}
|