|
@@ -0,0 +1,320 @@
|
|
|
|
+package com.ywt.biz.common.util;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.google.gson.Gson;
|
|
|
|
+import com.ywt.biz.common.util.serializers.JsonSerializer;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.text.DecimalFormat;
|
|
|
|
+import java.text.ParseException;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * create by daiyihua
|
|
|
|
+ */
|
|
|
|
+public class FormatUtil {
|
|
|
|
+
|
|
|
|
+ public static final String FORMAT_DATE = "yyyy-MM-dd";
|
|
|
|
+ public static final String FORMAT_DATE_MINUTE = "yyyy-MM-dd HH:mm";
|
|
|
|
+ public static final String FORMAT_DATE_CYMDHM = "yyyy年M月d日 HH:mm";
|
|
|
|
+ public static final String FORMAT_DATE_SECOND = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 保留两位有效数字
|
|
|
|
+ */
|
|
|
|
+ public static String decimalFormat(BigDecimal value) {
|
|
|
|
+ DecimalFormat df = new DecimalFormat("0.00");
|
|
|
|
+ return df.format(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 保留两位有效数字
|
|
|
|
+ */
|
|
|
|
+ public static String decimalFormat(Double value) {
|
|
|
|
+ DecimalFormat df = new DecimalFormat("0.00");
|
|
|
|
+ if (value == null || value == 0) {
|
|
|
|
+ return "0.00";
|
|
|
|
+ }
|
|
|
|
+ return df.format(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(专用list列表里面)
|
|
|
|
+ */
|
|
|
|
+ public static String createTimeFormatList(String value) {
|
|
|
|
+ if (value == null || value.equals("")) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_MINUTE);
|
|
|
|
+ return sdf.format(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(专用list列表里面)
|
|
|
|
+ */
|
|
|
|
+ public static String createTimeFormatList(Date value) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_MINUTE);
|
|
|
|
+ return sdf.format(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(专用详情里面)
|
|
|
|
+ */
|
|
|
|
+ public static String createTimeFormatDetail(String value) {
|
|
|
|
+ if (value == null || value.equals("")) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ if (value.length() == 19) {
|
|
|
|
+ value = value.substring(0, 19);
|
|
|
|
+ }
|
|
|
|
+ return value;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(专用详情里面)
|
|
|
|
+ */
|
|
|
|
+ public static String createTimeFormatDetail(Date value) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_SECOND);
|
|
|
|
+ return sdf.format(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(得到日期)
|
|
|
|
+ */
|
|
|
|
+ public static String formatDate(Date value) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE);
|
|
|
|
+ return sdf.format(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(得到日期)
|
|
|
|
+ */
|
|
|
|
+ public static String formatDate(Date value, String pattern) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ if (Checker.isNull(pattern)) {
|
|
|
|
+ pattern = FORMAT_DATE;
|
|
|
|
+ }
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
|
+ return sdf.format(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(得到日期)
|
|
|
|
+ */
|
|
|
|
+ public static String formatDate(String value) {
|
|
|
|
+ if (value == null || value.equals("")) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE);
|
|
|
|
+ return sdf.format(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * BigDecimal 放大100倍后转int
|
|
|
|
+ *
|
|
|
|
+ * @param value
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static int bigDecimalTo100Int(BigDecimal value) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ BigDecimal bigDecimal = new BigDecimal("100");
|
|
|
|
+ bigDecimal = bigDecimal.multiply(value);
|
|
|
|
+ return bigDecimal.intValue();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * int 缩小100后转string
|
|
|
|
+ *
|
|
|
|
+ * @param value
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String intShrink100ToStr(Integer value) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return "0.00";
|
|
|
|
+ }
|
|
|
|
+ DecimalFormat df = new DecimalFormat("0.00");
|
|
|
|
+ return df.format(new BigDecimal(value).divide(new BigDecimal("100")).doubleValue());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * string 转 map,仅适用于json格式的string,eg:{"refuseOrderRemark":"this is add remark","company":{"companyName":"中华","address":"北京"},"username":"tom"}
|
|
|
|
+ *
|
|
|
|
+ * @param data
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public static Map stringToMap(String data) throws Exception {
|
|
|
|
+ if (Checker.isNone(data)) {
|
|
|
|
+ data = "{}";
|
|
|
|
+ }
|
|
|
|
+ Gson gson = new Gson();
|
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
|
+ map = gson.fromJson(data, map.getClass());
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 往string里面添加结点,并返回string
|
|
|
|
+ *
|
|
|
|
+ * @param data
|
|
|
|
+ * @param key
|
|
|
|
+ * @param value
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public static String jsonAddNode(String data, String key, String value) throws Exception {
|
|
|
|
+ Map<String, Object> map = stringToMap(data);
|
|
|
|
+ map.put(key, value);
|
|
|
|
+ return JsonSerializer.toJson(map);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(得到日期)
|
|
|
|
+ */
|
|
|
|
+ public static Date stringToDate(String value) throws ParseException {
|
|
|
|
+ if (value == null || value.equals("")) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE);
|
|
|
|
+ return sdf.parse(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 字符串的数字 放大100倍后转int
|
|
|
|
+ *
|
|
|
|
+ * @param value
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static int stringTo100Int(String value) {
|
|
|
|
+ if (value == null || value.trim() == "" || "".equals(value.trim()) || Checker.isNotNumber(value)) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ BigDecimal bigDecimalValue = new BigDecimal(value);
|
|
|
|
+ BigDecimal bigDecimal = new BigDecimal("100");
|
|
|
|
+ bigDecimal = bigDecimal.multiply(bigDecimalValue);
|
|
|
|
+ return bigDecimal.intValue();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * string型数据转list数据
|
|
|
|
+ *
|
|
|
|
+ * @param objectIds 传入的数据,eg: 1,3,5
|
|
|
|
+ * @return {@link List <Integer>} eg: [1,3,5]
|
|
|
|
+ */
|
|
|
|
+ public static List<Integer> getIntegerIds(String objectIds) {
|
|
|
|
+ if (!Checker.isNone(objectIds)) {
|
|
|
|
+ String[] objectIdsArr = objectIds.split(",");
|
|
|
|
+ List<Integer> lst = new LinkedList<>();
|
|
|
|
+ for (String id : objectIdsArr) {
|
|
|
|
+ if (Checker.isNone(id)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ lst.add(Integer.parseInt(id));
|
|
|
|
+ }
|
|
|
|
+ return lst;
|
|
|
|
+ }
|
|
|
|
+ return new LinkedList<>();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 字符串的数字 放大100倍后转int
|
|
|
|
+ *
|
|
|
|
+ * @param value 传过来的值
|
|
|
|
+ * @return long型的数字
|
|
|
|
+ */
|
|
|
|
+ public static long stringTo100Long(String value) {
|
|
|
|
+ if (value == null || value.trim() == "" || Checker.isNotNumber(value)) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ BigDecimal bigDecimalValue = new BigDecimal(value);
|
|
|
|
+ BigDecimal bigDecimal = new BigDecimal("100");
|
|
|
|
+ bigDecimal = bigDecimal.multiply(bigDecimalValue);
|
|
|
|
+ return bigDecimal.longValue();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * long 缩小100后转string
|
|
|
|
+ *
|
|
|
|
+ * @param value long类型的数字
|
|
|
|
+ * @return 缩小100倍之后的值
|
|
|
|
+ */
|
|
|
|
+ public static String longShrink100ToStr(Long value) {
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return "0.00";
|
|
|
|
+ }
|
|
|
|
+ DecimalFormat df = new DecimalFormat("0.00");
|
|
|
|
+ return df.format(new BigDecimal(value).divide(new BigDecimal("100")).doubleValue());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<Integer> idsToList(String ids) {
|
|
|
|
+ List list = new ArrayList();
|
|
|
|
+ if (!Checker.isNone(ids)) {
|
|
|
|
+ String[] idArr = ids.split(",");
|
|
|
|
+ for (String idStr : idArr) {
|
|
|
|
+ if (!Checker.isNone(idStr.trim())) {
|
|
|
|
+ list.add(Integer.parseInt(idStr));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String listToIds(List<Integer> listId) {
|
|
|
|
+ StringBuilder sb = new StringBuilder(",");
|
|
|
|
+ if (!Checker.isNone(listId)) {
|
|
|
|
+ for (Integer integerValue : listId) {
|
|
|
|
+ if (!Checker.isNone(integerValue)) {
|
|
|
|
+ sb.append(integerValue).append(",");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ sb.append(",");
|
|
|
|
+ }
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间转换(得到日期)
|
|
|
|
+ */
|
|
|
|
+ public static Date stringToDate(String value, String formart) throws ParseException {
|
|
|
|
+ if (value == null || value.equals("")) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(formart);
|
|
|
|
+ return sdf.parse(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String stringSetToIds(Set<String> listId) {
|
|
|
|
+ StringBuilder sb = new StringBuilder(",");
|
|
|
|
+ if (!Checker.isNone(listId)) {
|
|
|
|
+ for (String idStr : listId) {
|
|
|
|
+ if (!Checker.isNone(idStr)) {
|
|
|
|
+ sb.append(idStr).append(",");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ sb.append(",");
|
|
|
|
+ }
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|