forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReformatDate.java
35 lines (33 loc) · 1 KB
/
ReformatDate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class ReformatDate {
public String reformatDate(String date) {
final String[] parts = date.split(" ");
return new StringBuilder(parts[2])
.append('-')
.append(getMonth(parts[1]))
.append('-')
.append(getDay(parts[0]))
.toString();
}
private String getMonth(String month) {
return switch (month) {
case "Jan" -> "01";
case "Feb" -> "02";
case "Mar" -> "03";
case "Apr" -> "04";
case "May" -> "05";
case "Jun" -> "06";
case "Jul" -> "07";
case "Aug" -> "08";
case "Sep" -> "09";
case "Oct" -> "10";
case "Nov" -> "11";
case "Dec" -> "12";
default -> "";
};
}
private String getDay(String string) {
if (string.length() == 3) {
return '0' + string.substring(0, 1);
} return string.substring(0, 2);
}
}