Fri Oct 7 10:08:00 UTC 0800 2016日期格式转换为 yyyy-mm-dd hh24:mi:ss (Map实现版)

来源:互联网 发布:源码资本 趣分期 编辑:程序博客网 时间:2024/05/17 06:12

更新:

枚举实现版本请看点击打开链接

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

赶项目进度,做一个按日期区间将查询结果导出为excel的功能。代码写完自测的时候发现excel没导出,看了控制台输出,报日期格式错误的问题。把日期输出一看居然是“Fri Oct 7 10:08:00 UTC 0800 2016” 这样子的神一般的格式,惊了。

因为是在遗留的需求上做改进,对这个刚接手的项目采用的前端框架不是很熟悉,不太清楚其是如何把我在前端输入的yyyy-mm-dd hh:mi:ss的日期传回来时给换成这种神奇格式的。另外赶着做一时半会也没找着应该用什么API去转换日期格式,干脆就自己临时写了一个,仅供参考。

import java.util.*;public class Test{public static void main(String[] args){String whatTheFuckDate = "Fri Oct 7 10:08:00 UTC 0800 2016";System.out.println(formatDate(whatTheFuckDate));}public static String formatDate(String whatTheFuckDate){StringBuilder afterConvert = new StringBuilder();Map<String, String> monthMap = new HashMap<String, String>();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");try{ //拆分后的数组下标对应   0   1  2     3    4     5   6        //日期是这种形式 Fri Oct 7 10:08:00 UTC 0800 2016String[] arr = whatTheFuckDate.split(" ");afterConvert.append(arr[6] + "-"); afterConvert.append(monthMap.get(arr[1]) + "-"); if(arr[2].length() < 2){ afterConvert.append("0" + arr[2] +  " "); }else{afterConvert.append(arr[2] + " ");}afterConvert.append(arr[3]);}catch(Exception e){System.out.println(e.getMessage());}finally{monthMap.clear();monthMap = null;}return afterConvert.toString();   }}




0 0
原创粉丝点击