基本能够满足常见格式的日期到字符串,字符串到日期之间的转化。
package com.hongyuan.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { //按照给定的格式化字符串格式化日期 public static String formatDate(Date date, String formatStr) { SimpleDateFormat sdf = new SimpleDateFormat(formatStr); return sdf.format(date); } //按照给定的格式化字符串解析日期 public static Date parseDate(String dateStr, String formatStr) throws ParseException { Date date = null; SimpleDateFormat sdf = new SimpleDateFormat(formatStr); date = sdf.parse(dateStr); return date; } //从字符串中分析日期 public static Date parseDate(String dateStr) throws ParseException { Date date=null; String[] dateArray = dateStr.split("\\D+"); //+防止多个非数字字符在一起时导致解析错误 int dateLen = dateArray.length; int dateStrLen=dateStr.length(); if(dateLen>0){ if(dateLen==1&&dateStrLen>4){ if(dateStrLen=="yyyyMMddHHmmss".length()){ //如果字符串长度为14位并且不包含其他非数字字符,则按照(yyyyMMddHHmmss)格式解析 date=parseDate(dateStr,"yyyyMMddHHmmss"); }else if(dateStrLen=="yyyyMMddHHmm".length()){ date=parseDate(dateStr,"yyyyMMddHHmm"); }else if(dateStrLen=="yyyyMMddHH".length()){ date=parseDate(dateStr,"yyyyMMddHH"); }else if(dateStrLen=="yyyyMMdd".length()){ date=parseDate(dateStr,"yyyyMMdd"); }else if(dateStrLen=="yyyyMM".length()){ date=parseDate(dateStr,"yyyyMM"); } }else{ String fDateStr=dateArray[0]; for(int i=1;i<dateLen;i++){ //左补齐是防止十位数省略的情况 fDateStr+=leftPad(dateArray[i],"0",2); } if(dateStr.trim().matches("^\\d{1,2}:\\d{1,2}(:\\d{1,2})?$")){ //补充年月日3个字段 dateLen+=3; fDateStr=formatDate(new Date(),"yyyyMMdd")+fDateStr; } date=parseDate(fDateStr,"yyyyMMddHHmmss".substring(0, (dateLen-1)*2+4)); } } return date; } //左补齐 public static String leftPad(String str,String pad,int len){ String newStr=(str==null?"":str); while(newStr.length()<len){ newStr=pad+newStr; } if(newStr.length()>len){ newStr=newStr.substring(newStr.length()-len); } return newStr; } public static void main(String[] args) throws ParseException { String[] dateStrArray=new String[]{ "2014-03-12 12:05:34", "2014-03-12 12:05", "2014-03-12 12", "2014-03-12", "2014-03", "2014", "20140312120534", "2014/03/12 12:05:34", "2014/3/12 12:5:34", "2014年3月12日 12时5分34秒", "201403121205", "2014031212", "20140312", "201403", "12:05:34", "12:05", }; for(int i=0;i<dateStrArray.length;i++){ Date date=parseDate(dateStrArray[i]); System.out.println(dateStrArray[i]+" ==> "+formatDate(date,"yyyy-MM-dd HH:mm:ss")); } } }