Reformat Date
给一个string, 是日期的表示, 返回另一种表示方式.
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 |
class Solution { public String reformatDate(String date) { Map<String, String> months = new HashMap<>(); months.put("Jan", "01"); months.put("Feb", "02"); months.put("Mar", "03"); months.put("Apr", "04"); months.put("May", "05"); months.put("Jun", "06"); months.put("Jul", "07"); months.put("Aug", "08"); months.put("Sep", "09"); months.put("Oct", "10"); months.put("Nov", "11"); months.put("Dec", "12"); String[] arr = date.split(" "); StringBuilder str = new StringBuilder(); str.append(arr[2]); str.append("-"); str.append(months.get(arr[1])); str.append("-"); String day = arr[0].substring(0, arr[0].length()-2); if(day.length() == 1){ str.append("0"); } str.append(day); return str.toString(); } } |