zoukankan      html  css  js  c++  java
  • JDK8中新日期时间API的复习

     1 package day2_15;
     2 
     3 import org.junit.Test;
     4 
     5 import java.time.Instant;
     6 import java.time.LocalDateTime;
     7 import java.time.OffsetDateTime;
     8 import java.time.ZoneOffset;
     9 import java.time.format.DateTimeFormatter;
    10 import java.time.temporal.TemporalAccessor;
    11 
    12 /**
    13  * @Author Tianhao
    14  * @create 2021-02-14-20:53
    15  */
    16 public class DateTimeTest {
    17 
    18     @Test
    19     public void testLocalDateTime() {
    20         //LocalDateTime  类似于Calendar
    21         //两种实例化方式
    22         //方式一:now():当前日期时间
    23         LocalDateTime now = LocalDateTime.now();
    24         System.out.println(now);//2021-02-16T15:54:17.649
    25         //方式二:of():指定日期时间
    26         LocalDateTime past = LocalDateTime.of(2015, 6, 7, 11, 59, 59);
    27         System.out.println(past);//2015-06-07T11:59:59
    28         //获取相关属性
    29         System.out.println(now.getDayOfMonth());//16
    30         System.out.println(past.getMonthValue());//6
    31 
    32         System.out.println("**********");
    33         //对LocalDateTime操作(体现不变性)
    34         //设置相关属性,返回一个新的日期时间对象
    35         LocalDateTime now2 = now.withDayOfMonth(10);
    36         LocalDateTime past2 = past.withMonth(5);
    37         System.out.println(now2);//2021-02-10T16:02:34.914
    38         System.out.println(past2);//2015-05-07T11:59:59
    39         System.out.println(now.getDayOfMonth());//16
    40         System.out.println(past.getMonthValue());//6
    41 
    42         System.out.println("**********");
    43         //增加相关属性值,返回一个新的日期时间对象
    44         LocalDateTime now3 = now.plusDays(4);
    45         LocalDateTime past3 = past.plusMonths(3);
    46         System.out.println(now3);//2021-02-20T16:09:39.983
    47         System.out.println(past3);//2015-09-07T11:59:59
    48         System.out.println(now.getDayOfMonth());//16
    49         System.out.println(past.getMonthValue());//6
    50 
    51         System.out.println("**********");
    52         //减少相关属性值,返回一个新的日期时间对象
    53         LocalDateTime now4 = now.minusDays(8);
    54         LocalDateTime past4 = past.minusMonths(3);
    55         System.out.println(now4);//2021-02-08T16:13:43.328
    56         System.out.println(past4);//2015-03-07T11:59:59
    57         System.out.println(now.getDayOfMonth());//16
    58         System.out.println(past.getMonthValue());//6
    59     }
    60 
    61     @Test
    62     public void testInstant() {
    63         //两种实例化方式
    64         //方式一:本初子午线的标准时间
    65         Instant now = Instant.now();
    66         System.out.println(now);//2021-02-16T08:19:51.419Z
    67         //中国处于东八区,所以小时需要加上8小时的偏移量
    68         OffsetDateTime chinaNow = now.atOffset(ZoneOffset.ofHours(8));
    69         System.out.println(chinaNow);//2021-02-16T16:19:51.419+08:00
    70 
    71         //方式二:指定与1970年1月1日0时0秒0分的毫秒数差值来创建对象
    72         Instant now2 = Instant.ofEpochMilli(0);
    73         Instant now3 = Instant.ofEpochMilli(3600000L);
    74         System.out.println(now2);//1970-01-01T00:00:00Z
    75         System.out.println(now3);//1970-01-01T01:00:00Z
    76 
    77         //对Instant操作
    78         //获取当前时刻与1970年1月1日0时0秒0分的毫秒数差值
    79         long milli = now.toEpochMilli();
    80         System.out.println(milli);//1613464115414
    81     }
    82 
    83     @Test
    84     public void testDateTimeFormatter() {
    85         //有三种方式创建对象,这里只测常用的一种
    86         //自定义格式器
    87         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
    88         //对DateTimeFormatter的操作
    89         //格式化  LocalDateTime -->String
    90         String str = formatter.format(LocalDateTime.now());
    91         System.out.println(str);//2021-02-16 04:39:21
    92         //解析  String--->LocalDateTime
    93         TemporalAccessor localDateTime = formatter.parse("2021-02-16 04:39:21");
    94         //{SecondOfMinute=21, NanoOfSecond=0, MicroOfSecond=0, MinuteOfHour=39, MilliOfSecond=0, HourOfAmPm=4},ISO resolved to 2021-02-16
    95         System.out.println(localDateTime);
    96     }
    97 
    98 }
  • 相关阅读:
    react创建项目01
    jq中的时间转化
    vue-resource调用promise取数据方式整理
    ES6 语法
    js获取时间
    trigger()模拟事件操作
    移动端调出不同需求的键盘
    移动端ios滚动卡顿问题
    godaddy购买的域名解析到aws的route53
    k8s 排错
  • 原文地址:https://www.cnblogs.com/zui-ai-java/p/14406917.html
Copyright © 2011-2022 走看看