博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
日期格式化大全
阅读量:7028 次
发布时间:2019-06-28

本文共 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/

你可能感兴趣的文章
hidesBottomBarWhenPushed 设置为NO的问题
查看>>
cisco常用命令详解
查看>>
谁在追踪谁?
查看>>
HTTP请求返回状态码详解
查看>>
句柄类
查看>>
GitLab
查看>>
【常用配置】Spring框架web.xml通用配置
查看>>
[leetcode 240]Search a 2D Matrix II
查看>>
域名指的是这一级目录
查看>>
[Angular] Creating an Observable Store with Rx
查看>>
[转]Porting to Oracle with Entity Framework NLog
查看>>
chmod更改文件的权限
查看>>
oracle 10g/11g RAC 启停归档模式
查看>>
poj3461 Oulipo
查看>>
OAuth2.0学习(1-12)开源的OAuth2.0项目和比较
查看>>
Gitlab,这也就O了???
查看>>
2014 百度之星 1003 题解 Xor Sum
查看>>
Linux中在主机上实现对备机上文件夹及文件的操作的C代码实现
查看>>
iOS 块的简单理解
查看>>
idea中如何配置git以及在idea中初始化git
查看>>