Date、Calendar、SimpleDateFormat

Date

  • Date表示特定的瞬间,精确到毫秒。Date类中的大部分方法都已经被Calendar类中的方法所取代。

  • 时间单位:

    • 1秒=1000毫秒

    • 1毫秒=1000微秒

    • 1微秒=1000纳秒

 

 

Calendar

  • Calendar提供了获取或设置各种日历字段的方法。

  • 构造方法:

    • protected Calendar() :由于修饰符是protected,所以无法直接创建该对象

  • 其他方法

方法名说明
static Calendar getInstance() 使用默认时区和区域获取日历
void set(int year,int month,int date, inthourofday,int minute,int second) 设置日历的年、月、日、时、分、秒。
int get(int field) 返回给定日历字段的值。字段比如年、月、日等
void setTime(Date date) 用给定的Date设置此日历的时间。Date-Calendar
Date getTime() 返回一个Date表示此日历的时间。Calendar-Date
void add(int field,int amount) 按照日历的规则,给指定字段添加或减少时间量
long getTimelnMillies() 毫秒为单位返回该日历的时间值

 

 

SimpleDateFormat

  • SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类。

  • 进行格式化(日期->文本)、解析(文本->日期)

  • 常用的时间模式字母

字母日期或时间示例
y 2019
M 年中月份 08
d 月中天数 10
H 1天中小时数(0-23) 22
m 分钟 16
s 59
S 毫秒 367

 

 

posted @ 2021-03-09 23:41  斯卡哈  阅读(102)  评论(0)    收藏  举报