chudk 7 vuotta sitten
vanhempi
commit
4955de43de

+ 28 - 0
src/main/java/com/tidecloud/dataacceptance/common/DateUtil.java

@@ -0,0 +1,28 @@
+package com.tidecloud.dataacceptance.common;
+
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+/**   
+ * @author: chudk 
+ * @date:   2017年9月21日 上午10:09:31   
+ */
+public class DateUtil {
+    
+    public static Date calculateByDate(Date d, int amount) {   
+        return calculate(d, GregorianCalendar.DATE, amount);   
+    }  
+    
+    public static Date calculateByHour(Date d, int amount){
+        return calculate(d, GregorianCalendar.HOUR, amount);
+    }
+    
+    private static Date calculate(Date d, int field, int amount) {   
+        if (d == null)   
+            return null;   
+        GregorianCalendar g = new GregorianCalendar();   
+        g.setTime(d);
+        g.add(field, amount);   
+        return g.getTime();   
+    }  
+}

+ 19 - 2
src/main/java/com/tidecloud/dataacceptance/entity/Device.java

@@ -1,8 +1,12 @@
 package com.tidecloud.dataacceptance.entity;
 
 import java.io.Serializable;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 
+import com.tidecloud.dataacceptance.common.DateUtil;
+
 /**   
  * @author: chudk 
  * @date:   2017年9月18日 上午11:04:09   
@@ -81,10 +85,11 @@ public class Device implements Serializable{
                 + electric + ", step=" + step + ", terminalState=" + terminalState + ", deviceId=" + deviceId + "]";
     }
     public static String buildDeviceStr(Device device){
-        
+        Date timestamp = device.getTimestamp();
+        timestamp = DateUtil.calculateByHour(timestamp, 8);
         StringBuilder builder = new StringBuilder();
         builder.append(device.getDeviceId()).append(",");
-        builder.append(device.getTimestamp().getTime() / 1000).append(",");
+        builder.append(1).append(",");
         builder.append(device.getItemState()).append(",");
         builder.append(device.getLng()).append(",");
         builder.append(device.getLat()).append(",");
@@ -93,4 +98,16 @@ public class Device implements Serializable{
         
         return builder.toString();
     }
+    
+    public static void main(String[] args) {
+        Date parse;
+        try {   
+            parse = new SimpleDateFormat("yyMMddHHmmss").parse("170228235504");
+            Date calculateByDate = DateUtil.calculateByHour(parse, 8);
+            String format = new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(calculateByDate);
+            System.out.println(format);
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+    }
 }

+ 2 - 2
src/main/java/com/tidecloud/datacceptance/service/impl/DiscardServerHandler.java

@@ -167,14 +167,14 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
         }*/
         String date = bodys[1];
         String time = bodys[2];
-        Date timestamp = new SimpleDateFormat("yyMMddHHmmss").parse(date + time);
+        Date timestamp = new SimpleDateFormat("ddMMyyHHmmss").parse(date + time);
         device.setTimestamp(timestamp);
         device.setLat(getDouble(bodys[4]));
         device.setLng(getDouble(bodys[6]));
         device.setSpeed(getDouble(bodys[8]));
         device.setElectric(getDouble(bodys[13]));
         device.setStep(getInteger(bodys[14]));
-        device.setItemState(bodys[15]);
+        device.setItemState(bodys[16]);
         // getDouble()
         logger.info("设备上传具体监控项信息:" + device.toString());
         return device;