package com.vci.starter.web.toolmodel;
|
|
import com.vci.starter.web.constant.RegExpConstant;
|
import com.vci.starter.web.exception.VciBaseException;
|
import com.vci.starter.web.util.VciDateUtil;
|
import org.apache.commons.lang3.StringUtils;
|
|
import java.beans.PropertyEditorSupport;
|
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;
|
|
/**
|
* 时间处理,前台json传输过来的内容的时候
|
* @author weidy
|
*/
|
public class DateConverter extends PropertyEditorSupport {
|
|
/**
|
*时间格式
|
*/
|
private DateFormat dateFormat;
|
|
/**
|
*时间内容的长度
|
*/
|
private int exactDateLength;
|
|
/**
|
* 具体的值
|
*/
|
private Date value;
|
|
public DateFormat getDateFormat() {
|
return dateFormat;
|
}
|
|
public void setDateFormat(DateFormat dateFormat) {
|
this.dateFormat = dateFormat;
|
}
|
|
public int getExactDateLength() {
|
return exactDateLength;
|
}
|
|
public void setExactDateLength(int exactDateLength) {
|
this.exactDateLength = exactDateLength;
|
}
|
|
public Date getValue() {
|
return value;
|
}
|
|
public void setValue(Date value) {
|
this.value = value;
|
}
|
|
/**
|
*将字符串转换为日期格式
|
* @param text 字符串
|
* @throws VciBaseException 转换出错的时候抛出异常
|
*/
|
public void setAsText(String text) throws VciBaseException {
|
if (StringUtils.isBlank(text)) {
|
setValue(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()){
|
//说明是符合要求的
|
String dateSplit = m.group(1);
|
//中间的空格是第二部分
|
String timeSplit = m.group(3);
|
String formateStr = String.format("yyyy%sMM%sdd",dateSplit,dateSplit);
|
if(StringUtils.isNotBlank(timeSplit)){
|
//有时间
|
String timeStr = text.substring(text.indexOf(" "));
|
String[] split = timeStr.split(timeSplit);
|
if(split.length == 2){
|
formateStr += String.format(" HH%smm",timeSplit);
|
}
|
if(split.length>2){
|
formateStr += String.format(" HH%smm%sss",timeSplit,timeSplit);
|
}
|
if(timeStr.contains(".")){
|
//毫秒
|
formateStr += ".SSS";
|
}
|
}
|
String yearMD = text.contains(" ")?text.substring(0, text.indexOf(" ")):text;
|
if(StringUtils.isNotBlank(dateSplit)){
|
//说明有日期的分割符
|
String year = text.substring(0, text.indexOf(dateSplit));
|
if(StringUtils.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(RegExpConstant.NUMBER)){
|
//说明还是有符号,但是不符合要求而已
|
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(RegExpConstant.NUMBER)) {
|
formateStr = "yy";
|
} else if (text.length() == 4 && text.matches(RegExpConstant.NUMBER)) {
|
formateStr = "yyyy";
|
} else if (text.length() == 6 && text.matches(RegExpConstant.NUMBER)) {
|
formateStr = "yyyyMM";
|
} else {
|
if (StringUtils.isNotBlank(yearMD) && yearMD.length() < 6) {
|
formateStr = formateStr.replace("yyyy", "yy");
|
}
|
}
|
}
|
}
|
//只有时间,我们是没办法转换的
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formateStr);
|
try {
|
setValue(simpleDateFormat.parse(text));
|
} catch (ParseException e) {
|
//是看看短年份
|
formateStr= formateStr.replace("yyyy","yy");
|
simpleDateFormat = new SimpleDateFormat(formateStr);
|
try {
|
setValue(simpleDateFormat.parse(text));
|
} catch (ParseException ex) {
|
//如果只有日期
|
if(text.length() == 2 && text.matches(RegExpConstant.NUMBER)){
|
formateStr = "yy";
|
}
|
throw new VciBaseException("不能格式化日期: {0}", new String[]{text}, e);
|
}
|
}
|
}else{
|
throw new VciBaseException("不是合格的时间格式字符串,{0}", new String[]{text});
|
}
|
// if(text.contains("年") || text.contains("月") || text.contains("日")
|
// ||text.contains("时") || text.contains("分") || text.contains("秒") || text.contains("毫秒")
|
// ||text.contains("周") || text.contains("礼拜") || text.contains("天")){
|
// //1. 只有年
|
// if(text.indexOf("年") == text.length()-1){
|
// this.exactDateLength = text.length();
|
// if(text.length() ==3) {
|
// this.dateFormat = new SimpleDateFormat("yy年");
|
// }
|
// }
|
// }
|
// if(text.contains("/")) {
|
// if ( text.trim().length() == 19) {
|
// this.exactDateLength = 19;
|
// this.dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
// } else if (text.indexOf(".") > -1 && text.length() > 20) {
|
// this.exactDateLength = 23;
|
// text = fillNano(text);
|
// this.dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
|
// } else if (text.length() == 17) {
|
// this.exactDateLength = 17;
|
// this.dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
|
// } else if ( text.indexOf(".") > -1 && text.length() > 17) {
|
// this.exactDateLength = 21;
|
// text = fillNano(text);
|
// this.dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss.SSS");
|
// } else if (text.length() == 10) {
|
// this.exactDateLength = 10;
|
// this.dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
// } else if (text.length() >=8 && text.length() < 10 ) {
|
// //2018/1/1 或者18/01/01,18/1/1
|
// if(text.substring(0,text.indexOf("/")).length() == 4){
|
// this.dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
// }else{
|
// this.dateFormat = new SimpleDateFormat("yy/MM/dd");
|
// }
|
// this.exactDateLength = text.length();
|
// }else{
|
// this.dateFormat = new SimpleDateFormat("yy/MM/dd");
|
// this.exactDateLength = text.length();
|
// }
|
// }else if(text.contains("-")) {
|
// if (text.length() == 19) {
|
// this.exactDateLength = 19;
|
// this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// } else if ( text.indexOf(".") > -1 && text.length() > 20) {
|
// this.exactDateLength = 23;
|
// text = fillNano(text);
|
// this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
// } else if (text.length() == 17) {
|
// this.exactDateLength = 17;
|
// this.dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
|
// } else if ( text.length() == 8) {
|
// this.exactDateLength = 8;
|
// this.dateFormat = new SimpleDateFormat("yy-MM-dd");
|
// } else if (text.length() == 10) {
|
// this.exactDateLength = 10;
|
// this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
// }else if (text.length() >=8 && text.length() < 10 ) {
|
// //2018-1-1 或者18-01-01,18/1/1
|
// if(text.substring(0,text.indexOf("-")).length() == 4){
|
// this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
// }else{
|
// this.dateFormat = new SimpleDateFormat("yy-MM-dd");
|
// }
|
// this.exactDateLength = text.length();
|
// }else{
|
// this.dateFormat = new SimpleDateFormat("yy-MM-dd");
|
// this.exactDateLength = text.length();
|
// }//先弄这么多,不够再添加
|
// }else if(text.matches(RegExpConstant.NUMBER)){
|
//
|
// }else{
|
// this.exactDateLength = 19;
|
// this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// }
|
// //if ((text != null) && (this.exactDateLength >= 0)&& (text.length() != this.exactDateLength)) {
|
// // throw new IllegalArgumentException("不能初始化时间,因为内容不到" + this.exactDateLength + "长度");
|
// // }
|
// try {
|
// setValue(VciDateUtil.str2Date(VciDateUtil.date2Str(this.dateFormat.parse(text),VciDateUtil.DateTimeMillFormat),VciDateUtil.DateTimeMillFormat));
|
// //无论界面是什么格式,我们都转化为"yyyy-MM-dd HH:mm:ss.SSS"这样可以统一后台的内容
|
// } catch (ParseException ex) {
|
// throw new VciBaseException("不能格式化日期: "+ ex.getMessage(), new Object[0],ex);
|
// } catch (Exception e) {
|
// throw new VciBaseException("不能格式化日期: "+ e.getMessage(), new Object[0], e);
|
// }
|
}
|
}
|
|
/**
|
* 抛弃毫秒
|
* @param text 字符串
|
* @return 没有毫秒的时间值
|
*/
|
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 += "0";
|
}
|
}
|
return text.substring(0,text.lastIndexOf(".")) + "." + nano;
|
}
|
|
public static void main(String[] args) {
|
List<String> list = new ArrayList<String>(){{
|
add("2020-11-12 12:12:03");
|
add("2020-1-2 2:2:3");
|
add("2020-11-12 12:12:03.232");
|
add("2020/11/12 12:12:03");
|
add("2020/1/2 2:2:3");
|
add("2020/11/12 12:12:03.232");
|
add("20/11/12 12:12:03");
|
add("20/1/2 2:2:3");
|
add("20/11/12 12:12:03.232");
|
add("2020");
|
add("2020-11");
|
add("202011");
|
add("12:02:12");
|
add("2:2:3");
|
add("20201112");
|
add("202012");
|
add("202012");
|
add("2020年11月12日 12时12分03秒");
|
add("2020年1月2日 2:2:3");
|
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(), VciDateUtil.DateTimeMillFormat));
|
}catch (Throwable e){
|
e.printStackTrace();
|
}
|
});
|
|
}
|
|
/**
|
* 获取日期转换为字符串的值
|
* @return 字符串
|
*/
|
public String getAsText(String dateFormat) {
|
Date value = (Date) getValue();
|
if(StringUtils.isEmpty(dateFormat)){
|
dateFormat= VciDateUtil.DateTimeMillFormat;
|
}
|
return value != null ? new SimpleDateFormat(dateFormat).format(value) : "";
|
}
|
}
|