• 注册
当前位置:1313e > 默认分类 >正文

日期格式化大全

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


一、JSP页面中格式化日期


JSTL

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

在JSP页面中格式化日期


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


二、使用JS格式化日期


1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 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格式化日期


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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);
    }
}





本文转自 genuinecx 51CTO博客,原文链接:http://blog.51cto.com/favccxx/1296441,如需转载请自行联系原作者

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录
相关推荐