【Java基础进阶笔记】- Day01 - 第二章 日期时间类

Java基础进阶笔记 - Day01 - 第二章 日期时间类

系统:Win10
JDK:1.8.0_121
IDE:IntelliJ IDEA 2017.3.7

2.1 Date类

概述
java.util.Date类:表示特定的瞬间,精确到毫秒
继续查阅Date类的描述,发现Date拥有多个构造函数,只是部分已经过时,但是其中有未过时的构造函数可以把毫秒值转成日期对象

  • public Date():分配Date对象并初始化对象,以表示分配它的时间(精确到毫秒)
  • public Date(long date):分配Date对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即1970年1月1日00:00:00 GMT)以来的指定毫秒数

小贴士:由于我们中国处于东八区,所以我们的基准时间为1970年1月1日8时0分0秒

简单来说:使用无参构造,可以自动设置当前系统时间的毫秒时刻;指定long类型的构造参数,可以自定义毫秒时刻。例如:

public class DateDemo01 {
    
    
    public static void main(String[] args) {
    
    
        // 创建日期对象,把当前时间转换为日期对象
        System.out.println(new Date()); // Wed Nov 25 16:42:38 CST 2020
        // 创建日期对象,把当前的毫秒值转成日期对象
        System.out.println(new Date(0L)); // Thu Jan 01 08:00:00 CST 1970
    }
}

运行结果:
在这里插入图片描述

小贴士:在使用println方法时,会自动调用Date类中的toString方法。Date类对Object类中的toString方法进行了覆盖重写,所以结果为指定格式的字符串
toLocaleString可以按本地时间格式输出时间

常用方法
Date类中的多数方法已经过时,常用的方法有:

  • public long getTime():把日期对象转换成对应的时间毫秒值

2.2 DateFormat类

java.text.DateFormat:是时间/日期格式化子类的抽象类,我们通过这个类可以帮助我们完成日期和文本之间的转换,也就是可以在Date对象与String对象之间进行来回转换

  • 格式化:按照指定的格式,从Date对象转换为String对象
  • 解析:按照指定的格式,从String对象转换为Date对象

构造方法
由于DateFormat为抽象类,不能直接使用,所以需要常用的子类java.text.SimpleDateFormat。这个类需要一个模式(格式)来指定格式化或解析的标准。构造方法为:

  • public SimpleDateFormat(String pattern):用给定的模式和默认语言环境的日期格式符号构造SimpleDateFormat
    参数pattern是一个字符串,代表日期时间的自定义格式

格式规则
常用的格式规则为:

标识字母(区分大小写) 含义
y 当天所在的年度
Y 当周所在的年度,跨年这一周的每一天都算下一年的年度
M
d
H
m
s

备注:更详细的格式规则,可以参考SimpleDateFormat类的API()文档,年的用法特别要注意,一般都用小写的 y

创建SimpleDateFormat对象的代码如下:

public class DateDemo02 {
    
    
    public static void main(String[] args) {
    
    
        // 对应的日期格式如:2020-11-25 19:52:56
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
}

常用方法
DateFormat类的常用方法有:

  • public String format(Date date):将一个Date格式化为日期/时间字符串
  • public Date parse(String source):从给定字符串开始解析文本,以生成一个日期

format方法
使用format方法的代码如下:

/*
 将Date对象转换成String
 */
public class DateDemo03 {
    
    
    public static void main(String[] args) {
    
    
        Date date = new Date();
        // 创建日期格式化对象,在获取格式化对象时可以指定风格
        DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String str = dateFormat.format(date);
        System.out.println(str);
    }
}

运行结果:
在这里插入图片描述

parse方法
使用parse方法的代码如下:

/*
 把String按指定格式转换成Date对象
 */
public class DateDemo04 {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        // 创建日期格式化对象,在获取格式化对象时可以指定风格
        DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String str = "2020年11月26日";
        Date date = dateFormat.parse(str);
        System.out.println(date); // Thu Nov 26 00:00:00 CST 2020
    }
}

运行结果:
在这里插入图片描述

2.3 练习

使用日期时间相关的API,计算一个人已经出生了多少天
解题思路:

  1. 输入自己出生日期
  2. 获取当前日期
  3. 将两个日期转成毫秒值相减(当前时间-出生日期)
  4. 将毫秒值的差值转成天

代码实现:

/*
 使用日期时间相关的API,计算一个人已经出生了多少天
 */
public class Test {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        /*
         1.输入出生日期
         2.获取当前日期
         3.将两个日期转成毫秒值相减(当前时间-出生日期)
         4.将毫秒值的差值转成天
         */
        // 1.输入出生日期
        System.out.println("请输入出生日期:格式 yyyy-MM-dd");
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = dateFormat.parse(str);
        // 2.获取当前日期
        Date today = new Date();
        // 3.将两个日期转成毫秒值相减
        long time = today.getTime() - birthday.getTime();
        // 4.将毫秒值的差值转成天
        long day = time / 1000 / 60 / 60 / 24;
        if (day < 0) {
    
    
            System.out.println("还没出生!");
        } else {
    
    
            System.out.println(day);
        }
    }
}

运行结果:
在这里插入图片描述

2.4 Calendar类

概念
以前我们生活中应该都有见到过手撕的纸质日历
在这里插入图片描述

java.util.Calendar 是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装为静态成员变量,方便获取。日历类就是方便获取各个时间属性的
获取方式
Calendar为抽象类,由于语言敏感性,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,返回子类对象,如下:
Calendar静态方法

  • public static Calendar getInstance():使用默认时区和语言环境获得一个日历
    例如:
public class CalendarDemo01 {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        Calendar calendar = Calendar.getInstance();
    }
}

常用方法
根据Calendar类的API文档,常用方法有:

  • public int get(int field):返回给定日历字段的值
  • public void set(int field, int value):将给定的日历字段设置为给定值
  • public abstract void add(int field, int amount):根据日历的规则,为给定的日历字段添加或减去指定的时间量
  • public Date getTime():返回一个表示此Calendar时间值(从历元[即格林威治标准时间1970年1月1日的00:00:00]至现在的毫秒偏移量)的Date对象

Calendar类中提供很多成员常量,代表给定的日历字段:

字段值 含义
YEAR
MONTH 月(从0开始,可以+1使用)
DAY_OF_MONTH 月中的天(几号)
HOUR 时(12小时制)
HOUR_OF_DAY 时(24小时制)
MINUTE
SECOND
DAY_OF_WEEK 周中的天(周几,周日为1,可以-1使用)

get/set方法
get方法用来获取指定字段的值,代码如下:

public class CalendarDemo02 {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();
        // 获取年
        int year = calendar.get(Calendar.YEAR);
        // 获取月
        int month = calendar.get(Calendar.MONTH) + 1;
        // 获取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(year + "年" + month + "月" + day + "日");
    }
}

运行结果:
在这里插入图片描述
set方法用来设置指定字段的值,代码如下:

public class CalendarDemo03 {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();
        // 设置年
        calendar.set(Calendar.YEAR, 2345);
        // 设置月
        calendar.set(Calendar.MONTH, 05);
        // 设置日
        calendar.set(Calendar.DAY_OF_MONTH, 07);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(year + "年" + month + "月" + day + "日");
    }
}

运行结果:
在这里插入图片描述

add方法
add方法可以对指定日历字段的值进行加减操作,如果第二个参数为正数则加上偏移量,如果为负数则减去偏移量。代码如下:

public class CalendarDemo04 {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(year + "年" + month + "月" + day + "日");
        // 使用add方法
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -14);
        int year1 = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        int day1 = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(year1 + "年" + month1 + "月" + day1 + "日");
    }
}

运行结果:
在这里插入图片描述

getTime方法
Calendar中的getTime方法并不是获取毫秒值,而是拿到对应的Date对象

public class CalendarDemo05 {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        System.out.println(date);
    }
}

运行结果:
在这里插入图片描述

小贴士:
西方星期的开始为周日,中国为周一。所以DAY_OF_WEEK中的1-7代表周日-周六
在Calendar类中,月份的表示是以0-11代表1-12月
日期是有大小关系的,时间靠后,时间越大

猜你喜欢

转载自blog.csdn.net/qq_35132089/article/details/111193050
今日推荐