JavaAPI-日期类

一、日期类

1.1、Date 类

1.1.1、Date 类概述

  • Date 代表了一个特定的时间,精确到毫秒。

1.1.2、Date 的构造方法

方法名 说明
public Date() 分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
public Date(long date) 分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数

1.1.2.1、Date()

  • 分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒。
// 打印当前时间
Date d = new Date();
System.out.println(d);

1.1.2.2、Date(long date)

  • 分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数。
// 1000*60*60:时间进制换算
long date = 1000*60*60;
Date d = new Date(date);
System.out.println(d);

1.1.3、Date 常用方法

方法名 说明
public long getTime() 获取的是日期对象从 1970年1月1日 00:00:00 到现在的毫秒值
public void setTime(long time) 设置时间,给的是毫秒值

1.1.3.1、getTime() 方法

  • 获取的是日期对象从 1970年1月1日 00:00:00 到现在的毫秒值。
Date d = new Date();
System.out.println(d.getTime());
System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");

1.1.3.2、setTime(long time) 方法

  • 设置时间,给的是毫秒值。
Date d = new Date();

  //long time = 1000*60*60;
// 获取系统当前时间
long time = System.currentTimeMillis();
d.setTime(time);
System.out.println(d);

1.2、SimpleDateFormat 类

1.2.1、SimpleDateFormat 类概述

  • SimpleDateFormat 是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。

  • 常用的模式字母及对应关系如下:

    字母 日期
    y
    M
    d
    H
    m
    s

1.2.2、SimpleDateFormat 的构造方法

方法名 说明
public SimpleDateFormat​() 构造一个 SimpleDateFormat,使用默认模式和日期格式
public SimpleDateFormat​(String pattern) 构造一个 SimpleDateFormat​ 使用给定的模式和默认的日期格式

1.2.3、SimpleDateFormat 格式化和解析日期

1.2.3.1、格式化(从 Date 到 String)

  • public final String format(Date date):将日期格式化成日期/时间字符串
Date d = new Date();
SimpleDateFormat sf = new SimpleDateFormat();
//  手动设置日期格式
//  SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String f = sf.format(d);
System.out.println(f);

1.2.3.2、解析(从 String 到 Date)

  • publi Date parse(String source):从给定字符串的开始解析文本以生成日期。
Date d = new Date();
String s = "2048-08-09 11:11:11";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sf.parse(s);  // parse() 方法需要抛出异常。
System.out.println(dd);
  • 注意:
    • String 日期格式必须和 SimpleDateFormat() 中的日期格式相匹配。

1.3、Calendar 类

1.3.1、Calendar 类概述

  • Calendar 为某一时刻和一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法。
  • Calendar 提供了一个类方法 getlnstance 用于获取 Calendar 对象,其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getlnstance();

1.3.2、Calendar 的常用方法

方法名 说明
public int get(int field) 返回给定日历字段的值
public abstract void add(int field,int amount) 根据日历的规则,将指定的时间量添加或减去给定的日历字段
public final void set(int year,int month,int date) 设置当前日历的年月日

1.3.2.1、get(int field) 方法

  • 返回给定日历字段的值。
// 获取对象
Calendar c = Calendar.getInstance(); // 通过多态创建对象

// 通过 get() 方法传日历字段,我们就能得到对应字段的值
int year = c.get(Calendar.YEAR);    // 年
int march = c.get(Calendar.MARCH) + 1;  // 月:月是从 0 开始的,所以需要 +1
int date = c.get(Calendar.DATE);    // 日
System.out.println(year + "年" + march + "月" + date + "日");

1.3.2.2、add(int field,int amount) 方法

  • 根据日历的规则,将指定的时间量添加或减去给定的日历字段。
// 获取对象
Calendar c = Calendar.getInstance(); // 通过多态创建对象

// 需求:10 年后的 5 天前
c.add(Calendar.YEAR,10);
c.add(Calendar.DATE,-5);

int year = c.get(Calendar.YEAR);
int march = c.get(Calendar.MARCH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + march + "月" + date + "日");

1.3.2.3、set(int year,int month,int date) 方法

  • 设置当前日历的年月日。
// 获取对象
Calendar c = Calendar.getInstance(); // 通过多态创建对象

// 设置当前日历的年月日
c.set(2020,11,1+7);

int year = c.get(Calendar.YEAR);
int march = c.get(Calendar.MARCH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + march + "月" + date + "日");
posted @ 2021-02-20 10:47    阅读(55)  评论(0)    收藏  举报