0%

常用类-3

常用类

日期类

第一代日期类Date
  • 精确到毫秒,代表特定的瞬间。

    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
    package com.f.chapter13;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    /**
    * @author fzy
    * @date 2023/4/28 16:41
    */
    public class Date_ {
    public static void main(String[] args) throws ParseException {
    /*
    * 1. 这里的 Date 类是在 java.util 包下
    * 2. 获取当前系统时间
    * 3. 默认输出的日期格式是国外的方式,需要对格式进行转换
    * */
    Date date1 = new Date();
    System.out.println("当前日期为:" + date1); //默认输出的日期格式是国外的方式:当前日期为:Fri Apr 28 16:51:47 CST 2023

    /*
    * 1. 创建 SimpleDateFormat 对象,可以指定相应的格式
    * 2. 这里的格式使用的字母是规定好的,不能乱写
    * */
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String date1Format = sdf.format(date1);
    System.out.println("当前日期为:" + date1Format); //经过格式化后的日期格式:当前日期为:2023年04月28日 16:51:47

    /*
    * 1. 可以把一个格式化的 String 转换为对应的 Date
    * 2. 转换后得到的 Date 仍然是国外的日期格式,如果希望指定输出格式,仍然需要使用 SimpleDateFormat
    * */
    String s = "2023年04月28日 16:51:47";
    Date parse = sdf.parse(s);
    System.out.println(parse); //Fri Apr 28 16:51:47 CST 2023
    }
    }
第二代日期类Calendar
  • public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar >

    Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、 DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

    1. Calendar 是一个抽象类,并且构造器是 protected 的;
    2. 可以通过 getInstance() 来获取实例;
    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
    package com.f.chapter13;

    import java.util.Calendar;

    /**
    * @author fzy
    * @date 2023/4/28 17:11
    */
    public class Calendar_ {
    public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance(); //通过 getInstance 方法来获取 Calendar 实例
    System.out.println(calendar);

    //获取日历对象的某个日历字段
    System.out.println("年:" + calendar.get(Calendar.YEAR));
    System.out.println("月:" + (calendar.get(Calendar.MONTH) + 1)); //Calendar.MONTH在返回月的时候,是从0开始编号的
    System.out.println("日:" + calendar.get(Calendar.DAY_OF_MONTH));
    System.out.println("小时:" + calendar.get(Calendar.HOUR_OF_DAY));
    System.out.println("分钟:" + calendar.get(Calendar.MINUTE));
    System.out.println("秒:" + calendar.get(Calendar.SECOND));

    //注意:Calendar 没有专门的格式化方法,需要程序员自己组合输出
    //System.out.println("现在的时间为:" + calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日" +
    // calendar.get(Calendar.HOUR_OF_DAY) + "时" + calendar.get(Calendar.MINUTE) + "分" + calendar.get(Calendar.SECOND) + "秒");
    String formatString = "%d年%d月%d日%d时%d分%d秒";
    String time = String.format(formatString, calendar.get(Calendar.YEAR), (calendar.get(Calendar.MONTH) + 1), calendar.get(Calendar.DAY_OF_MONTH),
    calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
    System.out.println("现在的时间为:" + time);
    }
    }
    • 注意:Calendar 没有专门的格式化方法,需要程序员自己组合输出。可以用 String.format 方法
第三代日期类LocalDate、LocalTime、LocalDateTime
  • 前面两代日期类的不足分析:JDK1.0 中包含了一个 java.util.Date 类,但是他的大多数方法已经在 JDK1.1 引入 Calendar 类之后就被弃用了,而 Calendar 也存在问题:

    1. 可变性:像日期和时间这样的类应该是不可变的;
    2. 偏移性:Date 中的年份是从 1900 开始的,而月份都从 0 开始;
    3. 格式化:格式化只对 Date 有用,Calendar 则不行;
    4. 此外,它们也不是线程安全的、不能处理闰秒(每隔2天,多出1s)等。
  • 在 JDK1.8 加入 LocalDate、LocalTime、LocalDateTime 类。

    • LocalDate:日期,包含年月日
    • LocalTime:时间,包含时分秒
    • LocalDateTime:日期时间,包含年月日时分秒
    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
    package com.f.chapter13;

    import java.time.LocalTime;
    import java.time.LocalDate;
    import java.time.LocalDateTime;

    /**
    * @author fzy
    * @date 2023/4/28 17:42
    */
    public class LocalDateTime_ {
    public static void main(String[] args) {
    /*
    * 1. 使用 now() 返回表示当前日期时间的对象
    * */
    LocalDateTime localDateTime = LocalDateTime.now();
    //LocalDate localDate = LocalDate.now();
    //LocalTime localTime = LocalTime.now();
    System.out.println(localDateTime); //2023-04-28T19:43:26.869
    System.out.println("年:" + localDateTime.getYear());
    System.out.println("月:" + localDateTime.getMonth()); //月:APRIL
    System.out.println("月:" + localDateTime.getMonthValue()); //月:4
    System.out.println("日:" + localDateTime.getDayOfMonth());
    System.out.println("时:" + localDateTime.getHour());
    System.out.println("分:" + localDateTime.getMinute());
    System.out.println("秒:" + localDateTime.getSecond());
    }
    }
★第三代日期类方法
  1. DateTimeFormatter 格式日期类。使 用类似于 Date 类中的 SimpleDateFormat

  2. plus:对当前的时间进行加法。

  3. minus:对当前的时间进行减法。

  4. isAfter:检查此日期时间是否在指定的日期时间之后。

  5. isBefore:检查此日期时间是否在指定的日期时间之前。

  6. isEqual:检查此日期时间是否和指定的日期时间相等。

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
package com.f.chapter13;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author fzy
* @date 2023/4/28 19:44
*/
public class LocalDateTimeMethod {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("格式化前的日期为:" + now);
//1. 使用 DateTimeFormatter 对象来对 LocalDateTime 格式化
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String formatNow = dtf.format(now);
System.out.println("格式化后的日期为:" + formatNow);

//2. plus 对当前的时间进行加法
//看看890天后,是什么时间
LocalDateTime future = now.plusDays(890);
System.out.println("890天后的时间为:" + dtf.format(future));

//3. minus 对当前的时间进行减法
//看看750个小时前,是什么时间
LocalDateTime past = now.minusHours(750);
System.out.println("750个小时前的时间为:" + dtf.format(past));

//4. isAfter 检查此日期时间是否在指定的日期时间之后
System.out.println(future.isAfter(now)); //true

//5. isBefore 检查此日期时间是否在指定的日期时间之前
System.out.println(past.isBefore(now)); //true

//6. isEqual 检查此日期时间是否和指定的日期时间相等
System.out.println(now.isEqual(now)); //true
}
}
---------------The End---------------