zoukankan      html  css  js  c++  java
  • 时间类



    /**
    * 日期:2019/7/24.
    * 功能:
    * 1. 时间戳 -> Instant -> LocalDateTime -> Instant -> 时间戳
    * 2. 格式化:LocalDateTime -> 字符串
    * 解 析:字符串 -> LocalDateTime
    */
    public class MyDemo {

    @Test
    public void TimeStampAndDateTime() {
    System.out.println("System 获取的时间戳:" + System.currentTimeMillis());
    System.out.println("Instant 获取的时间戳:" + Instant.now().getEpochSecond() + " ");
    Long systemTimeMillis = System.currentTimeMillis();

    //long型时间戳转 Instant
    Instant instant = Instant.ofEpochMilli(systemTimeMillis);
    System.out.println("long型时间戳转 Instant: " + systemTimeMillis + " --> " + instant + " ");

    //Instant 转 LocalDateTime
    LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    System.out.println("Instant 转 LocalDateTime:" + instant + " --> " + ldt + " ");

    //LocalDateTime 转 Instant
    Instant instant2 = ldt.toInstant(ZoneOffset.of("+8"));
    System.out.println("LocalDateTime 转 Instant:" + ldt + " --> " + instant2 + " ");

    //Instant 转 long型时间戳
    long systemTimeMillis2 = instant2.toEpochMilli();
    System.out.println("Instant 转 long型时间戳:" + instant2 + " --> " + systemTimeMillis2);
    }


    @Test
    public void formatAndParse() {
    LocalDateTime ldt = LocalDateTime.now();

    //将LocalDateTime格式化成特定格式的字符串
    DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
    String newDateString = dtf2.format(ldt);
    System.out.println("LocalDateTime --> 字符串 : " + ldt + " --> " + newDateString);

    //将字符串解析成 LocalDateTime
    LocalDateTime ldt2 = LocalDateTime.parse(newDateString, dtf2);
    System.out.println("字符串 --> LocalDateTime : " + newDateString + " --> " + ldt2);
    }
    }
  • 相关阅读:
    Cogs 465. 挤牛奶
    洛谷P1083 借教室
    Cogs 1264. [NOIP2012] 开车旅行(70分 暴力)
    2017-10-19 NOIP模拟赛
    Codevs 2144 砝码称重 2
    洛谷P1450 [HAOI2008]硬币购物
    洛谷P2534 [AHOI2012]铁盘整理
    洛谷P1731 生日蛋糕
    2017-10-18 NOIP模拟赛
    洛谷P1074 靶形数独
  • 原文地址:https://www.cnblogs.com/ctaixw/p/11240771.html
Copyright © 2011-2022 走看看