Java8 LocalDate

Java 8 的所有日期和时间API都是不可变类并且线程安全,而现有的Date和Calendar API中的java.util.Date和SimpleDateFormat是非线程安全的。
javax.time.ZoneId 获取时区。LocalDate和LocalTime类获取时间

 //获取当天的日期及年月日
    public static void getCurrentDate(){
        LocalDate today = LocalDate.now();
        System.out.println("Local date : " + today);
        int year = today.getYear();
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();
        System.out.printf("Year : %d  Month : %d  day : %d t %n", year, month, day);
    }
    //处理特定日期
    public static void handleSpecilDate(){
        LocalDate dateOfBirth = LocalDate.of(2020, 06, 21);
        System.out.println("The specil date is : " + dateOfBirth);
    }
    //获取当前时间
    public static void getCurrentTime(){
        LocalTime time = LocalTime.now();
        System.out.println("local time now : " + time);
        // 增加两小时
        LocalTime newTime = time.plusHours(2); 
        System.out.println("Time after 2 hours : " +  newTime);
        //增加一周
         LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);    
        System.out.println("Date after 1 week : " + nextWeek);
    }
    public static void clock(){
        // 根据系统时间返回当前时间并设置为UTC。
        Clock clock = Clock.systemUTC();
        System.out.println("Clock : " + clock);
        // 根据系统时钟区域返回时间
        Clock defaultClock = Clock.systemDefaultZone();
        System.out.println("Clock : " + clock);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值