Java8 新日期时间类(2)

java.time 时区日期时间类

java.time处理本地日期时间的主要类为ZonedDateTime,一般用于使用地区的时区需要被考虑的情况下,用于取代 LocalDate;

ZonedDateTime
静态构造方式摘要
static ZonedDateTime 
static ZonedDateTime  


static ZoneDateTime

static ZonedDateTime
now()
of(int year,int month,int dayofMonth,int hour,int minute[,int second[,int nanoSeconds]] ,ZonId zone)
of(LocalDateTime localDateTime,ZonId zone)

parse
(CharSequence text)
使用当前时间构造一个LocalDateTime对象
使用指定的数值创建一个LocalDateTime对象 (year,month,day从1开始,hour,min,second从0开始)

使用指定LocalDateTime对象创建

使用形如 “2017-2-28T13:29:30[Europe/Pair]”的格式创建对象
成员方法摘要
+LocalDate
+LocalTime
+LocalDateTime

+ZonedDateTime
+int
+ZonedDateTime


+String
toLocalDate()
toLocalTime()
toLocalDateTime()

with
(TemporalField field,long value)
get(TemporalField field)
plus(TemporalAmount amoutToAdd)
plus(long amountToAdd,TemporaUnit unit)

toString()
获取该对象的 LocalDate对象
获取该对象的 LocalTime对象
获取该对象的 LocalDateTime对象

修改ZonedDateTime的时间数值参数(可以链式调用)
获取ZonedDateTime的时间数值参数
对ZonedDateTime的某个时间参数值的流逝量


字符串格式:2017-2-28T13:29:30[Eurpe/Paris]
注:localDateTime的 with,get,plus 方法,都有单项filed的实现  with[Filed](int filed),如 withDayOfMonth(int days),主要Field值如下:
  • with,get:   year,month,dayOfMonth,hour,minute,second,dayOfYear,ZoneSanmeLocal(ZoneId zone);
  • plus :    years,months,weeks,days,hours,minutes,seconds,nanos;

其中ZoneId类的构造如下:
   
   
  1. ZoneId zone = ZoneId.systemDefault(); //使用系统时区
  2. ZoneId zone = ZoneId.of("CTT"); //使用预定义时区
ZoneId string 常量如下:
  • EST - -05:00
  • HST - -10:00
  • MST - -07:00
  • ACT - Australia/Darwin
  • AET - Australia/Sydney
  • AGT - America/Argentina/Buenos_Aires
  • ART - Africa/Cairo
  • AST - America/Anchorage
  • BET - America/Sao_Paulo
  • BST - Asia/Dhaka
  • CAT - Africa/Harare
  • CNT - America/St_Johns
  • CST - America/Chicago
  • CTT - Asia/Shanghai
  • EAT - Africa/Addis_Ababa
  • ECT - Europe/Paris
  • IET - America/Indiana/Indianapolis
  • IST - Asia/Kolkata
  • JST - Asia/Tokyo
  • MIT - Pacific/Apia
  • NET - Asia/Yerevan
  • NST - Pacific/Auckland
  • PLT - Asia/Karachi
  • PNT - America/Phoenix
  • PRT - America/Puerto_Rico
  • PST - America/Los_Angeles
  • SST - Pacific/Guadalcanal
  • VST - Asia/Ho_Chi_Minh
也可以自定义ZoneId时区偏移
   
   
  1. ZoneId zone = ZoneId.ofOffset("UTF",ZoneOffset.ofHours(8));




Java8 的向后兼容

将Date对象转化为LocalDateTime对象
   
   
  1. import java.time.LocalDateTime;
  2. import java.util.Date;
  3. import java.time.Instant;
  4. import java.time.ZoneId;
  5. ...
  6. Date date = new Date();
  7. Instant instant = date.toInstant();
  8. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant,ZoneId.systemDefault);





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值