Java8新特性之ZonedDateTime类

ZonedDateTime类


一、概述

ZonedDateTime处理日期和时间与相应的时区

二、常用方法

  1. ZonedDateTime.now():获取系统当前日期和时间。
  2. String format(DateTimeFormatter formatter):
    根据DateTimeFormatter对象设置的格式将日期转换为一个字符串
  3. plusDays(int days); 加上指定的天数,

【案例】将当前日期格式化为字符串并显示年、月、日、时、分和秒。

测试代码如下:

public class Test10_zonedDateTime {
    /**
     * 【案例】将当前日期格式化为字符串并显示年、月、日、时、分和秒。
     * 
     */
    public static void main(String[] args) {
        ZonedDateTime now = ZonedDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yy HH:mm:ss");
        String strDate = now.format(formatter);
        System.out.println("format方法返回的字符串:"+strDate);
        now = now.plusDays(1);//当前日期 加上1天
        String strDate2 = now.format(formatter);
        System.out.println("加上一天后的日期:"+strDate2);
    }
}

运行上述代码控制台打印结果如下:

format方法返回的字符串:08/19/18 23:05:40
加上一天后的日期:08/20/18 23:05:40

本文代码资源地址:
https://github.com/gitHjx/java8.git

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值