JDK1.8 新的时间和日期API

java.time包下,主要的几个final修饰的类:java

  • LocalDate:年月日
  • LocalTime:时分秒
  • LocalDateTime:年月日时分秒
  • Instant:时间戳
  • Duration:时间段

Demo:code

public static void main(String[] args){orm

** //1.LocalDate 年月日**

    //当前系统日期(年月日)
	Date localDate = LocalDate.now();
    System.out.println(localDate); // 2019-12-17
    //获取年、月、日、星期
    System.out.println(
            "年:" + localDate.getYear() + ",月:" + localDate.getMonth() + "/" + localDate.get(ChronoField.MONTH_OF_YEAR) + ",日:"
                    + localDate.getDayOfMonth() + ",星期:" + localDate.getDayOfWeek() + "/" + localDate.get(ChronoField.DAY_OF_WEEK)
    ); //年:2019,月:DECEMBER/12,日:17,星期:TUESDAY/2

    //构造某个日期
    System.out.println(LocalDate.of(2019,12,12)); //2019-12-12

  **  //2.LocalTime  时分秒**
    //当前系统时分秒
    LocalTime localTime = LocalTime.now();
    System.out.println(localTime); //10:48:54.036
    //获取时、分、秒
    System.out.println(
            "时:" + localTime.getHour() + "/" + localTime.get(ChronoField.HOUR_OF_DAY) +
                    ",分:" + localTime.getMinute() + "/" + localTime.get(ChronoField.MINUTE_OF_HOUR) +
                    ",秒:" + localTime.getSecond() + "/" + localTime.get(ChronoField.SECOND_OF_MINUTE)
    ); //时:11/11,分:13/13,秒:56/56

    //构造某个时分秒
    System.out.println(LocalTime.of(11,11,11)); //11:11:11

  **  //3.LocalDateTime 至关于LocalDate + LocalTime**
    //当前系统时间(到毫秒)
    System.out.println(LocalDateTime.now()); //2019-12-17T10:48:54.036
    //构造时间
    System.out.println(LocalDateTime.of(2019, Month.DECEMBER,17,12,12,12)); //2019-12-17T12:12:12

    //当前系统时间(根据自定义根式显示)
    System.out.println(
            LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")) //2019-12-17 10:49:49
    );

    **//4.时间戳,精确到纳秒**
    Instant instant = Instant.now();
    System.out.println(instant); //2019-12-17T02:55:10.736Z

    //获取秒数
    long currentSecond = instant.getEpochSecond();
    System.out.println(currentSecond); //1576551247

    //获取毫秒数
    long currentMilli = instant.toEpochMilli(); //1576551247168
    System.out.println(currentMilli);

   ** //5.Duration 表示一个时间段**
    LocalDateTime start = LocalDateTime.of(2018,Month.FEBRUARY,01,00,00,00);    // 2018-02-01T00:00
    LocalDateTime end = LocalDateTime.of(2019,Month.DECEMBER,31,23,59,59);     // 2019-12-31T23:59:59
    Duration duration = Duration.between(start, end);     // 表示从 start 到 end 这段时间
    long days = duration.toDays();
    System.out.println("这段时间的总天数:" + days);//698
    long hours = duration.toHours();
    System.out.println("这段时间的小时数 : " + hours);//16775
    long minutes = duration.toMinutes();
    System.out.println("这段时间的分钟数:" + minutes);//1006559
    long seconds = duration.getSeconds();
    System.out.println("这段时间的秒数:" + seconds);//60393599
    long milliSeconds = duration.toMillis();
    System.out.println("这段时间的毫秒数:" + milliSeconds);//60393599000
    long nanoSeconds = duration.toNanos();
    System.out.println("这段时间的纳秒数:" + nanoSeconds);//60393599000000000
}
相关文章
相关标签/搜索