Table of Contents
ToggleJava LocalDate Methods
隨著 Java 8 的出現,建立日期的方式重新被定義,相對於 java.util 舊的 API , java.time 中的新日期和時間 API 可以更加便利與輕鬆地使用, LocalDate 是一個不可變、線程安全的物件,只能儲存日期,不能包含時間,介紹常見的 now 、 of 、 get 、 with 等方法, LocalDate Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- localdate
| +- LocalDateMethodsTest.java
單元測試
LocalDate Methods Java 提供目前日期、建立、取得等操作 LocalDate 。
now
LocalDate Methods Java 建立一個 LocalDate ,提供目前日期。
@Test
public void now() {
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
ZoneId zone = ZoneId.of("America/Chicago");
localDate = LocalDate.now(zone);
System.out.println(localDate);
zone = ZoneId.of("UTC-5");
Clock clock = Clock.system(zone);
localDate = LocalDate.now(clock);
System.out.println(localDate);
zone = ZoneId.of("Etc/GMT+5");
localDate = LocalDate.now(zone);
System.out.println(localDate);
}
2023-06-25
2023-06-24
2023-06-24
of
LocalDate Methods Java 建立一個 LocalDate ,建立指定日期。
@Test
public void of() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
System.out.println(localDate);
assertEquals("2023-06-18", localDate.toString());
long epochDay = localDate.toEpochDay();
System.out.println(epochDay);
assertEquals(19526, epochDay);
localDate = LocalDate.of(2023, Month.JUNE, 19);
System.out.println(localDate);
assertEquals("2023-06-19", localDate.toString());
localDate = LocalDate.ofYearDay(2023, 171);
System.out.println(localDate);
assertEquals("2023-06-20", localDate.toString());
localDate = LocalDate.ofEpochDay(19529);
System.out.println(localDate);
assertEquals("2023-06-21", localDate.toString());
}
2023-06-18
19526
2023-06-19
2023-06-20
2023-06-21
get
LocalDate Methods Java 建立一個 LocalDate ,取得指定欄位,如:年、月、日等。
@Test
public void get() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
int year = localDate.get(ChronoField.YEAR);
System.out.println(year);
assertEquals(2023, year);
int month = localDate.get(ChronoField.MONTH_OF_YEAR);
System.out.println(month);
assertEquals(6, month);
int dayOfMonth = localDate.get(ChronoField.DAY_OF_MONTH);
System.out.println(dayOfMonth);
assertEquals(18, dayOfMonth);
int dayOfWeek = localDate.get(ChronoField.DAY_OF_WEEK);
System.out.println(dayOfWeek);
assertEquals(7, dayOfWeek);
int dayOfYear = localDate.get(ChronoField.DAY_OF_YEAR);
System.out.println(dayOfYear);
assertEquals(169, dayOfYear);
}
2023
6
18
7
169
getValue
LocalDate Methods in Java 建立一個 LocalDate ,取得指定欄位,如:年、月、日等。
@Test
public void getValue() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
int year = localDate.getYear();
System.out.println(year);
assertEquals(2023, year);
int month = localDate.getMonthValue();
System.out.println(month);
assertEquals(6, month);
int dayOfMonth = localDate.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(18, dayOfMonth);
int dayOfWeek = localDate.getDayOfWeek().getValue();
System.out.println(dayOfWeek);
assertEquals(7, dayOfWeek);
int dayOfYear = localDate.getDayOfYear();
System.out.println(dayOfYear);
assertEquals(169, dayOfYear);
}
2023
6
18
7
169
with
LocalDate Methods in Java 建立一個 LocalDate ,調整指定欄位,如:年、月、日等,會傳回新物件。
@Test
public void with() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.withYear(2024);
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDate.withMonth(7);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDate.withDayOfMonth(19);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(19, dayOfMonth);
}
2024
7
19
withTemporalAdjuster
LocalDate Methods in Java 建立一個 LocalDate ,使用 TemporalAdjuster 調整指定欄位,如:年、月、日等,會傳回新物件。
@Test
public void withTemporalAdjuster() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.with(ChronoField.DAY_OF_MONTH, 19);
System.out.println(result);
assertEquals("2023-06-19", result.toString());
result = localDate.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
System.out.println(result);
assertEquals("2023-06-24", result.toString());
result = localDate.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(result);
assertEquals("2023-06-30", result.toString());
TemporalAdjuster temporalAdjuster = t -> t.plus(Period.ofDays(14));
result = localDate.with(temporalAdjuster);
System.out.println(result);
assertEquals("2023-07-02", result.toString());
}
2023-06-19
2023-06-24
2023-06-30
2023-07-02
LocalDateMethodsTest.java
LocalDate Methods in Java 新增單元測試,驗證 LocalDate Functions in Java 是否符合預期。
package org.ruoxue.java_147.datetime.localdate;
import static org.junit.Assert.*;
import java.time.Clock;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import org.junit.Test;
public class LocalDateMethodsTest {
@Test
public void now() {
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
ZoneId zone = ZoneId.of("America/Chicago");
localDate = LocalDate.now(zone);
System.out.println(localDate);
zone = ZoneId.of("UTC-5");
Clock clock = Clock.system(zone);
localDate = LocalDate.now(clock);
System.out.println(localDate);
zone = ZoneId.of("Etc/GMT+5");
localDate = LocalDate.now(zone);
System.out.println(localDate);
}
@Test
public void of() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
System.out.println(localDate);
assertEquals("2023-06-18", localDate.toString());
long epochDay = localDate.toEpochDay();
System.out.println(epochDay);
assertEquals(19526, epochDay);
localDate = LocalDate.of(2023, Month.JUNE, 19);
System.out.println(localDate);
assertEquals("2023-06-19", localDate.toString());
localDate = LocalDate.ofYearDay(2023, 171);
System.out.println(localDate);
assertEquals("2023-06-20", localDate.toString());
localDate = LocalDate.ofEpochDay(19529);
System.out.println(localDate);
assertEquals("2023-06-21", localDate.toString());
}
@Test
public void get() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
int year = localDate.get(ChronoField.YEAR);
System.out.println(year);
assertEquals(2023, year);
int month = localDate.get(ChronoField.MONTH_OF_YEAR);
System.out.println(month);
assertEquals(6, month);
int dayOfMonth = localDate.get(ChronoField.DAY_OF_MONTH);
System.out.println(dayOfMonth);
assertEquals(18, dayOfMonth);
int dayOfWeek = localDate.get(ChronoField.DAY_OF_WEEK);
System.out.println(dayOfWeek);
assertEquals(7, dayOfWeek);
int dayOfYear = localDate.get(ChronoField.DAY_OF_YEAR);
System.out.println(dayOfYear);
assertEquals(169, dayOfYear);
}
@Test
public void getValue() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
int year = localDate.getYear();
System.out.println(year);
assertEquals(2023, year);
int month = localDate.getMonthValue();
System.out.println(month);
assertEquals(6, month);
int dayOfMonth = localDate.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(18, dayOfMonth);
int dayOfWeek = localDate.getDayOfWeek().getValue();
System.out.println(dayOfWeek);
assertEquals(7, dayOfWeek);
int dayOfYear = localDate.getDayOfYear();
System.out.println(dayOfYear);
assertEquals(169, dayOfYear);
}
@Test
public void with() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.withYear(2024);
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDate.withMonth(7);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDate.withDayOfMonth(19);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(19, dayOfMonth);
}
@Test
public void withTemporalAdjuster() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.with(ChronoField.DAY_OF_MONTH, 19);
System.out.println(result);
assertEquals("2023-06-19", result.toString());
result = localDate.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
System.out.println(result);
assertEquals("2023-06-24", result.toString());
result = localDate.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(result);
assertEquals("2023-06-30", result.toString());
TemporalAdjuster temporalAdjuster = t -> t.plus(Period.ofDays(14));
result = localDate.with(temporalAdjuster);
System.out.println(result);
assertEquals("2023-07-02", result.toString());
}
}
心得分享
LocalDate Functions in Java 是一個不可變的類別,表示日期,預設格式為 yyyy-mm-dd, 實現 ChronoLocalDate 接口, LocalDate Methods in Java 提供 now 、 of 、 get 、 with 等常見方法的操作範例。