Skip to content

Commit 1f41576

Browse files
Kate IvanovaUbuntu
authored andcommitted
Fix failing DateTimeUtilTest tests
DEVSIX-6043
1 parent 7eee081 commit 1f41576

File tree

2 files changed

+107
-10
lines changed

2 files changed

+107
-10
lines changed

commons/src/main/java/com/itextpdf/commons/utils/DateTimeUtil.java

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ private DateTimeUtil() {
6666
* Gets the {@link Calendar} as UTC milliseconds from the epoch.
6767
*
6868
* @param calendar the calendar to be converted to millis
69+
*
6970
* @return the date as UTC milliseconds from the epoch
7071
*/
7172
public static double getUtcMillisFromEpoch(Calendar calendar) {
@@ -79,6 +80,7 @@ public static double getUtcMillisFromEpoch(Calendar calendar) {
7980
* Gets the date as {@link Calendar}.
8081
*
8182
* @param date the date to be returned as {@link Calendar}
83+
*
8284
* @return the date as {@link Calendar}
8385
*/
8486
public static Calendar getCalendar(Date date) {
@@ -87,27 +89,72 @@ public static Calendar getCalendar(Date date) {
8789
return calendar;
8890
}
8991

92+
/**
93+
* Gets a default {@link GregorianCalendar}.
94+
*
95+
* @return a default {@link GregorianCalendar} using the current time in the default
96+
* time zone with the default locale
97+
*/
9098
public static Calendar getCurrentTimeCalendar() {
9199
return new GregorianCalendar();
92100
}
93101

102+
103+
/**
104+
* Gets current time consistently.
105+
*
106+
* @return the time at which it was allocated, measured to the nearest millisecond
107+
*/
94108
public static Date getCurrentTimeDate() {
95109
return new Date();
96110
}
97111

112+
/**
113+
* Adds the specified amount of days to the given calendar field.
114+
*
115+
* @param calendar the calendar field where to add
116+
* @param days the amount of days to be added
117+
*
118+
* @return the time at which it was allocated, measured to the nearest millisecond
119+
*/
98120
public static Calendar addDaysToCalendar(Calendar calendar, int days) {
99121
calendar.add(Calendar.DAY_OF_YEAR, days);
100122
return calendar;
101123
}
102124

125+
/**
126+
* Defines if date is in past.
127+
*
128+
* @param date the date to be compared with current date
129+
*
130+
* @return <code>true</code> if given date is in past, <code>false</code> instead
131+
*/
103132
public static boolean isInPast(Date date) {
104133
return date.before(getCurrentTimeDate());
105134
}
106135

136+
/**
137+
* Gets the number of milliseconds since January 1, 1970, 00:00:00 GMT
138+
* represented by specified date.
139+
*
140+
* @param date the specified date to get time
141+
*
142+
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
143+
* represented by the specified date
144+
*/
107145
public static long getRelativeTime(Date date) {
108146
return date.getTime();
109147
}
110148

149+
/**
150+
* Adds the specified amount of days to the given date.
151+
*
152+
* @param date the specified date to add
153+
* @param days the amount of days to be added
154+
*
155+
* @return a {@link Date} object representing the calendar's time value (millisecond
156+
* offset from the Epoch)
157+
*/
111158
public static Date addDaysToDate(Date date, int days) {
112159
Calendar cal = new GregorianCalendar();
113160
cal.setTime(date);
@@ -116,15 +163,24 @@ public static Date addDaysToDate(Date date, int days) {
116163
}
117164

118165
/**
119-
* Parses passing date with default {@code yyyy-MM-dd} pattern.
166+
* Parses passing date with default yyyy-MM-dd pattern.
120167
*
121168
* @param date is date to be parse
169+
*
122170
* @return parse date
123171
*/
124172
public static Date parseWithDefaultPattern(String date) {
125173
return parse(date, DEFAULT_PATTERN);
126174
}
127175

176+
/**
177+
* Parses passing date with specified format.
178+
*
179+
* @param date the date to be parsed
180+
* @param format the format of parsing the date
181+
*
182+
* @return parsed date
183+
*/
128184
public static Date parse(String date, String format) {
129185
try {
130186
return initParserSDF(format).parse(date);
@@ -134,22 +190,52 @@ public static Date parse(String date, String format) {
134190
}
135191

136192
/**
137-
* Format passing date with default {@code yyyy-MM-dd} pattern.
193+
* Format passing date with default yyyy-MM-dd pattern.
194+
*
195+
* @param date the date to be formatted
138196
*
139-
* @param date is date to be format
140-
* @return format date
197+
* @return formatted date
141198
*/
142199
public static String formatWithDefaultPattern(Date date) {
143200
return format(date, DEFAULT_PATTERN);
144201
}
145202

203+
/**
204+
* Format passing date with specified pattern.
205+
*
206+
* @param date date to be formatted
207+
* @param pattern pattern for format
208+
*
209+
* @return formatted date
210+
*/
146211
public static String format(Date date, String pattern) {
147212
return initParserSDF(pattern).format(date);
148213
}
149214

215+
/**
216+
* Gets the offset of time zone from UTC
217+
*
218+
* @return the offset of time zone from UTC
219+
*
220+
* @deprecated Unused and will be removed in the next major release.
221+
* Use {@link DateTimeUtil#getCurrentTimeZoneOffset(Date)} instead.
222+
*/
223+
@Deprecated
150224
public static long getCurrentTimeZoneOffset() {
225+
return getCurrentTimeZoneOffset(getCurrentTimeDate());
226+
}
227+
228+
/**
229+
* Gets the offset of time zone from UTC at the specified date.
230+
*
231+
* @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT
232+
*
233+
* @return the offset of time zone from UTC at the specified date adjusted with the amount
234+
* of daylight saving.
235+
*/
236+
public static long getCurrentTimeZoneOffset(Date date) {
151237
TimeZone tz = TimeZone.getDefault();
152-
return tz.getOffset(getCurrentTimeDate().getTime());
238+
return tz.getOffset(date.getTime());
153239
}
154240

155241
private static DateFormat initParserSDF(String pattern) {

commons/src/test/java/com/itextpdf/commons/utils/DateTimeUtilTest.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ public void isInPastTest() {
5151

5252
@Test
5353
public void parseDateAndGetUtcMillisFromEpochTest() {
54-
Calendar parsedDate = DateTimeUtil.getCalendar(DateTimeUtil.parseWithDefaultPattern("2020-05-05"));
54+
Date date = DateTimeUtil.parseWithDefaultPattern("2020-05-05");
55+
Calendar parsedDate = DateTimeUtil.getCalendar(date);
56+
5557
double millisFromEpochTo2020_05_05 = DateTimeUtil.getUtcMillisFromEpoch(parsedDate);
5658

57-
long offset = DateTimeUtil.getCurrentTimeZoneOffset();
59+
long offset = DateTimeUtil.getCurrentTimeZoneOffset(date);
60+
5861
Assert.assertEquals(1588636800000d - offset, millisFromEpochTo2020_05_05, ZERO_DELTA);
5962
}
6063

6164
@Test
62-
public void compareUtcMillisFromEpochWithNullParamAndCurrentTimeTest() throws InterruptedException {
65+
public void compareUtcMillisFromEpochWithNullParamAndCurrentTimeTest() {
6366
double getUtcMillisFromEpochWithNullParam = DateTimeUtil.getUtcMillisFromEpoch(null);
6467
double millisFromEpochToCurrentTime = DateTimeUtil.getUtcMillisFromEpoch(DateTimeUtil.getCurrentTimeCalendar());
6568

@@ -68,9 +71,17 @@ public void compareUtcMillisFromEpochWithNullParamAndCurrentTimeTest() throws In
6871

6972
@Test
7073
public void parseDateAndGetRelativeTimeTest() {
71-
double relativeTime = DateTimeUtil.getRelativeTime(DateTimeUtil.parseWithDefaultPattern("2020-05-05"));
74+
Date date = DateTimeUtil.parseWithDefaultPattern("2020-05-05");
75+
double relativeTime = DateTimeUtil.getRelativeTime(date);
76+
77+
long offset = DateTimeUtil.getCurrentTimeZoneOffset(date);
7278

73-
long offset = DateTimeUtil.getCurrentTimeZoneOffset();
7479
Assert.assertEquals(1588636800000d - offset, relativeTime, ZERO_DELTA);
7580
}
81+
82+
@Test
83+
public void getCurrentTimeZoneOffsetTest() {
84+
Assert.assertEquals(DateTimeUtil.getCurrentTimeZoneOffset(DateTimeUtil.getCurrentTimeDate()),
85+
DateTimeUtil.getCurrentTimeZoneOffset());
86+
}
7687
}

0 commit comments

Comments
 (0)