Skip to content

Instantly share code, notes, and snippets.

@chris-zh
Created June 13, 2016 01:22
Show Gist options
  • Save chris-zh/980bc8d648931e03b9b332cc17ffe3cc to your computer and use it in GitHub Desktop.
Save chris-zh/980bc8d648931e03b9b332cc17ffe3cc to your computer and use it in GitHub Desktop.
处理日期格式
/**
* 原始日期类型
* YYYY-MM-DD HH24:MI:ss
* YYYY-MM-DD HH24:MI:ss.0
* YYYY-MM-DD
* YYYYMMDD
* 转换
* 成YYYY-MM-DDTHH24:mi:ss
* '2016-01-01T12:00:00
*
* @param inputDate
* @return
* @throws Exception
*/
public static String parseDate(String date) throws ParseException {
if(StringUtils.isEmpty(date)){
return "";
}
int len = date.length();
if(len==10){
return date + "T00:00:00";
}else if(len==19){
return date.replace(" ", "T");
}else if(len>19){
return date.substring(0, 19).replace(" ", "T");
}else if(len==8){
StringBuilder sb = new StringBuilder();
sb.append(date.substring(0, 4))
.append("-")
.append(date.substring(4, 6))
.append("-").append(date.substring(6, 8))
.append("T00:00:00");
return sb.toString();
}
else{
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment