File tree Expand file tree Collapse file tree
java-dates/src/test/java/com/baeldung/date Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88import java .time .LocalDate ;
99import java .time .LocalDateTime ;
1010import java .time .Period ;
11+ import java .time .temporal .ChronoUnit ;
1112import java .util .Date ;
1213import java .util .Locale ;
1314import java .util .TimeZone ;
@@ -51,6 +52,16 @@ public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
5152 assertEquals (diff , 6 );
5253 }
5354
55+ @ Test
56+ public void givenTwoDateTimesInJava8_whenDifferentiatingInSeconds_thenWeGetTen () {
57+ LocalDateTime now = LocalDateTime .now ();
58+ LocalDateTime tenSecondsLater = now .plusSeconds (10 );
59+
60+ long diff = ChronoUnit .SECONDS .between (now , tenSecondsLater );
61+
62+ assertEquals (diff , 10 );
63+ }
64+
5465 @ Test
5566 public void givenTwoZonedDateTimesInJava8_whenDifferentiating_thenWeGetSix () {
5667 LocalDateTime ldt = LocalDateTime .now ();
@@ -60,6 +71,16 @@ public void givenTwoZonedDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
6071 assertEquals (diff , 6 );
6172 }
6273
74+ @ Test
75+ public void givenTwoDateTimesInJava8_whenDifferentiatingInSecondsUsingUntil_thenWeGetTen () {
76+ LocalDateTime now = LocalDateTime .now ();
77+ LocalDateTime tenSecondsLater = now .plusSeconds (10 );
78+
79+ long diff = now .until (tenSecondsLater , ChronoUnit .SECONDS );
80+
81+ assertEquals (diff , 10 );
82+ }
83+
6384 @ Test
6485 public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix () {
6586 org .joda .time .LocalDate now = org .joda .time .LocalDate .now ();
You can’t perform that action at this time.
0 commit comments