shjung 9 ماه پیش
والد
کامیت
346b7ad324

+ 14 - 0
its-common/src/main/java/com/its/common/utils/TimeUtils.java

@@ -2,6 +2,9 @@ package com.its.common.utils;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.concurrent.TimeUnit;
@@ -307,4 +310,15 @@ public class TimeUtils {
         Calendar cal = Calendar.getInstance();
         return getFiveMinToString(cal.getTime());
     }
+
+    public static long getStringTimeToSeconds(String paramDt, String dateFmt, long defaultSeconds) {
+        try {
+            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFmt);
+            LocalDateTime dateTime = LocalDateTime.parse(paramDt, formatter);
+
+            return dateTime.toEpochSecond(ZoneId.systemDefault().getRules().getOffset(dateTime));
+        } catch (Exception e) {
+            return defaultSeconds;
+        }
+    }
 }

+ 72 - 0
its-common/src/main/java/com/its/common/utils/Unsigned.java

@@ -0,0 +1,72 @@
+package com.its.common.utils;
+
+import java.nio.ByteBuffer;
+
+public class Unsigned {
+
+    private Unsigned() {}
+
+    // Byte
+    public static short getUnsignedByte(ByteBuffer bb) {
+        return((short)(bb.get() & 0xff));
+    }
+    public static short getUnsignedByte(ByteBuffer bb, int position) {
+        return((short)(bb.get(position) &(short)0xff));
+    }
+
+    public static void putUnsignedByte(ByteBuffer bb, int value) {
+        bb.put((byte)(value & 0xff));
+    }
+    public static void putUnsignedByte(ByteBuffer bb, int position, int value) {
+        bb.put(position,(byte)(value & 0xff));
+    }
+
+    // Short
+    public static int getUnsignedShort(ByteBuffer bb) {
+        return(bb.getShort() & 0xFFFF);
+    }
+    public static int getUnsignedShort(ByteBuffer bb, int position) {
+        return(bb.getShort(position) & 0xFFFF);
+    }
+
+    public static void putUnsignedShort(ByteBuffer bb, int value) {
+        bb.putShort((short)(value & 0xFFFF));
+    }
+    public static void putUnsignedShort(ByteBuffer bb, int position, int value) {
+        bb.putShort(position,(short)(value & 0xFFFF));
+    }
+
+    // Integer
+    public static long getUnsignedInt(ByteBuffer bb) {
+        return(bb.getInt() & 0xFFFFFFFFL);
+    }
+    public static long getUnsignedInt(ByteBuffer bb, int position) {
+        return(bb.getInt(position) & 0xFFFFFFFFL);
+    }
+
+    public static void putUnsignedInt(ByteBuffer bb, long value) {
+        bb.putInt((int)(value & 0xFFFFFFFFL));
+    }
+    public static void putUnsignedInt(ByteBuffer bb, int position, long value) {
+        bb.putInt(position,(int)(value & 0xFFFFFFFFL));
+    }
+
+    // TEST
+    public static void main(String [] argv) {
+        ByteBuffer buffer = ByteBuffer.allocate(20);
+
+        buffer.clear();
+        Unsigned.putUnsignedByte(buffer, 255);
+        Unsigned.putUnsignedByte(buffer, 128);
+        Unsigned.putUnsignedShort(buffer, 0xCAFE);
+        Unsigned.putUnsignedInt(buffer, 0xCAFEBABE);
+
+        for(int i = 0; i < 8; i++) {
+            System.out.println("" + i + ": "
+                    + Integer.toHexString((int)getUnsignedByte(buffer, i)));
+        }
+
+        System.out.println("2: " + Integer.toHexString(getUnsignedShort(buffer, 2)));
+        System.out.println("4: " + Long.toHexString(getUnsignedInt(buffer, 4)));
+    }
+}