|
| 1 | +package com.baeldung.jodatime; |
| 2 | + |
| 3 | +import org.joda.time.*; |
| 4 | +import org.joda.time.format.DateTimeFormat; |
| 5 | +import org.joda.time.format.DateTimeFormatter; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.util.Date; |
| 9 | +import java.util.TimeZone; |
| 10 | + |
| 11 | +import static org.junit.Assert.*; |
| 12 | + |
| 13 | +public class JodaTimeUnitTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void testDateTimeRepresentation() { |
| 17 | + |
| 18 | + DateTimeZone.setDefault(DateTimeZone.forID("Europe/Bucharest")); |
| 19 | + |
| 20 | + // representing current date and time |
| 21 | + LocalDate currentDate = LocalDate.now(); |
| 22 | + LocalTime currentTime = LocalTime.now(); |
| 23 | + LocalDateTime currentLocalDateTime = LocalDateTime.now(); |
| 24 | + |
| 25 | + LocalDateTime currentDateTimeFromJavaDate = new LocalDateTime(new Date()); |
| 26 | + Date currentJavaDate = currentDateTimeFromJavaDate.toDate(); |
| 27 | + |
| 28 | + // representing custom date and time |
| 29 | + Date oneMinuteAgoDate = new Date(System.currentTimeMillis() - (60 * 1000)); |
| 30 | + Instant oneMinutesAgoInstant = new Instant(oneMinuteAgoDate); |
| 31 | + |
| 32 | + DateTime customDateTimeFromInstant = new DateTime(oneMinutesAgoInstant); |
| 33 | + DateTime customDateTimeFromJavaDate = new DateTime(oneMinuteAgoDate); |
| 34 | + DateTime customDateTimeFromString = new DateTime("2018-05-05T10:11:12.123"); |
| 35 | + DateTime customDateTimeFromParts = new DateTime(2018, 5, 5, 10, 11, 12, 123); |
| 36 | + |
| 37 | + // parsing |
| 38 | + DateTime parsedDateTime = DateTime.parse("2018-05-05T10:11:12.123"); |
| 39 | + assertEquals("2018-05-05T10:11:12.123+03:00", parsedDateTime.toString()); |
| 40 | + |
| 41 | + DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss"); |
| 42 | + DateTime parsedDateTimeUsingFormatter = DateTime.parse("05/05/2018 10:11:12", dateTimeFormatter); |
| 43 | + assertEquals("2018-05-05T10:11:12.000+03:00", parsedDateTimeUsingFormatter.toString()); |
| 44 | + |
| 45 | + // Instant |
| 46 | + Instant instant = new Instant(); |
| 47 | + Instant.now(); |
| 48 | + |
| 49 | + Instant instantFromString = new Instant("2018-05-05T10:11:12"); |
| 50 | + Instant instantFromDate = new Instant(oneMinuteAgoDate); |
| 51 | + Instant instantFromTimestamp = new Instant(System.currentTimeMillis() - (60 * 1000)); |
| 52 | + Instant parsedInstant = Instant.parse("05/05/2018 10:11:12", dateTimeFormatter); |
| 53 | + |
| 54 | + Instant instantNow = Instant.now(); |
| 55 | + Instant oneMinuteAgoInstant = new Instant(oneMinuteAgoDate); |
| 56 | + |
| 57 | + // epochMilli and epochSecond |
| 58 | + long milliesFromEpochTime = System.currentTimeMillis(); |
| 59 | + long secondsFromEpochTime = milliesFromEpochTime / 1000; |
| 60 | + Instant instantFromEpochMilli = Instant.ofEpochMilli(milliesFromEpochTime); |
| 61 | + Instant instantFromEpocSeconds = Instant.ofEpochSecond(secondsFromEpochTime); |
| 62 | + |
| 63 | + // convert Instants |
| 64 | + DateTime dateTimeFromInstant = instant.toDateTime(); |
| 65 | + Date javaDateFromInstant = instant.toDate(); |
| 66 | + |
| 67 | + int year = instant.get(DateTimeFieldType.year()); |
| 68 | + int month = instant.get(DateTimeFieldType.monthOfYear()); |
| 69 | + int day = instant.get(DateTimeFieldType.dayOfMonth()); |
| 70 | + int hour = instant.get(DateTimeFieldType.hourOfDay()); |
| 71 | + |
| 72 | + // Duration, Period, Instant |
| 73 | + long currentTimestamp = System.currentTimeMillis(); |
| 74 | + long oneHourAgo = currentTimestamp - 24*60*1000; |
| 75 | + |
| 76 | + Duration duration = new Duration(oneHourAgo, currentTimestamp); |
| 77 | + Instant.now().plus(duration); |
| 78 | + |
| 79 | + long durationInDays = duration.getStandardDays(); |
| 80 | + long durationInHours = duration.getStandardHours(); |
| 81 | + long durationInMinutes = duration.getStandardMinutes(); |
| 82 | + long durationInSeconds = duration.getStandardSeconds(); |
| 83 | + long durationInMilli = duration.getMillis(); |
| 84 | + |
| 85 | + // converting between classes |
| 86 | + DateTimeUtils.setCurrentMillisFixed(currentTimestamp); |
| 87 | + LocalDateTime currentDateAndTime = LocalDateTime.now(); |
| 88 | + |
| 89 | + assertEquals(currentTimestamp, currentDateAndTime.toDate().getTime()); |
| 90 | + assertEquals(new DateTime(currentTimestamp), currentDateAndTime.toDateTime()); |
| 91 | + assertEquals(new LocalDate(currentTimestamp), currentDateAndTime.toLocalDate()); |
| 92 | + assertEquals(new LocalTime(currentTimestamp), currentDateAndTime.toLocalTime()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testJodaInstant() { |
| 97 | + |
| 98 | + Date oneMinuteAgoDate = new Date(System.currentTimeMillis() - (60 * 1000)); |
| 99 | + |
| 100 | + Instant instantNow = Instant.now(); |
| 101 | + Instant oneMinuteAgoInstant = new Instant(oneMinuteAgoDate); |
| 102 | + |
| 103 | + assertTrue(instantNow.compareTo(oneMinuteAgoInstant) > 0); |
| 104 | + assertTrue(instantNow.isAfter(oneMinuteAgoInstant)); |
| 105 | + assertTrue(oneMinuteAgoInstant.isBefore(instantNow)); |
| 106 | + assertTrue(oneMinuteAgoInstant.isBeforeNow()); |
| 107 | + assertFalse(oneMinuteAgoInstant.isEqual(instantNow)); |
| 108 | + |
| 109 | + LocalDateTime localDateTime = new LocalDateTime("2018-02-01"); |
| 110 | + Period period = new Period().withMonths(1); |
| 111 | + LocalDateTime datePlusPeriod = localDateTime.plus(period); |
| 112 | + |
| 113 | + Instant startInterval1 = new Instant("2018-05-05T09:00:00.000"); |
| 114 | + Instant endInterval1 = new Instant("2018-05-05T11:00:00.000"); |
| 115 | + Interval interval1 = new Interval(startInterval1, endInterval1); |
| 116 | + |
| 117 | + Instant startInterval2 = new Instant("2018-05-05T10:00:00.000"); |
| 118 | + Instant endInterval2 = new Instant("2018-05-05T11:00:00.000"); |
| 119 | + Interval interval2 = new Interval(startInterval2, endInterval2); |
| 120 | + |
| 121 | + Instant startInterval3 = new Instant("2018-05-05T11:00:00.000"); |
| 122 | + Instant endInterval3 = new Instant("2018-05-05T13:00:00.000"); |
| 123 | + Interval interval3 = new Interval(startInterval3, endInterval3); |
| 124 | + |
| 125 | + Interval overlappingInterval = interval1.overlap(interval2); |
| 126 | + Interval notOverlappingInterval = interval1.overlap(interval3); |
| 127 | + |
| 128 | + assertTrue(overlappingInterval.isEqual(new Interval(new Instant("2018-05-05T10:00:00.000"), new Instant("2018-05-05T11:00:00.000")))); |
| 129 | + assertNotNull(overlappingInterval); |
| 130 | + |
| 131 | + interval1.abuts(interval3); |
| 132 | + assertTrue(interval1.abuts(new Interval(new Instant("2018-05-05T11:00:00.000"), new Instant("2018-05-05T13:00:00.000")))); |
| 133 | + |
| 134 | + interval1.gap(interval2); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testDateTimeOperations() { |
| 140 | + |
| 141 | + DateTimeUtils.setCurrentMillisFixed(1529612783288L); |
| 142 | + DateTimeZone.setDefault(DateTimeZone.UTC); |
| 143 | + |
| 144 | + LocalDateTime currentLocalDateTime = LocalDateTime.now(); |
| 145 | + assertEquals("2018-06-21T20:26:23.288", currentLocalDateTime.toString()); |
| 146 | + |
| 147 | + LocalDateTime nextDayDateTime = currentLocalDateTime.plusDays(1); |
| 148 | + assertEquals("2018-06-22T20:26:23.288", nextDayDateTime.toString()); |
| 149 | + |
| 150 | + Period oneMonth = new Period().withMonths(1); |
| 151 | + LocalDateTime nextMonthDateTime = currentLocalDateTime.plus(oneMonth); |
| 152 | + assertEquals("2018-07-21T20:26:23.288", nextMonthDateTime.toString()); |
| 153 | + |
| 154 | + LocalDateTime previousDayLocalDateTime = currentLocalDateTime.minusDays(1); |
| 155 | + assertEquals("2018-06-20T20:26:23.288", previousDayLocalDateTime.toString()); |
| 156 | + |
| 157 | + LocalDateTime currentDateAtHour10 = currentLocalDateTime |
| 158 | + .withHourOfDay(0) |
| 159 | + .withMinuteOfHour(0) |
| 160 | + .withSecondOfMinute(0) |
| 161 | + .withMillisOfSecond(0); |
| 162 | + assertEquals("2018-06-21T00:00:00.000", currentDateAtHour10.toString()); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void testTimezones() { |
| 167 | + |
| 168 | + System.getProperty("user.timezone"); |
| 169 | + DateTimeZone.getAvailableIDs(); |
| 170 | + // DateTimeZone.setDefault(DateTimeZone.forID("Europe/Bucharest")); |
| 171 | + |
| 172 | + DateTimeUtils.setCurrentMillisFixed(1529612783288L); |
| 173 | + |
| 174 | + DateTime dateTimeInChicago = new DateTime(DateTimeZone.forID("America/Chicago")); |
| 175 | + assertEquals("2018-06-21T15:26:23.288-05:00", dateTimeInChicago.toString()); |
| 176 | + |
| 177 | + DateTime dateTimeInBucharest = new DateTime(DateTimeZone.forID("Europe/Bucharest")); |
| 178 | + assertEquals("2018-06-21T23:26:23.288+03:00", dateTimeInBucharest.toString()); |
| 179 | + |
| 180 | + LocalDateTime localDateTimeInChicago = new LocalDateTime(DateTimeZone.forID("America/Chicago")); |
| 181 | + assertEquals("2018-06-21T15:26:23.288", localDateTimeInChicago.toString()); |
| 182 | + |
| 183 | + DateTime convertedDateTime = localDateTimeInChicago.toDateTime(DateTimeZone.forID("Europe/Bucharest")); |
| 184 | + assertEquals("2018-06-21T15:26:23.288+03:00", convertedDateTime.toString()); |
| 185 | + |
| 186 | + Date convertedDate = localDateTimeInChicago.toDate(TimeZone.getTimeZone("Europe/Bucharest")); |
| 187 | + assertEquals("Thu Jun 21 15:26:23 EEST 2018", convertedDate.toString()); |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments