guozhaoshun vor 6 Jahren
Ursprung
Commit
05a3916048

+ 11 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/util/DateUtil.java

@@ -255,6 +255,17 @@ public class DateUtil {
 		return formatter.format(new Date(times));
 	}
 
+	/**
+	 * @param startDate
+	 * @param interval
+	 * @return 某日期加间隔分钟数后日期
+	 */
+	public static Date getAfterIntervalMInDate(Date startDate, int interval) {
+		Date calendar = new Date();
+		calendar.setTime((startDate.getTime() + interval * 60 * 1000));
+		return calendar;
+	}
+
 	public static void main(String[] args) {
 		System.out.print(parseDateStr(1543766400000l));
 	}

+ 122 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/util/FastJsonUtils.java

@@ -0,0 +1,122 @@
+package cn.rankin.common.utils.util;
+
+import java.util.List;
+import java.util.Map;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
+import com.alibaba.fastjson.serializer.SerializeConfig;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+
+/**
+ * fastjson工具类
+ * @version:1.0.0
+ */
+public class FastJsonUtils {
+
+    private static final SerializeConfig config;
+
+    static {
+        config = new SerializeConfig();
+        config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
+        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
+    }
+
+    private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段
+            SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
+            SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
+            SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
+            SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null
+    };
+
+
+    public static String convertObjectToJSON(Object object) {
+        return JSON.toJSONString(object, config, features);
+    }
+
+    public static String toJSONNoFeatures(Object object) {
+        return JSON.toJSONString(object, config);
+    }
+
+
+
+    public static Object toBean(String text) {
+        return JSON.parse(text);
+    }
+
+    public static <T> T toBean(String text, Class<T> clazz) {
+        return JSON.parseObject(text, clazz);
+    }
+
+    // 转换为数组
+    public static <T> Object[] toArray(String text) {
+        return toArray(text, null);
+    }
+
+    // 转换为数组
+    public static <T> Object[] toArray(String text, Class<T> clazz) {
+        return JSON.parseArray(text, clazz).toArray();
+    }
+
+    // 转换为List
+    public static <T> List<T> toList(String text, Class<T> clazz) {
+        return JSON.parseArray(text, clazz);
+    }
+
+    /**
+     * 将javabean转化为序列化的json字符串
+     * @param keyvalue
+     * @return
+     */
+    /*public static Object beanToJson(KeyValue keyvalue) {
+        String textJson = JSON.toJSONString(keyvalue);
+        Object objectJson  = JSON.parse(textJson);
+        return objectJson;
+    }*/
+
+    /**
+     * 将string转化为序列化的json字符串
+     * @param keyvalue
+     * @return
+     */
+    public static Object textToJson(String text) {
+        Object objectJson  = JSON.parse(text);
+        return objectJson;
+    }
+
+    /**
+     * json字符串转化为map
+     * @param s
+     * @return
+     */
+    public static <K, V> Map<K, V>  stringToCollect(String s) {
+        Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);
+        return m;
+    }
+
+    /**
+     * 转换JSON字符串为对象
+     * @param jsonData
+     * @param clazz
+     * @return
+     */
+    public static Object convertJsonToObject(String jsonData, Class<?> clazz) {
+        return JSONObject.parseObject(jsonData, clazz);
+    }
+
+    public static Object convertJSONToObject(String content, Class<?> clazz) {
+        return JSONObject.parseObject(content, clazz);
+    }
+
+    /**
+     * 将map转化为string
+     * @param m
+     * @return
+     */
+    public static <K, V> String collectToString(Map<K, V> m) {
+        String s = JSONObject.toJSONString(m);
+        return s;
+    }
+
+}

+ 54 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/util/QRCodeUtil.java

@@ -0,0 +1,54 @@
+package cn.rankin.common.utils.util;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.MultiFormatWriter;
+import com.google.zxing.WriterException;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.client.j2se.MatrixToImageWriter;
+import sun.misc.BASE64Encoder;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+public class QRCodeUtil {
+
+    /**
+     * 根据内容,生成指定宽高、指定格式的二维码图片
+     *
+     * @param url   内容
+     * @return 生成的二维码图片路径
+     * @throws Exception
+     */
+    public static String generateQRCode(String url) throws IOException, WriterException {
+        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
+        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
+        hints.put(EncodeHintType.MARGIN, 0);
+        BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 360, 360, hints);
+        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        ImageIO.write(image, "png", os);
+        byte b[] = os.toByteArray();
+        String QRCode = new BASE64Encoder().encode(b);
+        return QRCode;
+    }
+
+    public static void main(String[] args) throws Exception {
+
+        Map qrcode = new HashMap();
+        qrcode.put("user","100166661101001");
+        qrcode.put("time",new Date());
+        String json = FastJsonUtils.collectToString(qrcode);
+        String qrCode = QRCodeUtil.generateQRCode(json);
+        System.out.println(qrCode);
+
+
+
+    }
+}
+