自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

探索LocalDateTime的魔法:高效日期時(shí)間處理的秘訣與技巧

開(kāi)發(fā) 前端
LocalDateTime獲取時(shí)間以及計(jì)算都非常方便,而且是線程安全的,建議使用LocalDateTime。

哈嘍,大家好,我是了不起。

LocalDateTime、LocalDate、LocalTime 是Java8全新的日期框架,較之前的util.Date以及Calander使用起來(lái)更加的方便直觀,下面介紹幾種常見(jiàn)的日期對(duì)象用法。

前言

  • LocalDateTime:日期加時(shí)間的日期對(duì)象,包含年月日時(shí)分秒
  • LocalDate:日期類(lèi),包含年月日
  • LocalTime:時(shí)間類(lèi),包含時(shí)分秒

LocalDateTime

JDK1.8版本中新引入的API,加強(qiáng)了對(duì)時(shí)間的管理,有很多特別好用的時(shí)間運(yùn)算方法,而且是線程安全的。

獲取當(dāng)前時(shí)間

@Test
public void test() {
    LocalDate localDate = LocalDate.now();
    LocalTime localTime = LocalTime.now();
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println("localDate:"+localDate);//2023-02-22
    System.out.println("localTime:"+localTime);//17:25:36.590
    System.out.println("localDateTime:"+localDateTime);//2023-02-22T17:25:36.590
}

輸出結(jié)果:

圖片圖片

可以看到不用做格式轉(zhuǎn)換就可以得到可讀性很高的日期格式。注意:ISO 8601規(guī)定的日期和時(shí)間分隔符是T。標(biāo)準(zhǔn)格式如下:

  • 日期:yyyy-MM-dd
  • 時(shí)間:HH:mm:ss
  • 帶毫秒的時(shí)間:HH:mm:ss.SSS
  • 日期和時(shí)間:yyyy-MM-dd'T'HH:mm:ss
  • 帶毫秒的日期和時(shí)間:yyyy-MM-dd'T'HH:mm:ss.SSS

獲取當(dāng)前時(shí)間的年月日時(shí)分秒

@Test
public void test() {
    LocalDateTime localDateTime = LocalDateTime.now(); // 獲取當(dāng)前時(shí)間
    int year = localDateTime.getYear(); // 獲取年份 2023
    int month = localDateTime.getMonthValue(); // 獲取月份 2
    int day = localDateTime.getDayOfMonth(); // 獲取月中的天數(shù) 22
    int hour = localDateTime.getHour(); // 獲取當(dāng)前的小時(shí) 17
    int minute = localDateTime.getMinute(); // 獲取當(dāng)前分鐘 33
    int second = localDateTime.getSecond(); // 獲取當(dāng)前秒數(shù) 22
    System.out.println(year);
    System.out.println(month);
    System.out.println(day);
    System.out.println(hour);
    System.out.println(minute);
    System.out.println(second);
}

輸出結(jié)果:

圖片圖片

給LocalDateTime賦值

在static目錄中新建kaptcha.html頁(yè)面,代碼如下:

public void test() {
    LocalDateTime of = LocalDateTime.of(2023,2,22,22,22,22);
    System.out.println(of); // 輸出2023-02-22T22:22:22
}

時(shí)間與字符串相互轉(zhuǎn)換

@Test
public void test() {
    // 將字符串轉(zhuǎn)換為指定格式的時(shí)間(格式要和給定的格式一致,不然會(huì)報(bào)錯(cuò))
    LocalDateTime parse = LocalDateTime.parse("2023-02-22 22:22:22", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    LocalDateTime parse1 = LocalDateTime.parse("2023 02 22 22:22:22", DateTimeFormatter.ofPattern("yyyy MM dd HH:mm:ss"));
    LocalDateTime parse2 = LocalDateTime.parse("2023.02.22 22:22:22", DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"));
    System.out.println(parse); // 輸出2023-02-22T22:22:22
    System.out.println(parse1); // 輸出2023-02-22T22:22:22
    System.out.println(parse2); // 輸出2023-02-22T22:22:22
    // 時(shí)間轉(zhuǎn)字符串
    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter of = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String dateTime = now.format(of);
    System.out.println(dateTime); // 輸出 2023-02-22 17:56:18
}

輸出結(jié)果:

圖片圖片

時(shí)間運(yùn)算:加上對(duì)應(yīng)時(shí)間

LocalDateTime提供了對(duì)日期和時(shí)間進(jìn)行加減的非常簡(jiǎn)單的鏈?zhǔn)秸{(diào)用,讓時(shí)間運(yùn)算變得非常簡(jiǎn)單:

@Test
public void test() {
    LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間2023-02-22T18:00:19.352
    LocalDateTime plusYears= now.plusYears(2); // 在當(dāng)前時(shí)間加上2年2025-02-22T18:00:19.352
    LocalDateTime plusMnotallow= now.plusMonths(2);// 在當(dāng)前時(shí)間商加上2月2023-04-22T18:00:19.352
    LocalDateTime plusDays= now.plusDays(2); // 在當(dāng)前時(shí)間加上2天2023-02-24T18:00:19.352
    LocalDateTime plusHours= now.plusHours(2); // 在當(dāng)前時(shí)間加上2個(gè)小時(shí)2023-02-22T20:00:19.352
    LocalDateTime plusMinutes= now.plusMinutes(30); // 在當(dāng)前時(shí)間加上30分鐘2023-02-22T18:30:19.352
    LocalDateTime plusSeconds = now.plusSeconds(30); // 在當(dāng)前時(shí)間加上30秒2023-02-22T18:00:49.352
    System.out.println(now);
    System.out.println(plusYears);
    System.out.println(plusMonths);
    System.out.println(plusDays);
    System.out.println(plusHours);
    System.out.println(plusMinutes);
    System.out.println(plusSeconds);
}

輸出結(jié)果:

圖片圖片

時(shí)間運(yùn)算:減去對(duì)應(yīng)時(shí)間

@Test
public void test() {
    LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間
    LocalDateTime minusYears = now.minusYears(2); // 在當(dāng)前時(shí)間減上2年
    LocalDateTime minusMonths = now.minusMonths(2);// 在當(dāng)前時(shí)間商減上2月
    LocalDateTime minusDays = now.minusDays(2); // 在當(dāng)前時(shí)間減上2天
    LocalDateTime minusHours = now.minusHours(2); // 在當(dāng)前時(shí)間減上2個(gè)小時(shí)
    LocalDateTime minusMinutes = now.minusMinutes(30); // 在當(dāng)前時(shí)間減上30分鐘
    LocalDateTime minusSeconds = now.minusSeconds(30); // 在當(dāng)前時(shí)間減上30秒
    System.out.println("now:" + now);
    System.out.println("minusYears:" + minusYears);
    System.out.println("minusMonths:" + minusMonths);
    System.out.println("minusDays:" + minusDays);
    System.out.println("minusHours:" + minusHours);
    System.out.println("minusMinutes:" + minusMinutes);
    System.out.println("minusSeconds:" + minusSeconds);
}

輸出結(jié)果:

圖片圖片

兩個(gè)時(shí)間比較

@Test
public void test() {
    LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間
    LocalDateTime now1 = now.plusYears(5); // 在當(dāng)前時(shí)間加上5年
    //  給LocalDateTime 賦值
    LocalDateTime of = LocalDateTime.of(2023,2,2,22,22,22);
    LocalDateTime of1 = LocalDateTime.of(2023,8,5,1,1,1);
    //兩個(gè)時(shí)間作比較,第一個(gè)時(shí)間減去第二個(gè)時(shí)間(如果年份相同,比較月份,月份相同比較天數(shù),以此類(lèi)推)
    int compareTo = now1.compareTo(now);
    int compareTo1 = now.compareTo(now1);
    int compareTo2 = now.compareTo(of);
    int compareTo3 = now.compareTo(of1);
    System.out.println(now);   // 輸出 2023-02-22T20:19:44.112v
    System.out.println(now1); // 輸出 2028-02-22T20:19:44.112
    System.out.println(of); // 輸出 2023-02-02T22:22:22
    System.out.println(of1); // 輸出 2023-08-05T01:01:01
    System.out.println(compareTo); // 輸出 5
    System.out.println(compareTo1); // 輸出 -5
    System.out.println(compareTo2); // 輸出 20
    System.out.println(compareTo3); // 輸出 -6
}

輸出結(jié)果:

圖片圖片

利用Duration計(jì)算時(shí)間差

注意沒(méi)有計(jì)算相差的年和秒值,對(duì)于要計(jì)算相差的秒數(shù),可以利用計(jì)算毫秒來(lái)進(jìn)行轉(zhuǎn)換

@Test
public void test() {
    LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間
    //  給LocalDateTime 賦值
    LocalDateTime of = LocalDateTime.of(2022,2,22,2,2,2);
    Duration duration = Duration.between(of,now); // 后面減去前面
    long toDays = Duration.between(of,now).toDays(); //相差的天數(shù)
    long toHours = Duration.between(of,now).toHours();//相差的小時(shí)數(shù)
    long toMinutes = Duration.between(of,now).toMinutes();//相差的分鐘數(shù)
    long toMillis = Duration.between(of,now).toMillis();//相差毫秒數(shù)
    long toNanos = Duration.between(of,now).toNanos();//相差的納秒數(shù)
    System.out.println("toDays:"+ toDays); // 輸出 toDays:365
    System.out.println("toHours:"+ toHours); // 輸出 toHours:8778
    System.out.println("toMinutes:"+ toMinutes); // 輸出 toMinutes:526732
    System.out.println("toMillis:"+ toMillis); // 輸出 toMillis:31603973840
    System.out.println("toNanos:"+ toNanos); // 輸出 toNanos:31603973840000000
}

輸出結(jié)果:

圖片圖片

改變當(dāng)前時(shí)間的年月日時(shí)分秒

@Test
public void test() {
    LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間
    LocalDateTime withYear = now.withYear(2060); // 改變當(dāng)前年份(變成2060年)
    LocalDateTime withMonth = now.withMonth(12); // 改變當(dāng)前月份(變成12月份)
    LocalDateTime withDayOfMonth = now.withDayOfMonth(28); //改變當(dāng)前天數(shù)(變成28日)
    LocalDateTime withHour = now.withHour(23); // 改變當(dāng)前小時(shí)數(shù)(變成23時(shí))
    LocalDateTime withMinute = now.withMinute(30); // 改變當(dāng)前分鐘(變成30分鐘)
    LocalDateTime withSecond = now.withSecond(23); // 改變當(dāng)前小時(shí)數(shù)(變成23時(shí))
    LocalDateTime withDayOfYear = now.withDayOfYear(60); // 從一月一號(hào)開(kāi)始加上60天
    System.out.println(now);
    System.out.println("withYear:"+ withYear);
    System.out.println("withMonth:"+ withMonth);
    System.out.println("withDayOfMonth:"+ withDayOfMonth);
    System.out.println("withHour:"+ withHour);
    System.out.println("withMinute:"+ withMinute);
    System.out.println("withSecond:"+ withSecond);
    System.out.println("withDayOfYear:"+ withDayOfYear);
}

輸出結(jié)果:

圖片圖片

自定義輸出的格式

@Test
public void test() {
    // 自定義格式化:
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
    DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
    System.out.println("自定義格式y(tǒng)yyy/MM/dd HH:mm:ss :"+dtf.format(LocalDateTime.now()));
    System.out.println("自定義格式y(tǒng)yyy.MM.dd HH:mm:ss :"+dtf1.format(LocalDateTime.now()));

    // 用自定義格式解析:
    LocalDateTime dt2 = LocalDateTime.parse("2020/10/20 15:16:17", dtf);
    System.out.println("格式解析:"+dt2);
}

輸出結(jié)果:

圖片圖片

LocalDateTime的with()方法

@Test
public void test() {
    // 本月第一天0:00時(shí)刻:
    LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();
    // 本月最后1天:
    LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
    // 下月第1天:
    LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
    // 本月第1個(gè)周一:
    LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
}

兩個(gè)日期前后的比較與判斷

@Test
public void test() {
    //判斷兩個(gè)時(shí)間點(diǎn)的前后
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime target = LocalDateTime.of(2022, 2, 22, 22, 22, 22);
    boolean isBefore = now.isBefore(target);
    System.out.println("now:"+now);
    System.out.println("target:"+target);
    System.out.println("isBefore:"+isBefore);
    System.out.println(LocalDate.now().isBefore(LocalDate.of(2022, 2, 22)));
    System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));
}

輸出結(jié)果:

圖片圖片

結(jié)束語(yǔ)

LocalDateTime獲取時(shí)間以及計(jì)算都非常方便,而且是線程安全的,建議使用LocalDateTime。

責(zé)任編輯:武曉燕 來(lái)源: Java技術(shù)指北
相關(guān)推薦

2010-07-19 15:37:48

Perl日期時(shí)間

2024-08-12 08:36:28

2024-01-11 18:04:53

SQL數(shù)據(jù)庫(kù)

2023-11-30 16:05:17

2023-12-08 07:52:51

Spring項(xiàng)目開(kāi)發(fā)

2009-07-27 13:46:48

ASP.NET編程日期與時(shí)間的處理

2024-04-08 07:17:21

Date日期處理類(lèi)型

2024-06-24 13:35:48

2024-06-04 07:46:05

2024-06-19 10:08:42

Python編程while循環(huán)

2024-07-26 00:00:05

JavaScript單行技巧

2025-04-28 02:00:00

CPU數(shù)據(jù)序列化

2018-03-21 10:24:25

2015-08-17 10:13:35

ios習(xí)慣高效

2017-07-27 15:50:19

Java時(shí)間日期

2010-06-23 13:24:00

CSSCSS選擇器

2025-02-05 12:28:44

2023-11-07 08:33:08

2024-05-08 08:57:25

2024-08-08 16:34:16

C++11編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)