本文共 3126 字,大约阅读时间需要 10 分钟。
谈起日期格式化,那几乎是所有程序员的痛。尽管格式化日期并不难,但是要记住在不同语言下所有的日起格式化方法,那绝对是一大难事。但是今天,小编为大家整理好了,在各种不同语言下格式化日期的方法,从此再也不用为格式化日期而发愁。
JSTL
<%@ taglib prefix="fmt"uri="http://java.sun.com/jsp/jstl/fmt" %>
在JSP页面中格式化日期
Date.prototype.Format = function (fmt) { 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(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt;}//茫var time1 = new Date().Format("yyyy-MM-dd");var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import org.apache.commons.lang.StringUtils;public class DateUtil { private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); /** * 玫某莸牡yyyy-MM-dd眨玫前莸牡 * @param dealDate * @return */ public static String getMonthFirstDay(Date dealDate) { Calendar calendar = Calendar.getInstance(); if(dealDate != null){ calendar.setTime(dealDate); } calendar.set( Calendar.DATE, 1 ); return df.format(calendar.getTime()); } /** * 玫某莸一yyyy-MM-dd眨玫前莸一 * @param dealDate * @return */ public static String getMonthLastDay(Date dealDate) { Calendar calendar = Calendar.getInstance(); if(dealDate != null){ calendar.setTime(dealDate); } calendar.set(Calendar.DAY_OF_MONTH, calendar .getActualMaximum(Calendar.DAY_OF_MONTH)); return df.format(calendar.getTime()); } /** * 玫某潞 * @param lastDealDate 未 * @param intervalMonths 路 * @return */ public static String getNextDealDate(Date lastDealDate, int intervalMonths){ Calendar cal= Calendar.getInstance(); cal.setTime(lastDealDate); cal.add(cal.MONTH, intervalMonths); return df.format(cal.getTime()); } /** * Stringate * @param strDate * @return * @throws Exception */ public static Date toDate(String strDate) throws Exception{ if(StringUtils.isEmpty(strDate)){ throw new Exception("!"); } return df.parse(strDate); } /** * Date转为String * @param date * @return * @throws Exception */ public static String dateToString(Date date)throws Exception{ return df.format(date); }}
转载地址:http://xzrxl.baihongyu.com/