GeezerVison is showing github.com — BAEL-7109 - added code to demostrate how to get max date in java (#14… · FEDENG01/tutorials@149cf39
File tree
Expand file treeCollapse file tree
Open diff view settings
core-java-modules/core-java-8-datetime-2/src main/java/com/baeldung/maxdate test/java/com/baeldung/maxdate
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package com.baeldung.maxdate; |
| 2 | + |
| 3 | +import java.util.Date; |
| 4 | + |
| 5 | +public class DateComparison { |
| 6 | + public int compareTodayWithMaxDate() { |
| 7 | + Date today = new Date(); |
| 8 | + Date maxDate = new Date(Long.MAX_VALUE); |
| 9 | + |
| 10 | + int comparisonResult = today.compareTo(maxDate); |
| 11 | + return comparisonResult; |
| 12 | + } |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + DateComparison comparator = new DateComparison(); |
| 16 | + System.out.println(comparator.compareTodayWithMaxDate()); |
| 17 | + } |
| 18 | +} |
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package com.baeldung.maxdate; |
| 2 | + |
| 3 | +import java.util.Date; |
| 4 | +import java.text.SimpleDateFormat; |
| 5 | + |
| 6 | +public class MaxDateDisplay { |
| 7 | + public String getMaxDateValue() { |
| 8 | + Date maxDate = new Date(Long.MAX_VALUE); |
| 9 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 10 | + return "The maximum date value in Java is: " + sdf.format(maxDate); |
| 11 | + } |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + MaxDateDisplay display = new MaxDateDisplay(); |
| 15 | + System.out.println(display.getMaxDateValue()); |
| 16 | + } |
| 17 | +} |
| 18 | + |
Collapse file
Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package com.baeldung.maxdate; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +class DateComparisonUnitTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + void whenCompareTodayWithMaxDate_thenCorrectResult() { |
| 11 | + DateComparison comparator = new DateComparison(); |
| 12 | + int result = comparator.compareTodayWithMaxDate(); |
| 13 | + |
| 14 | + assertTrue(result < 0); |
| 15 | + } |
| 16 | +} |
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package com.baeldung.maxdate; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +class MaxDateDisplayUnitTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + void whenGetMaxDate_thenCorrectResult() { |
| 11 | + MaxDateDisplay display = new MaxDateDisplay(); |
| 12 | + String result = display.getMaxDateValue(); |
| 13 | + assertEquals( |
| 14 | + "The maximum date value in Java is: 292278994-08-17 07:12:55.807", |
| 15 | + result |
| 16 | + ); |
| 17 | + } |
| 18 | +} |