zoukankan      html  css  js  c++  java
  • SimpleDateFormat日期格式(浅面)

    java中使用SimpleDateFormat类的构造函数SimpleDateFormat(String str)构造格式化日期的格式,

    通过format(Date date)方法将指定的日期对象格式化为指定格式的字符串.

    SimpleDateFormat构造函数中字符串的格式,以及各部分代表的含义:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class test{
    
        public static void main(String args[]) {
            Date newTime = new Date();
            //设置时间格式
            SimpleDateFormat sdf1 = new SimpleDateFormat("y-M-d h:m:s a E");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yy-MM-dd hh:mm:ss a E");
            SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MMM-ddd hhh:mmm:sss a E");
            SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyy-MMMM-dddd hhhh:mmmm:ssss a E");
            
            //获取的时间,是本机的时间
            String formatDate1 = sdf1.format(newTime);
            String formatDate2 = sdf2.format(newTime);
            String formatDate3 = sdf3.format(newTime);
            String formatDate4 = sdf4.format(newTime);
    
            System.out.println(formatDate1);  
            System.out.println(formatDate2); 
            System.out.println(formatDate3); 
            System.out.println(formatDate4); 
        }
    }

    运行结果:

    字符串"yyyy-MM-dd hh:mm:ss",其中:

    yyyy : 代表年(不去区分大小写) 假设年份为 2017

        "y" , "yyy" , "yyyy" 匹配的都是4位完整的年 如 : "2017"

        "yy" 匹配的是年分的后两位 如 : "15"

        超过4位,会在年份前面加"0"补位 如 "YYYYY"对应"02017"

    MM : 代表月(只能使用大写) 假设月份为 9

        "M" 对应 "9"

        "MM" 对应 "09"

        "MMM" 对应 "Sep"

        "MMMM" 对应 "Sep"

        超出3位,仍然对应 "September"

        

    dd : 代表日(只能使用小写) 假设为13号

        "d" , "dd" 都对应 "13"

        超出2位,会在数字前面加"0"补位. 例如 "dddd" 对应 "0013"

    hh : 代表时(区分大小写,大写为24进制计时,小写为12进制计时) 假设为15时

        "H" , "HH" 都对应 "15" , 超出2位,会在数字前面加"0"补位. 例如 "HHHH" 对应 "0015"

        "h" 对应 "3"

        "hh" 对应 "03" , 超出2位,会在数字前面加"0"补位. 例如 "hhhh" 对应 "0003"

    mm : 代表分(只能使用小写) 假设为32分

        "m" , "mm" 都对应 "32" ,  超出2位,会在数字前面加"0"补位. 例如 "mmmm" 对应 "0032"

    ss : 代表秒(只能使用小写) 假设为15秒

        "s" , "ss" 都对应 "15" , 超出2位,会在数字前面加"0"补位. 例如 "ssss" 对应 "0015"

    E : 代表星期(只能使用大写) 假设为 Sunday

        "E" , "EE" , "EEE" 都对应 "Sun"

        "EEEE" 对应 "Sunday" , 超出4位 , 仍然对应 "Sunday"

    a : 代表上午还是下午,如果是上午就对应 "AM" , 如果是下午就对应 "PM"

    其中的分隔符"-"可以替换成其他非字母的任意字符(也可以是汉字),例如:

    部分修改:

    运行结果:

    ------------------------------------------------
  • 相关阅读:
    『题解』POJ1753 Flip Game
    『数论』乘法逆元
    『数据结构』RMQ问题
    UVA 253 Cube painting 【数学】
    POJ 1852 Ants 【水+Trick+贪心】
    POJ 2955 Brackets 【区间DP】
    LOJ 1422 Halloween Costumes【区间DP】
    HDU 4193 Non-negative Partial Sums 【单调队列】
    CodeForces-545C Woodcutters 【贪心+机智】
    CodeForces 19D A and B and Interesting Substrings 【前缀和】
  • 原文地址:https://www.cnblogs.com/jyiqing/p/6858224.html
Copyright © 2011-2022 走看看