UI-date-format

一,纯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')

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值