日期格式化大全

本文详细介绍了在多种编程语言中如何进行日期格式化的操作。包括JSP页面中的JSTL标签使用、JavaScript自定义方法及Java实用类实现等。无论您使用哪种语言,都能找到适合的日期格式化解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  谈起日期格式化,那几乎是所有程序员的痛。尽管格式化日期并不难,但是要记住在不同语言下所有的日起格式化方法,那绝对是一大难事。但是今天,小编为大家整理好了,在各种不同语言下格式化日期的方法,从此再也不用为格式化日期而发愁。


一、JSP页面中格式化日期


JSTL

<%@ taglib prefix="fmt"uri="http://java.sun.com/jsp/jstl/fmt" %>

在JSP页面中格式化日期


<fmt:formatDate value="${result.showDate}"type="both" pattern=yyyy-MM-dd/>


二、使用JS格式化日期


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");


三、使用Java格式化日期


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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值