Java8特性之时间API

2021-04-20  本文已影响0人  felixfeijs

JDK8特性之时间API

LocalDate、LocalDateTIme、LocalTIme
方法 描述
now()/* now(Zoneld zone) 静态方法,根据当前时间创建对象/指定时区的对象
of() 静态方法,根据指定日期/时间创建对象
getDayOfMonth()/getDayOfYear() 获取月份天数(1-31)/获取年份天数(1~366)
getDayOfWeek() 获取星期几(返回一个DayOfWeek枚举值)
getMonth() 获取月份,返回一个Month枚举值
getMonthValue()/getYear() 获取鱼粉(1-12)/获取年份
getHour()/getMinute()/getSeconde() 获取当前对象对应的小时、分钟、秒
withDayOfMonth()/withDayOfYear()/withMonty()/withYear() 将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象
plusDays()/plusWeeks()/plusMonths()/plusYears()/plusHours() 向当前对象添加几天、几周、几个月、几年、几小时
minusMonths()/minusWeeks()/minusDays()/minusYears()/minusHours() 从当前对象减去几月、几周、几天、几年、几小时
package com.felixfei.study.test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

import java.util.Date;


/**
 * 为什么会出现新的日期API
 * 1. 可变性:像日期和时间这样的类应该是不可变的(calendar是可变的,修改的是本身的值)
 * 2. 偏移性:Date中的年份是从1900年开始的,而月份都从0开始
 * 3. 格式化:格式化只对Date有用,Calendar则不行
 * 4. 此外,他们也不是线程安全的,不能处理闰秒等
 */
public class LocalDateTimeTest {
    public static void main(String[] args) {
        // 获取2021年3月22号的时间,要进行偏移量计算
        Date date = new Date(2021 - 1900, 3 - 1, 22);
        System.out.println("2021年3月22号时间为:" + date);

        // LocalDate、LocalDateTime、LocalTime
        LocalDate now = LocalDate.now();
        LocalDateTime now1 = LocalDateTime.now();
        LocalTime now2 = LocalTime.now();
        System.out.println("当前日期:" + now);
        System.out.println("当时间:" + now1);
        System.out.println("当前日期时间:" + now2);

        // 获取指定的日期时间,没有偏移量
        LocalDateTime of = LocalDateTime.of(2020, 3, 10, 22, 12, 13);
        System.out.println("指定的日期时:" + of);

        System.out.println("当月的第几天:" + now.getDayOfMonth());
        System.out.println("这周的第几天:" + now.getDayOfWeek());
        System.out.println("第几月:" + now.getMonth());
        System.out.println("月份数值:" + now.getMonthValue());
        System.out.println("今天的第几天:" + now.getDayOfYear());

        // 不可变性,修改的返回是新的值
        LocalDate localDate = now.withDayOfMonth(23);
        System.out.println("修改天数后的值:" + localDate);

        LocalDate localDate1 = now.plusDays(2);
        System.out.println("增加1天后的值:" + localDate1);

        LocalDate localDate2 = now.minusDays(6);
        System.out.println("减6天后的值:" + localDate2);
    }
}

Instant
方法 描述
now() 静态方法,返回默认UTC时区的Instant类的对象
ofEpochMilli(long epochMilli) 静态方法,返回在1970-01-01 00:00:00基础上加上指定毫秒数之后的Instant类的对象
atOffset(ZoneOffset offset) 结合即时的创建一个OffsetDateTime
toEpochMilli() 返回1970-01-01 00:00:00到当前时间的毫秒数,即为时间戳
package com.felixfei.study.test;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

/**
 * Instant:时间线上的一个瞬时点,这可能被用来记录应用程序中的事件时间戳。
 * 类似于java.util.Date类
 */
public class LocalDateTimeTest {
    public static void main(String[] args) {
        Instant now = Instant.now(); // 获取的是本初子午线的时间
        System.out.println("获取的是本初子午线的时间=" + now);

        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.of("+8"));
        System.out.println("获取的东八区的时间=" + offsetDateTime);

        long l = now.toEpochMilli();
        System.out.println("获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数=" + l);

        Instant instant = Instant.ofEpochMilli(l);
        System.out.println("通过给定的毫秒数,获取的Instant实例=" + instant);
    }
}

DateTimeFormatter类
方法 描述
ofPattern(String pattern) 静态方法,返回一个指定字符串格式的DateTImeFormatter
format(TemproalAccessor t) 格式化一个日期、时间,返回字符串
parse(CharSequence text) 将指定格式的字符序列解析为一个日期、时间
package com.felixfei.study.test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;


public class LocalDateTimeTest {
    public static void main(String[] args) {
        DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE;
        String str1 = isoDate.format(LocalDateTime.now());
        System.out.println("格式化后的字符串日期=" + str1);

        TemporalAccessor parse = isoDate.parse(str1);
        System.out.println("字符串解析后的日期=" + parse);

        // FormatStyle.LONG  2021年3月24日 下午08时53分19秒 可使用 ofLocalizedDateTime 方法
        // FormatStyle.MEDIUM  2021-3-24 20:55:10 可使用 ofLocalizedDateTime 方法
        // FormatStyle.SHORT  21-3-24 下午8:55 可使用 ofLocalizedDateTime 方法
        // FormatStyle.FULL  2021年3月24日 星期三 这种要使用 ofLocalizedDate 方法
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format = formatter.format(LocalDateTime.now());
        System.out.println("格式化对应样式后的日期时间字符串=" + format);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format1 = formatter1.format(LocalDateTime.now());
        System.out.println("格式化对应样式后的日期字符串=" + format1);

        TemporalAccessor parse1 = formatter1.parse(format1);
        System.out.println("字符串解析后的日期=" + parse1); // 注意字符串的格式必须和ofPattern中的一致

        System.out.println("获取解析后的年="+parse1.get(ChronoField.YEAR));

    }
}

上一篇 下一篇

猜你喜欢

热点阅读