作者 王勇

封装时间转换

1 package com.sunyo.wlpt.message.bus.service.utils; 1 package com.sunyo.wlpt.message.bus.service.utils;
2 2
  3 +import java.text.ParsePosition;
3 import java.text.SimpleDateFormat; 4 import java.text.SimpleDateFormat;
4 import java.util.Calendar; 5 import java.util.Calendar;
5 import java.util.Date; 6 import java.util.Date;
@@ -86,4 +87,18 @@ public class DateUtils { @@ -86,4 +87,18 @@ public class DateUtils {
86 calendar.add(Calendar.DATE, days); 87 calendar.add(Calendar.DATE, days);
87 return calendar.getTime(); 88 return calendar.getTime();
88 } 89 }
  90 +
  91 + /**
  92 + * 将字符串时间转换成 java.util.Date 类型
  93 + *
  94 + * @param strDate 格式为 yyyyMMddHHmmss 的时间字符串
  95 + * @return java.util.Date
  96 + */
  97 + public static Date strToDateLong(String strDate)
  98 + {
  99 + SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
  100 + ParsePosition pos = new ParsePosition(0);
  101 + Date strtodate = formatter.parse(strDate, pos);
  102 + return strtodate;
  103 + }
89 } 104 }
@@ -14,8 +14,6 @@ import org.springframework.beans.factory.annotation.Value; @@ -14,8 +14,6 @@ import org.springframework.beans.factory.annotation.Value;
14 import org.springframework.stereotype.Component; 14 import org.springframework.stereotype.Component;
15 15
16 import javax.annotation.Resource; 16 import javax.annotation.Resource;
17 -import java.text.ParsePosition;  
18 -import java.text.SimpleDateFormat;  
19 import java.util.Date; 17 import java.util.Date;
20 import java.util.List; 18 import java.util.List;
21 19
@@ -55,13 +53,10 @@ public class XmlUtils { @@ -55,13 +53,10 @@ public class XmlUtils {
55 { 53 {
56 // 将String类型的xml字符串转成xml 54 // 将String类型的xml字符串转成xml
57 Document document = DocumentHelper.parseText(xmlStr); 55 Document document = DocumentHelper.parseText(xmlStr);
  56 +
58 Element msg = document.getRootElement(); 57 Element msg = document.getRootElement();
59 Element meta = msg.element("META"); 58 Element meta = msg.element("META");
60 -  
61 - String ddtm = meta.elementText("DDTM");  
62 - SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
63 - ParsePosition pos = new ParsePosition(0);  
64 - Date sendDateTime = formatter.parse(ddtm, pos); 59 + Date sendDateTime = DateUtils.strToDateLong(meta.elementText("DDTM"));
65 60
66 XmlData xmlData = XmlData.builder() 61 XmlData xmlData = XmlData.builder()
67 .sendContent(msg.elementText("BODY")) 62 .sendContent(msg.elementText("BODY"))