栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDK8新增时间相关类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDK8新增时间相关类

jdk7: 代码麻烦, 多线程环境下会导致数据安全的问题
jdk8: 简单, 时间日期对象都是不可变的, 解决上面jdk7出现的问题

jdk7之前时间相关类

Date  时间
SimpleDateFormat  格式化时间
Calendar  日历

jdk8新增的时间相关类

Date类:
    ZoneId 时区
    	洲名/城市名  比如: Asia/Shanghai(亚洲/上海)  Asia/Taipei(亚洲/台北)  Asia/Chongqing(亚洲/重庆)
    	国家名/城市名 比如: Australia/Canberra(澳大利亚/堪培拉)  America/New_York(美国/纽约)
    Instant 时间戳 (不带时区, 所以要加8个小时, 因为我们在东八区)
    ZonedDateTime 带时区的时间

日期格式化类:
	DateTimeFormatter 用于时间的格式化和解析

日历类:
    LocalDate 年、月、日
    LocalTime 时、分、秒
    LocalDateTime 年、月、日、时、分、秒

工具类:
    Duration 用于计算时间间隔 (秒, 纳秒)
    Period 用于计算时间间隔 (年, 月, 日)
    ChronoUnit 用于计算时间间隔 (所有单位)
Date类

ZoneId

import java.time.ZoneId;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        Set zoneIds = ZoneId.getAvailableZoneIds(); //获取java中支持的所有时区
        ZoneId zoneId = ZoneId.systemDefault(); //获取系统默认时区
        ZoneId of = ZoneId.of("Asia/Shanghai"); //获取指定的时区
    }
}

Instant

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Test {
    public static void main(String[] args) {
        Instant now = Instant.now(); //获取当前时间的Instant对象 (标准时间)
        //根据 (秒/毫秒/纳秒) 获取Instant对象
        Instant instant1 = Instant.ofEpochMilli(0); //毫秒
        Instant instant2 = Instant.ofEpochSecond(1);  //秒
        Instant instant3 = Instant.ofEpochSecond(1,1000000000); //秒, 纳秒
        //指定时区
        ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        //判断
        Instant instant4 = Instant.ofEpochMilli(0);
        Instant instant5 = Instant.ofEpochMilli(1000);
        //用于时间的判断
        //isBefore: 判断调用者代表的时间是否在参数表示时间的前面
        boolean before = instant4.isBefore(instant5); //true
        //isAfter: 判断调用者代表的时间是否在参数表示时间的后面
        boolean after = instant4.isAfter(instant5); //false
        //减少时间系列的方法
        Instant instant6 = Instant.ofEpochMilli(3000);
        Instant instant7 = instant6.minusSeconds(1); //秒
        Instant instant8 = instant6.minusMillis(1000); //毫秒
        Instant instant9 = instant6.minusNanos(1000000000); //纳秒
        //增加时间系列的方法
        Instant instant10 = instant6.plusSeconds(1); //秒
        Instant instant11 = instant6.plusMillis(1000); //毫秒
        Instant instant12 = instant6.plusNanos(1000000000); //纳秒
    }
}

ZonedDateTime

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Test {
    public static void main(String[] args) {
        ZonedDateTime now1 = ZonedDateTime.now(); //获取当前时间对象 (带时区)
        //获取指定的时间对象 (带时区)
        //年、月、日、时、分、秒、毫秒、时区
        ZonedDateTime time1 = ZonedDateTime
                .of(2022, 7, 28,
                        14, 30, 0, 0,
                        ZoneId.of("Asia/Shanghai"));
        //通过Instant + 时区的方式指定获取时间对象
        Instant instant1 = Instant.ofEpochMilli(0);
        ZoneId zoneId1 = ZoneId.of("Asia/Shanghai");
        ZonedDateTime time2 = ZonedDateTime.ofInstant(instant1, zoneId1);
        //withxxx 修改时间系列的方法
        ZonedDateTime time3 = time2.withYear(2021); //修改年
        //减少时间, 减少年, 也可以有其他的比如月和日
        ZonedDateTime time4 = time3.minusYears(1);
        //增加时间, 增加年, 也可以有其他的比如月和日
        ZonedDateTime time5 = time4.plusYears(1);
    }
}
日期格式化类

DateTimeFormatter

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        //获取时间对象
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));

        //解析/格式化器
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");

        //格式化
        String format = dtf.format(time);
        System.out.println(format);
        
        //年月日 字符串转换为 LocalDate
        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String str1 = "2022-01-01";
        LocalDate localDate = LocalDate.parse(str1, dtf1);
        System.out.println(localDate);
        System.out.println(LocalDate.parse(str1));

        //年月日时分秒 字符串转换为 LocalDateTime
        // Locale.US 的作用是格式化时,会按照当地的习惯来格式化,如中国是 星期日,美国是Sun
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.US);
        String str2 = "2022-01-01 00:00:00";
        LocalDateTime localDateTime = LocalDateTime.parse(str2, dtf2);
        System.out.println(localDateTime);
        System.out.println(localDateTime.format(dtf2));
    }
}
日历类

LocalDate

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;

public class Test {
    public static void main(String[] args) {
        //获取当前时间的日历对象 (包含 年月日)
        LocalDate nowDate = LocalDate.now();
        System.out.println(nowDate);

        //获取指定的时间的日历对象
        LocalDate ldDate = LocalDate.of(2022, 7, 7);
        System.out.println(ldDate);

        //获取年
        int year = ldDate.getYear();

        //获取月
        //方式一:
        Month m = ldDate.getMonth();
        int month = m.getValue();
        //方式二:
        int monthValue = ldDate.getMonthValue();

        //获取日
        int day = ldDate.getDayOfMonth();

        //获取一年的第几天
        int dayOfYear = ldDate.getDayOfYear();

        //获取星期
        DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
        int value = dayOfWeek.getValue();

        //is开头的方法表示判断
        boolean before = ldDate.isBefore(nowDate); //判断ldDate是否在nowDate之前
        boolean after = ldDate.isAfter(nowDate); //判断ldDate是否在nowDate之后

        //with开头的方法表示修改, 只能修改年月日
        LocalDate withLocalDate = ldDate.withYear(2000);

        //minus开头发方法表示减少, 只能减少年月日
        LocalDate minusLocalDate = ldDate.minusYears(1);

        //plus开头发方法表示增加, 只能增加年月日
        LocalDate plusLocalDate = ldDate.plusYears(1);

        //判断今天是否是你的生日
        LocalDate birDate = LocalDate.of(2000,7,28);
        LocalDate nowDate1 = LocalDate.now();

        MonthDay birMd = MonthDay.of(birDate.getMonthValue(),birDate.getDayOfMonth());
        MonthDay nowMd = MonthDay.from(nowDate1);

        System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));
    }
}

LocalTime

import java.time.LocalTime;

public class Test6 {
    public static void main(String[] args) {
        //获取本地时间的日历对象 (包含 时分秒、纳秒)
        LocalTime nowTime = LocalTime.now();
        System.out.println(nowTime);

        int hour = nowTime.getHour(); //获取时
        int minute = nowTime.getMinute(); //获取分
        int second = nowTime.getSecond(); //获取秒
        int nano = nowTime.getNano(); //获取纳秒

        //获取指定的LocalTime对象
        LocalTime localTime1 = LocalTime.of(8, 20); //时分
        LocalTime localTime2 = LocalTime.of(8, 20,30); //时分秒
        LocalTime localTime3 = LocalTime.of(8, 20,30,150); //时分秒、纳秒

        //is系列的方法
        boolean before = nowTime.isBefore(localTime1); //判断nowTime是否在localTime1之前
        boolean after = nowTime.isAfter(localTime1); //判断nowTime是否在localTime1之后

        //with系列的方法, 只能修改时、分、秒、纳秒
        LocalTime withLocalTime = nowTime.withHour(9);

        //minus系列的方法, 减少时、分、秒、纳秒
        LocalTime minusLocalTime = nowTime.minusHours(1);

        //plus系列的方法, 增加时、分、秒、纳秒
        LocalTime plusLocalTime = nowTime.plusHours(1);
    }
}

LocalDateTime

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

public class Test {
    public static void main(String[] args) {
        //获取当前时间的日历对象 (包含 年月日时分秒)
        LocalDateTime nowDateTime = LocalDateTime.now();

        System.out.println("今天是: " + nowDateTime);
        System.out.println(nowDateTime.getYear()); //年
        System.out.println(nowDateTime.getMonth()); //月
        System.out.println(nowDateTime.getDayOfMonth()); //日
        System.out.println(nowDateTime.getHour()); //时
        System.out.println(nowDateTime.getMinute()); //分
        System.out.println(nowDateTime.getSecond()); //秒
        System.out.println(nowDateTime.getNano()); //纳秒
        //日: 当年的第几天
        System.out.println("dayOfYear: " + nowDateTime.getDayOfYear());
        //星期
        System.out.println(nowDateTime.getDayOfWeek());
        System.out.println(nowDateTime.getDayOfWeek().getValue());
        //月份
        System.out.println(nowDateTime.getMonth());
        System.out.println(nowDateTime.getMonth().getValue());

        //LocalDateTime 转换为 LocalDate
        LocalDate ld = nowDateTime.toLocalDate();
        System.out.println(ld);

        //LocalDateTime 转换为 LocalTime
        LocalTime lt = nowDateTime.toLocalTime();
        System.out.println(lt);
    }
}
工具类

Period

import java.time.LocalDate;
import java.time.Period;

public class Test {
    public static void main(String[] args) {
        //获取当前日期
        LocalDate today = LocalDate.now();

        //生日的 年月日
        LocalDate birthDate = LocalDate.of(2000, 1, 1);

        Period period = Period.between(birthDate, today); //第二个参数减第一个参数

        System.out.println("相差的时间间隔对象: " + period); //P22Y6M27D 22年6月27天
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

        System.out.println(period.toTotalMonths()); //间隔的总月数
    }
}

Duration

import java.time.Duration;
import java.time.LocalDateTime;

public class Test {
    public static void main(String[] args) {
        // 获取当前时间对象
        LocalDateTime today = LocalDateTime.now();

        //生日的日期对象
        LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1, 0, 0, 0);

        Duration duration = Duration.between(birthDate, today);//第二个参数减第一个参数
        System.out.println("相差的时间间隔对象: " + duration); 
        //PT197872H14M41.916S   197872小时1414分41.916秒

        System.out.println(duration.toDays()); //两个时间相差的天数
        System.out.println(duration.toHours()); //两个时间相差的小时数
        System.out.println(duration.toMinutes()); //两个时间相差的分钟数
        System.out.println(duration.toMillis()); //两个时间相差的毫秒数
        System.out.println(duration.toNanos()); //两个时间相差的纳秒数
    }
}

ChronoUnit

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class Test10 {
    public static void main(String[] args) {
        //获取当前时间
        LocalDateTime today = LocalDateTime.now();

        //生日时间
        LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1, 0, 0, 0, 0);

        System.out.println("相差的年数: " + ChronoUnit.YEARS.between(birthDate, today)); //第二个参数减第一个参数
        System.out.println("相差的月数: " + ChronoUnit.MONTHS.between(birthDate, today));
        System.out.println("相差的周数: " + ChronoUnit.WEEKS.between(birthDate, today));
        System.out.println("相差的天数: " + ChronoUnit.DAYS.between(birthDate, today));
        System.out.println("相差的时数: " + ChronoUnit.HOURS.between(birthDate, today));
        System.out.println("相差的分数: " + ChronoUnit.MINUTES.between(birthDate, today));
        System.out.println("相差的秒数: " + ChronoUnit.SECONDS.between(birthDate, today));
        System.out.println("相差的毫秒数: " + ChronoUnit.MILLIS.between(birthDate, today));
        System.out.println("相差的微秒数: " + ChronoUnit.MICROS.between(birthDate, today));
        System.out.println("相差的纳秒数: " + ChronoUnit.NANOS.between(birthDate, today));
        System.out.println("相差的半天数: " + ChronoUnit.HALF_DAYS.between(birthDate, today));
        System.out.println("相差的十年数: " + ChronoUnit.DECADES.between(birthDate, today));
        System.out.println("相差的世纪(百年)数: " + ChronoUnit.CENTURIES.between(birthDate, today));
        System.out.println("相差的千年数: " + ChronoUnit.MILLENNIA.between(birthDate, today));
        System.out.println("相差的纪元数: " + ChronoUnit.ERAS.between(birthDate, today));
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032858.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号