一,纯js解决方式
1,定义脚本
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
: ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
2,调用
new Date().format("yyyy-MM-dd hh:mm:ss")
二,vm tools方式解决
1,定义tools文件
public class DateFormatUtils {
public static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
/**
* 日期转字符串
* @param date
* @param format
* @return
*/
public static String format (Date date, String format) {
if(date == null){
return null;
}
String resultStr = "";
String formatTemp = format;
if(formatTemp == null || "".equals(formatTemp)){
formatTemp = DEFAULT_DATE_FORMAT;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(formatTemp);
resultStr = dateFormat.format(date).trim();
return resultStr;
}
/**
* 字符串转日期
* @param str
* @param format
* @return
* @throws java.text.ParseException
*/
public static Date parse (String str, String format) throws ParseException {
String formatTemp = format;
if(formatTemp == null || "".equals(formatTemp)){
formatTemp = DEFAULT_DATE_FORMAT;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(formatTemp);
return dateFormat.parse(str);
}
/**
* 字符串转日期,自动适配字符串格式
* @param str
* @return
* @throws java.text.ParseException
*/
public static Date parse (String str) throws ParseException {
String[] parsePatterns = {DEFAULT_TIME_FORMAT, DEFAULT_DATE_FORMAT, "yyyyMMdd", "yyyyMMddHHmmss",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss"};
return parseDateWithLeniency(str, parsePatterns, false);
}
private static Date parseDateWithLeniency(
String str, String[] parsePatterns, boolean lenient) throws ParseException {
if (str == null || parsePatterns == null) {
throw new IllegalArgumentException("Date and Patterns must not be null");
}
SimpleDateFormat parser = new SimpleDateFormat();
parser.setLenient(lenient);
ParsePosition pos = new ParsePosition(0);
for (String parsePattern : parsePatterns) {
String pattern = parsePattern;
// LANG-530 - need to make sure 'ZZ' output doesn't get passed to SimpleDateFormat
if (parsePattern.endsWith("ZZ")) {
pattern = pattern.substring(0, pattern.length() - 1);
}
parser.applyPattern(pattern);
pos.setIndex(0);
String str2 = str;
// LANG-530 - need to make sure 'ZZ' output doesn't hit SimpleDateFormat as it will ParseException
if (parsePattern.endsWith("ZZ")) {
str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2");
}
Date date = parser.parse(str2, pos);
if (date != null && pos.getIndex() == str2.length()) {
return date;
}
}
throw new ParseException("Unable to parse the date: " + str, -1);
}
}
2,xml引入
<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<tool>
<key>dateFormatUtils</key>
<scope>application</scope>
<class>com.my.commons.utils.DateFormatUtils</class>
</tool>
</toolbox>
3,调用
$!dateFormatUtils.format(date,'yyyy-MM-dd HH:mm:ss')