java8时间api

1.java8中关于时间,常用的类有LocalDate,LocalTime,LocalDateTime,DateTimeFormater,Instant
2.基本使用

package com.yl.pdfdemo.day08.p1;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;

/**
 * @Author wfj
 * @Date 2021/5/21
 * @Description jdk8.0时间api
 * @Version 1.0
 */

public class Jdk8DateTime {

    /**
     * LocalDate,LocalTime,LocalDateTime的使用
     */
    @Test
    public void test1() {
        //now():获取当前的日期,时间,日期+时间,LocalDateTime的使用率高
        //LocalDateTime类似于Calendar
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);//2021-06-09
        System.out.println(localTime);//10:08:02.179
        System.out.println(localDateTime);//2021-06-09T10:08:02.179

        //of():设置指定的年月日时分秒,没有偏移量
        LocalDateTime time = LocalDateTime.of(2020, 11, 11, 13, 11, 11);
        System.out.println(time);

        //getxxx()
        System.out.println(localDateTime.getYear());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getMinute());
        System.out.println(localDateTime.getSecond());

        //设置属性值,体现了不可变性,返回了一个新对象
        //withxxx()
        LocalDate localDate1 = localDate.withDayOfMonth(23);
        System.out.println(localDate);
        System.out.println(localDate1);

        LocalDateTime localDateTime1 = localDateTime.withHour(5);
        System.out.println(localDateTime);
        System.out.println(localDateTime1);

        //增加或者减少
        LocalDateTime localDateTime2 = localDateTime.plusMonths(3);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);

        LocalDateTime localDateTime3 = localDateTime.minusDays(8);
        System.out.println(localDate);
        System.out.println(localDateTime3);

    }

    /**
     * Instant的使用,Instant:时间上的一个瞬时点
     * Instant类似于java.util.Date
     */
    @Test
    public void test2() {
        //now():获取本初子午线的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);

        //添加偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        //获取自1970年1月1日0时0分0秒开始的毫秒数
        long milli = instant.toEpochMilli();
        System.out.println(milli);

        //通过指定的毫秒数获取Instant对象
        Instant instant1 = Instant.ofEpochMilli(1621583858630L);
        System.out.println(instant1);
    }

    /**
     * DateTimeFormatter:格式化,解析日期
     * 类似于simpleDateFormat
     *
     */
    @Test
    public void test3() {
        //方式一:预定义的标准格式 ,如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期 -》 字符串
        LocalDateTime dateTime = LocalDateTime.now();
        String s1 = formatter.format(dateTime);
        System.out.println(dateTime);
        System.out.println(s1);

        //解析:字符串 -》日期
        TemporalAccessor parse = formatter.parse("2021-06-09T09:49:12.414");
        System.out.println(parse);

        //方式二:本地化相关的格式,如ofLocalizedDateTime(),适用于localDateTime
        //FormatStyle.LONG,FormatStyle.MEDIUM,FormatStyle.SHORT
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        String s2 = formatter1.format(dateTime);
        System.out.println(s2);//2021年6月9日 上午09时56分05秒
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        String s3 = formatter2.format(dateTime);
        System.out.println(s3);//2021-6-9 9:57:23
        DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String s4 = formatter3.format(dateTime);
        System.out.println(s4);//21-6-9 上午9:57

        //本地化相关格式,如ofLocalizedDate(),适用于localDate
        //FormatStyle.FULL,FormatStyle.LONG,FormatStyle.MEDIUM,FormatStyle.SHORT
        DateTimeFormatter formatter4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String s5 = formatter4.format(LocalDate.now());
        System.out.println(s5);//2021年6月9日 星期三
        DateTimeFormatter formatter5 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        String s6 = formatter5.format(LocalDate.now());
        System.out.println(s6);//2021年6月9日
        DateTimeFormatter formatter6 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        String s7 = formatter6.format(LocalDate.now());
        System.out.println(s7);//2021-6-9
        DateTimeFormatter formatter7 = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
        String s8 = formatter7.format(LocalDate.now());
        System.out.println(s8);//21-6-9

        //重点 方式三:自定义的格式,如ofPattern("yyyy-MM-dd hh:mm:ss E")
        DateTimeFormatter formatter8 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String s9 = formatter8.format(LocalDateTime.now());
        System.out.println(s9);//2021-06-09 10:11:27
        TemporalAccessor temporalAccessor = formatter8.parse("2051-06-09 10:11:27");
        System.out.println(temporalAccessor);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值