-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReformatDate.java
More file actions
40 lines (33 loc) · 1.17 KB
/
Copy pathReformatDate.java
File metadata and controls
40 lines (33 loc) · 1.17 KB
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
36
37
38
39
package LeetcodePractice;
import java.util.*;
public class ReformatDate {
public static String reformatDate(String date) {
Map<String, String> monthMap = new HashMap<>();
monthMap.put("Jan", "01");
monthMap.put("Feb", "02");
monthMap.put("Mar", "03");
monthMap.put("Apr", "04");
monthMap.put("May", "05");
monthMap.put("Jun", "06");
monthMap.put("Jul", "07");
monthMap.put("Aug", "08");
monthMap.put("Sep", "09");
monthMap.put("Oct", "10");
monthMap.put("Nov", "11");
monthMap.put("Dec", "12");
String[] parts = date.split(" ");
String day = parts[0].replaceAll("[a-zA-Z]", "");
if (day.length() == 1) day = "0" + day;
String month = monthMap.get(parts[1]);
String year = parts[2];
return year + "-" + month + "-" + day;
}
public static void main(String[] args) {
String d1 = "20th Oct 2052";
String d2 = "6th Jun 1933";
String d3 = "26th May 1960";
System.out.println(reformatDate(d1));
System.out.println(reformatDate(d2));
System.out.println(reformatDate(d3));
}
}