Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 658b48f

Browse files
authored
Add JWT DateUtil functions (#25)
1 parent 4117a6e commit 658b48f

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed
Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,93 @@
11
package com.genexus.JWT.utils;
22

33
import java.time.LocalDateTime;
4+
import java.time.LocalTime;
45
import java.time.format.DateTimeFormatter;
6+
import java.time.format.DateTimeParseException;
7+
import java.time.temporal.ChronoField;
8+
import java.util.Calendar;
59

610
import com.genexus.commons.DateUtilObject;
711

8-
public final class DateUtil extends DateUtilObject{
12+
public final class DateUtil extends DateUtilObject {
913

1014
/******** EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
1115
public String getCurrentDate() {
1216
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
1317
LocalDateTime now = LocalDateTime.now();
1418
return dtf.format(now);
1519
}
16-
20+
1721
public String currentPlusSeconds(long seconds) {
1822
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
1923
LocalDateTime now = LocalDateTime.now();
2024
LocalDateTime aux = now.plusSeconds(seconds);
2125
return dtf.format(aux);
2226
}
23-
27+
2428
public String currentMinusSeconds(long seconds) {
2529
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
2630
LocalDateTime now = LocalDateTime.now();
2731
LocalDateTime aux = now.minusSeconds(seconds);
2832
return dtf.format(aux);
2933
}
34+
35+
public String currentPlusMinutes(long minutes) {
36+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
37+
LocalDateTime now = LocalDateTime.now();
38+
LocalDateTime aux = now.plusMinutes(minutes);
39+
return dtf.format(aux);
40+
}
41+
42+
public String currentPlusHours(long hours) {
43+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
44+
LocalDateTime now = LocalDateTime.now();
45+
LocalDateTime aux = now.plusHours(hours);
46+
return dtf.format(aux);
47+
}
48+
49+
public String currentPlusDays(long days) {
50+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
51+
LocalDateTime now = LocalDateTime.now();
52+
LocalDateTime aux = now.plusDays(days);
53+
return dtf.format(aux);
54+
}
55+
56+
public String currentPlusMonths(int months) {
57+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
58+
LocalDateTime now = LocalDateTime.now();
59+
LocalDateTime aux = now.plusMonths(months);
60+
return dtf.format(aux);
61+
}
62+
63+
public String lastDayOfCurrentMonth(String time) {
64+
LocalTime localTime;
65+
try {
66+
localTime = LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm:ss"));
67+
} catch (DateTimeParseException e) {
68+
this.error.setError("DU001", "Wrong format in input parameter");
69+
return "";
70+
}
71+
72+
int hour = localTime.get(ChronoField.CLOCK_HOUR_OF_DAY);
73+
int minute = localTime.get(ChronoField.MINUTE_OF_HOUR);
74+
int second = localTime.get(ChronoField.SECOND_OF_MINUTE);
75+
Calendar calendar = Calendar.getInstance();
76+
int day = calendar.getActualMaximum(Calendar.DATE);
77+
LocalDateTime now = LocalDateTime.now();
78+
int year = now.getYear();
79+
int month = now.getMonthValue();
80+
String result;
81+
try {
82+
result = String.format("%d/%02d/%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
83+
} catch (java.util.IllegalFormatException e) {
84+
this.error.setError("DU002", "Could not generate correct date");
85+
return "";
86+
}
87+
return result;
88+
89+
}
90+
3091
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
3192

3293
}

0 commit comments

Comments
 (0)