时间及其相关类

System:public static long currentTimeMillis()返回当前时间(以毫秒为单位)。从1970.01.01开始

long time = System.currentTimeMillis();

Date类public Date()   public Date(long date)使⽤⽆参构造,可以⾃动设置当前系统时间的毫秒时刻;指定long类型的构造参数,可以⾃定义毫秒时刻。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

System.out.println(sdf.format(new Date(0L))); //1970-01-01 08:00:00  

由于我们处于东⼋区,所以我们的基准时间为1970年1⽉1⽇8时0分0秒。

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

public void setTime(long date):使用给定的毫秒时间值设置现有的Date对象。

Calendar类(抽象类),由于语⾔敏感性,Calendar类在创建对象时并⾮直接创建,⽽是通过静态⽅法创建,返回⼦类对象,如下:

Calendar静态⽅法public static Calendar getInstance():使⽤默认时区和语⾔环境获得⼀个⽇历

public int get(int field) :返回给定⽇历字段的值。

public void set(int field, int value) :将给定的⽇历字段设置为给定值。

public abstract void add(int field, int amount) :根据⽇历的规则,为给定的⽇历字段

添加或减去指定的时间量。

public Date getTime() :返回⼀个表示此Calendar时间值(从历元到现在的毫秒偏移量)的Date

对象。

long Date Calendar 三者之间的转化:long <=>Date<=>Calendar<=>long

long->Date:long time=System.currentTimeMillis();Date date = new Date(time);  date.setTime(time);

Date->long:Date date = new Date();  long time = date.getTime();

Date->Calendar:Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date);

Calendar->Date:Calendar cal = Calendar.getInstance();  Date date = cal.getTime();

long->Calendar:long time=System.currentTimeMillis(); Calendar cal = Calendar.getInstance();

cal.setTimeInMillis(time);

Calendar->long:Calendar cal = Calendar.getInstance(); long time = cal.getTimeInMillis();

 DateFormat类(抽象类)通过这个类可以帮我们完成⽇期和⽂本之间的转换,也就是可以在Date对象与String对象之间进⾏来回转换。

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

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

SimpleDateFormat类与Date类配对使用

由于DateFormat为抽象类,不能直接使⽤,所以需要常⽤的⼦类SimpleDateFormat

public SimpleDateFormat(String pattern) :⽤给定的模式和默认语⾔环境的⽇期格式符号构

造SimpleDateFormat。参数pattern是⼀个字符串,代表⽇期时间的⾃定义格式。

public String format(Date date) :将Date对象格式化为字符串。

public Date parse(String source) :将字符串解析为Date对象

JDK1.8新增的时间日期类

LocalDateTime: 表示时间+日期(年月日时分秒)

LocalDateTime now = LocalDateTime.now(); LocalDateTime local = LocalDateTime.of(年,月,日,时,分);

static LocalDateTime  of(LocalDate date, LocalTime time)从日期和时间获取 LocalDateTime一个实例。  

LocalDateTime的转换:LocalDate  toLocalDate(): LocalDateTime -> LocalDate    

LocalTime  toLocalTime(): LocalDateTime -> LocalTime

LocalDateTime的格式化:

创建方式:DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");

LocalDateTime 转换 String:String text = date.toString(formatter);

String 转换 LocalDateTime:LocalDateTime date = LocalDateTime.parse(text, formatter);

LocalDate: 表示日期(年月日)

static LocalDate  now() 从默认时区的系统时钟获取当前日期。  

static LocalDate  of(int year, int month, int dayOfMonth) 从一年一个月和一天获得一个LocalDate的实例。  

LocalTime: 表示时间(时分秒)static LocalTime now() 从默认时区的系统时钟获取当前时间。  

static LocalTime of(int hour, int minute) 从一小时分钟获取一个 LocalTime的实例。  

static LocalTime of(int hour, int minute, int second) 从一小时,一分钟和秒钟获得一个 LocalTime的实例。  

Period: 计算日期之间的偏差

常用API:long toTotalMonths() :获得此期间的总月数。  

static Period between(开始时间,结束时间): 计算两个时间的间隔

Duration: 计算时间之间的偏差

常用API:static Duration between(开始时间,结束时间): 计算两个时间的间隔

int getNano() :在此持续时间内获得第二秒内的纳秒数。  

long getSeconds() :获取此持续时间内的秒数。  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值