|
| 1 | +package com.baeldung.dateapi; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.text.ParseException; |
| 6 | +import java.time.Duration; |
| 7 | +import java.time.LocalDate; |
| 8 | +import java.time.LocalDateTime; |
| 9 | +import java.time.LocalTime; |
| 10 | +import java.time.Month; |
| 11 | +import java.time.YearMonth; |
| 12 | +import java.time.format.DateTimeFormatter; |
| 13 | +import java.time.temporal.ChronoUnit; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +public class JavaUtilTimeTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void currentTime() { |
| 21 | + final LocalDate now = LocalDate.now(); |
| 22 | + |
| 23 | + System.out.println(now); |
| 24 | + // there is not much to test here |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void specificTime() { |
| 29 | + LocalDate birthDay = LocalDate.of(1990, Month.DECEMBER, 15); |
| 30 | + |
| 31 | + System.out.println(birthDay); |
| 32 | + // there is not much to test here |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void extractMonth() { |
| 37 | + Month month = LocalDate.of(1990, Month.DECEMBER, 15).getMonth(); |
| 38 | + |
| 39 | + assertThat(month).isEqualTo(Month.DECEMBER); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void subtractTime() { |
| 44 | + LocalDateTime fiveHoursBefore = LocalDateTime.of(1990, Month.DECEMBER, 15, 15, 0).minusHours(5); |
| 45 | + |
| 46 | + assertThat(fiveHoursBefore.getHour()).isEqualTo(10); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void alterField() { |
| 51 | + LocalDateTime inJune = LocalDateTime.of(1990, Month.DECEMBER, 15, 15, 0).with(Month.JUNE); |
| 52 | + |
| 53 | + assertThat(inJune.getMonth()).isEqualTo(Month.JUNE); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void truncate() { |
| 58 | + LocalTime truncated = LocalTime.of(15, 12, 34).truncatedTo(ChronoUnit.HOURS); |
| 59 | + |
| 60 | + assertThat(truncated).isEqualTo(LocalTime.of(15, 0, 0)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void getTimeSpan() { |
| 65 | + LocalDateTime now = LocalDateTime.now(); |
| 66 | + LocalDateTime hourLater = now.plusHours(1); |
| 67 | + Duration span = Duration.between(now, hourLater); |
| 68 | + |
| 69 | + assertThat(span).isEqualTo(Duration.ofHours(1)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void formatAndParse() throws ParseException { |
| 74 | + LocalDate someDate = LocalDate.of(2016, 12, 7); |
| 75 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| 76 | + String formattedDate = someDate.format(formatter); |
| 77 | + LocalDate parsedDate = LocalDate.parse(formattedDate, formatter); |
| 78 | + |
| 79 | + assertThat(formattedDate).isEqualTo("2016-12-07"); |
| 80 | + assertThat(parsedDate).isEqualTo(someDate); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void daysInMonth() { |
| 85 | + int daysInMonth = YearMonth.of(1990, 2).lengthOfMonth(); |
| 86 | + |
| 87 | + assertThat(daysInMonth).isEqualTo(28); |
| 88 | + } |
| 89 | +} |
0 commit comments