【java 基础 之 日期格式化大总结SimpleDateFormat,Calendar】

争取总结的全面一些,以供参考使用。

Date类

long currentTimeMillis = System.currentTimeMillis();//获取当前时间戳
        //Date date = new Date(currentTimeMillis);

        //和上面的两行代码效果一样,都是获取当前时间
        Date date = new Date();

        System.out.println(date.getTime());//得到当前时间的时间戳
        System.out.println(date.toGMTString());
        System.out.println(date.toLocaleString());
        System.out.println(date.toString());

打印结果:
1504862638000
8 Sep 2017 09:09:25 GMT
2017-9-8 17:09:25
Fri Sep 08 17:09:25 CST 2017

SimpleDateFormat类

先贴出一个全面的参考文档:

/**
     * SimpleDateFormat函数语法:
     * G 年代标志符
     * y 年
     * M 月
     * d 日
     * h 时 在上午或下午 (1~12)
     * H 时 在一天中 (0~23)
     * m 分
     * s 秒
     * S 毫秒
     * E 星期
     * D 一年中的第几天
     * F 一月中第几个星期几
     * w 一年中第几个星期
     * W 一月中第几个星期
     * a 上午 / 下午 标记符
     * k 时 在一天中 (1~24)
     * K 时 在上午或下午 (0~11)
     * z 时区
     */
  d               月中的某一天。一位数的日期没有前导零。  
  dd             月中的某一天。一位数的日期有一个前导零。  
  ddd           周中某天的缩写名称,在   AbbreviatedDayNames   中定义。  
  dddd         周中某天的完整名称,在   DayNames   中定义。  
  M               月份数字。一位数的月份没有前导零。  
  MM             月份数字。一位数的月份有一个前导零。  
  MMM           月份的缩写名称,在   AbbreviatedMonthNames   中定义。  
  MMMM         月份的完整名称,在   MonthNames   中定义。  
  y               不包含纪元的年份。不具有前导零。  
  yy             不包含纪元的年份。具有前导零。  
  yyyy         包括纪元的四位数的年份。  
  gg             时期或纪元。  
  h               12   小时制的小时。一位数的小时数没有前导零。  
  hh             12   小时制的小时。一位数的小时数有前导零。  
  H               24   小时制的小时。一位数的小时数没有前导零。  
  HH             24   小时制的小时。一位数的小时数有前导零。  
  m               分钟。一位数的分钟数没有前导零。  
  mm             分钟。一位数的分钟数有一个前导零。  
  s               秒。一位数的秒数没有前导零。  
  ss             秒。一位数的秒数有一个前导零。  
  f               秒的小数精度为一位。其余数字被截断。

感觉会经常常用的:
(1)

 //这里 "yyyy年MM月dd日 HH:mm:ss" HH:代表24小时制,hh:代表12小时制
 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

        long currentTimeMillis = System.currentTimeMillis();//获取当前时间戳
        //Date date = new Date(currentTimeMillis);

        //和上面的两个代码效果一样,都是获取当前时间
        Date date = new Date();

        //传入时间戳
        String strDate1 = simpleDateFormat.format(currentTimeMillis);
        System.out.println("TestJava.main strDate=" + strDate1);
        //传入Date实例,效果和上面的一样
        String strDate2 = simpleDateFormat.format(date);
        System.out.println("TestJava.main strDate=" + strDate2);

打印结果:
TestJava.main strDate=2017年09月08日 17:11:19
TestJava.main strDate=2017年09月08日 17:11:19

(2)

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String str = "2017年09月08日 17:23:58";
        try {
            Date date1 = simpleDateFormat.parse(str);
            long time = date1.getTime();
            System.out.println("TestJava.main time=" + time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

打印结果:
TestJava.main time=1504862638000

使用不是很频繁的:

 SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:SS");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");

        SimpleDateFormat simpleDateFormat5 = new SimpleDateFormat("yy年MM月dd日 HH:mm");

        SimpleDateFormat simpleDateFormat6=new SimpleDateFormat("一年中的第 D 天, 一年中第w个星期, 一月中第W个星期, 在一天中k时, z时区, 在 a ");

        Date date = new Date();

        System.out.println(simpleDateFormat1.format(date));
        System.out.println(simpleDateFormat2.format(date));
        System.out.println(simpleDateFormat3.format(date));
        System.out.println(simpleDateFormat4.format(date));
        System.out.println(simpleDateFormat5.format(date));
        System.out.println(simpleDateFormat6.format(date));

打印结果:
2017年09月09日 08:55:41:985
2017/09/09 08:55:41
2017-09-09 08:55:41
2017年09月09日 08:55:41 星期六
17年09月09日 08:55
一年中的第 252 天, 一年中第36个星期, 一月中第2个星期, 在一天中8时, CST时区, 在 上午


Calendar

注意事项:
Calendar 的 month 从 0 开始,也就是全年 12 个月由 0 ~ 11 进行表示。
而 Calendar.DAY_OF_WEEK 定义和值如下:

    Calendar.SUNDAY = 1

    Calendar.MONDAY = 2

    Calendar.TUESDAY = 3

    Calendar.WEDNESDAY = 4

    Calendar.THURSDAY = 5

    Calendar.FRIDAY = 6

    Calendar.SATURDAY = 7
 // 创建 Calendar 对象
        Calendar calendar = Calendar.getInstance();
        //calendar.set(2017,9,8);
        calendar.setTime(new Date());

        // 显示年份
        int year = calendar.get(Calendar.YEAR);
        System.out.println("year is = " + String.valueOf(year));

        // 显示月份 (从0开始, 实际显示要加一)
        int month = calendar.get(Calendar.MONTH);
        System.out.println("nth is = " + (month + 1));

        int date = calendar.get(Calendar.DATE);
        System.out.println("date is = " + date);


        // 本周几
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println("week is = " + week);

        // 今年的第 N 天
        int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
        System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);

        // 本月第 N 天
        int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));

        // 3小时以后
        calendar.add(Calendar.HOUR_OF_DAY, 3);
        int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
        System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);

        // 当前分钟数
        int MINUTE = calendar.get(Calendar.MINUTE);
        System.out.println("MINUTE = " + MINUTE);

        // 15 分钟以后
        calendar.add(Calendar.MINUTE, 15);
        MINUTE = calendar.get(Calendar.MINUTE);
        System.out.println("MINUTE + 15 = " + MINUTE);

        // 30分钟前
        calendar.add(Calendar.MINUTE, -30);
        MINUTE = calendar.get(Calendar.MINUTE);
        System.out.println("MINUTE - 30 = " + MINUTE);

        // 格式化显示,由于前面对时间,进行了后调和前调,打印的时间也不会是当前时间啦
        String str1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
        System.out.println(str1);

        // 重置 Calendar 显示当前时间
        calendar.setTime(new Date());
        String str2 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
        System.out.println(str2);

打印结果:
year is = 2017
nth is = 9
date is = 9
week is = 7
DAY_OF_YEAR is = 252
DAY_OF_MONTH = 9
HOUR_OF_DAY + 3 = 12
MINUTE = 38
MINUTE + 15 = 53
MINUTE - 30 = 23
2017-09-09 12:23:08:775
2017-09-09 09:38:08:780


String.format

(1)常用日期和时间的格式化

public static void main(String[] args) {

        Date today = new Date();

        String a = String.format("%tF", today);
        String b = String.format("%tD", today);
        String c = String.format("%tr", today);
        String d = String.format("%tT", today);
        String e = String.format("%tR", today);
        String m = String.format("%tS", today);//获取秒

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(m);


    }

打印结果:
2017-09-09
09/09/17
09:54:50 上午
09:54:50
09:54
50

(2)对日期的格式化

        Date today = new Date();

        String a1 = String.format(Locale.US, "%tb", today);//英文的星期简写
        String a2 = String.format(Locale.US, "%tB", today);//英文的星期全写

        String b = String.format("%ta", today);
        String c = String.format("%tA", today);


        String d = String.format("%tY", today);
        String e = String.format("%ty", today);

        String g = String.format("%tm", today);//获取月
        String i = String.format("%td", today);//获取日
        String m = String.format("%te", today);//获取日
        String n = String.format("%tj", today);//获取是一年的第多少天

        System.out.println(a1);
        System.out.println(a2);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(g);
        System.out.println(i);
        System.out.println(m);
        System.out.println(n);

打印结果:
Sep
September
星期六
星期六
2017
17
09
09
9
252

(3)对时间的格式化

Date today = new Date();

        String a1 = String.format("%tH", today); //获取21小时制的小时(补0)
        String a2 = String.format("%tk", today);//获取21小时制的小时(不补0)

        String b = String.format("%tI", today);//获取12小时制的小时(补0)
        String c = String.format("%tl", today);//获取12小时制的小时(不补0)


        String d = String.format("%tM", today);//获取分钟
        String e = String.format("%tS", today);//获取秒
        String f = String.format("%tL", today);//获取3位的毫秒
        String f1 = String.format("%tN", today);//获取9位的毫秒

        String g = String.format("%tp", today);//显示上下午标记
        String i = String.format(Locale.US, "%tp", today);//显示上下午标记

        System.out.println(a1);
        System.out.println(a2);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);
        System.out.println(f1);
        System.out.println(g);
        System.out.println(i);

打印结果:

11
11
11
11
32
31
090
090000000
上午
am
发布了82 篇原创文章 · 获赞 131 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/da_caoyuan/article/details/77896778