public class DateUtil {
public static String dateToString(Date date) {
try {
return dateToString(date, "yyyyMMddHHmmss");
} catch (Exception ex) {
// ex.printStackTrace();
return null;
}
}
public static String dateToString(Date date, String format) {
try {
SimpleDateFormat fmt = new SimpleDateFormat(format);
return fmt.format(date);
} catch (Exception ex) {
// ex.printStackTrace();
return null;
}
}
public static Date stringToDate(String date) {
String format = null;
SimpleDateFormat formatter = null;
try {
if (isNumeric(date)) {
format = getNumericDateFormat(date);
} else {
format = getStringDateFormat(date);
}
//2006年12月30日 11位
//下午14时26分30秒000 14位
if (format == null || format.toLowerCase().indexOf("yyyy") < 0 || format.length() > 25) {
return null;
}
formatter = new SimpleDateFormat(format);
return formatter.parse(date);
} catch (Exception ex) {
return null;
}
}
public static String stringToDateString(String _date) {
String format = null;
SimpleDateFormat formatter = null;
try {
if (isNumeric(_date)) {
format = getNumericDateFormat(_date);
} else {
format = getStringDateFormat(_date);
}
if (format == null || format.toLowerCase().indexOf("yyyy") < 0) {
return null;
}
formatter = new SimpleDateFormat(format);
Date date = formatter.parse(_date);
if (date != null) {
return DateUtil.dateToString(date, format);
}
return null;
} catch (Exception ex) {
return null;
}
}
public static String getNumericDateFormat(String date) {
String format = null;
try {
switch (date.trim().length()) {
case 8:
format = "yyyyMMdd";
break;
case 10:
format = "yyyyMMddHH";
break;
case 12:
format = "yyyyMMddHHmm";
break;
case 14:
format = "yyyyMMddHHmmss";
break;
case 17:
format = "yyyyMMddHHmmssSSS";
break;
}
return format;
} catch (Exception ex) {
// ex.printStackTrace();
return null;
}
}
public static String getStringDateFormat(String date) {
try {
String parse = date.trim();
if (parse.indexOf("T") > -1) {
return "yyyy-MM-dd'T'HH:mm:ss";
}
parse = parse.replaceFirst("^[0-9]{4}([^0-9]?)", "yyyy$1");
parse = parse.replaceFirst("^[0-9]{2}([^0-9]?)", "yy$1");
parse = parse
.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1MM$2");
parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2");
parse = parse.replaceFirst("( |.)[0-9]{1,2}([^0-9]?)", "$1HH$2");
parse = parse
.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1mm$2");
parse = parse
.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1ss$2");
return parse;
} catch (Exception ex) {
return null;
}
}
public static boolean isNumeric(String str) {
try {
if (str == null || str.trim().equals("")) {
return false;
}
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
} catch (Exception ex) {
return false;
}
}
public static String getDateFormat(String strDate) {
try {
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format1.format(stringToDate(strDate));
} catch (Exception ex) {
// ex.printStackTrace();
return strDate;
}
}
public static boolean isDateTime(String dateTime) {
try {
Pattern pattern = Pattern
.compile("^((\\d{2}(([02468][048])|([13579][26]))"
+ "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))"
+ "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|"
+ "([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))"
+ "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|"
+ "(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
+ "((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:"
+ "([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
Matcher matcher = pattern.matcher(dateTime);
return matcher.matches();
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
public static String getThisTime() {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(d);
}
public static String getThisTimeSlot(String date_early) {
return getTimeSlot(getThisTime(), date_early);
}
public static String getTimeSlot(String date_late, String date_early) {
String timeSlot = "";
try {
long l = stringToDate(date_late).getTime()
- stringToDate(date_early).getTime();
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
if (day > 0) {
timeSlot += day + "天";
}
if (hour > 0) {
timeSlot += hour + "小时";
}
if (min > 0) {
timeSlot += min + "分";
}
// long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min *
// 60);
// if(s > 0) {
// timeSlot += s + "秒";
// }
return timeSlot + "前";
} catch (Exception ex) {
return null;
}
}
public static String getFormatTime(String date,int type) {
if(date!=null&&date.length()==14){
if(type==1){
String year=date.substring(0,4);
String month=date.substring(4,6);
String day=date.substring(6,8);
date=year+"年"+month+"月"+day+"日";
}else if(type==2){
String hh=date.substring(8,10);
String mm=date.substring(10,12);
String ss=date.substring(12,14);
date=hh+":"+mm+":"+ss;
}
}
return date;
}
/**
* 判断格式是否14位时间
* @param str
* @return
*/
public static boolean is14DateTime(String str) {
try {
if (str == null || str.trim().equals("")) {
return false;
}
Pattern pattern = Pattern.compile("(19|20)[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
} catch (Exception ex) {
return false;
}
}
public static void main(String[] arr) {
String columnValue = "20061230111111";
// 日期类型不打乱内容
if (isDateTime(columnValue)) {
System.out.println("是日期:" + columnValue);
} else {
System.out.println("不是日期:");
}
Date常用方法工具类
最新推荐文章于 2024-07-05 08:14:42 发布