ObjectUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package com.qucheng.game.data.oldsystem.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang3.StringUtils;
  4. import java.math.BigDecimal;
  5. import java.text.SimpleDateFormat;
  6. import java.time.LocalDate;
  7. import java.time.LocalDateTime;
  8. import java.time.LocalTime;
  9. import java.time.ZoneOffset;
  10. import java.time.format.DateTimeFormatter;
  11. import java.util.Calendar;
  12. import java.util.Date;
  13. @Slf4j
  14. public class ObjectUtil {
  15. public static <T> boolean objEquals(T t1, T t2) {
  16. if (t1 == null && t2 == null) {
  17. return true;
  18. }
  19. if (t1 == null || t2 == null) {
  20. return false;
  21. }
  22. return t1.equals(t2);
  23. }
  24. public static Boolean objToBoolean(Object value) {
  25. return objToBoolean(value, null);
  26. }
  27. public static Boolean objToBoolean(Object value, Boolean defaultValue) {
  28. if (value == null) {
  29. return defaultValue;
  30. }
  31. try {
  32. if (value instanceof Boolean) {
  33. return (Boolean) value;
  34. } else if (value instanceof Integer) {
  35. return (Integer) value > 0;
  36. } else if (value instanceof Long) {
  37. return (Long) value > 0;
  38. } else if (value instanceof Short) {
  39. return (Short) value > 0;
  40. } else if (value instanceof String) {
  41. if ("true".equalsIgnoreCase((String) value)) {
  42. return true;
  43. }
  44. if ("false".equalsIgnoreCase((String) value)) {
  45. return false;
  46. }
  47. }
  48. } catch (Exception e) {
  49. log.error(e.getMessage(), e);
  50. }
  51. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> Boolean]");
  52. }
  53. public static String objToString(Object value) {
  54. return objToString(value, null);
  55. }
  56. public static String objToString(Object value, String defaultValue) {
  57. if (value == null) {
  58. return defaultValue;
  59. }
  60. if (value instanceof String) {
  61. return (String) value;
  62. }
  63. if (value instanceof Double) {
  64. String result = BigDecimal.valueOf((Double) value).toString();
  65. if (result.endsWith(".0")) {
  66. result = result.substring(0, result.length() - 2);
  67. }
  68. return result;
  69. }
  70. if (value instanceof Date) {
  71. return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format((Date) value);
  72. }
  73. if (value instanceof LocalDate) {
  74. return DateUtil.formatLocalDate((LocalDate) value);
  75. }
  76. if (value instanceof LocalDateTime) {
  77. return DateUtil.formatLocalDateTime((LocalDateTime) value);
  78. }
  79. return value.toString();
  80. }
  81. public static Double objToDouble(Object value) {
  82. return objToDouble(value, null);
  83. }
  84. public static Double objToDouble(Object value, Double defaultValue) {
  85. if (value == null) {
  86. return defaultValue;
  87. }
  88. try {
  89. if (value instanceof Integer) {
  90. return Double.valueOf((Integer) value);
  91. }
  92. if (value instanceof BigDecimal) {
  93. return ((BigDecimal) value).doubleValue();
  94. }
  95. if (value instanceof Double) {
  96. return (Double) value;
  97. }
  98. if (value instanceof String) {
  99. return Double.parseDouble((String) value);
  100. }
  101. if (value instanceof Long) {
  102. return Double.valueOf((Long) value);
  103. }
  104. if (value instanceof Float) {
  105. return Double.valueOf((Float) value);
  106. }
  107. if (value instanceof Byte) {
  108. return Double.valueOf((Byte) value);
  109. }
  110. if (value instanceof Short) {
  111. return Double.valueOf((Short) value);
  112. }
  113. } catch (Exception e) {
  114. log.error(e.getMessage(), e);
  115. }
  116. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> Double]");
  117. }
  118. public static Integer objToInteger(Object value) {
  119. return objToInteger(value, null);
  120. }
  121. public static Integer objToInteger(Object value, Integer defaultValue) {
  122. if (value == null) {
  123. return defaultValue;
  124. }
  125. try {
  126. if (value instanceof Integer) {
  127. return (Integer) value;
  128. }
  129. if (value instanceof BigDecimal) {
  130. return ((BigDecimal) value).intValue();
  131. }
  132. if (value instanceof Double) {
  133. return ((Double) value).intValue();
  134. }
  135. if (value instanceof String) {
  136. return Integer.parseInt((String) value);
  137. }
  138. if (value instanceof Long) {
  139. return ((Long) value).intValue();
  140. }
  141. if (value instanceof Short) {
  142. return ((Short) value).intValue();
  143. }
  144. if (value instanceof Float) {
  145. return ((Float) value).intValue();
  146. }
  147. } catch (Exception e) {
  148. log.error(e.getMessage(), e);
  149. }
  150. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> Integer]");
  151. }
  152. public static Long objToLong(Object value) {
  153. return objToLong(value, null);
  154. }
  155. public static Long objToLongNull(Object value) {
  156. return objToLong(value, 0L);
  157. }
  158. public static Long objToLong(Object value, Long defaultValue) {
  159. if (value == null) {
  160. return defaultValue;
  161. }
  162. try {
  163. if (value instanceof Integer) {
  164. return ((Integer) value).longValue();
  165. }
  166. if (value instanceof BigDecimal) {
  167. return ((BigDecimal) value).longValue();
  168. }
  169. if (value instanceof Double) {
  170. return ((Double) value).longValue();
  171. }
  172. if (value instanceof String) {
  173. return Long.parseLong((String) value);
  174. }
  175. if (value instanceof Long) {
  176. return (Long) value;
  177. }
  178. if (value instanceof Float) {
  179. return ((Float) value).longValue();
  180. }
  181. } catch (Exception e) {
  182. log.error(e.getMessage(), e);
  183. }
  184. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> Long]");
  185. }
  186. public static Float objToFloat(Object value) {
  187. return objToFloat(value, null);
  188. }
  189. public static Float objToFloat(Object value, Float defaultValue) {
  190. if (value == null) {
  191. return defaultValue;
  192. }
  193. try {
  194. if (value instanceof Float) {
  195. return (Float) value;
  196. }
  197. if (value instanceof Integer) {
  198. return ((Integer) value).floatValue();
  199. }
  200. if (value instanceof BigDecimal) {
  201. return ((BigDecimal) value).floatValue();
  202. }
  203. if (value instanceof Double) {
  204. return ((Double) value).floatValue();
  205. }
  206. if (value instanceof String) {
  207. return Float.parseFloat((String) value);
  208. }
  209. if (value instanceof Long) {
  210. return ((Long) value).floatValue();
  211. }
  212. } catch (Exception e) {
  213. log.error(e.getMessage(), e);
  214. }
  215. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> Float]");
  216. }
  217. public static BigDecimal objToBigDecimal(Object value) {
  218. return objToBigDecimal(value, null);
  219. }
  220. public static BigDecimal objToBigDecimal(Object value, BigDecimal defaultValue) {
  221. if (value == null) {
  222. return defaultValue;
  223. }
  224. try {
  225. if (value instanceof BigDecimal) {
  226. return (BigDecimal) value;
  227. }
  228. if (value instanceof Float) {
  229. return BigDecimal.valueOf((Float) value);
  230. }
  231. if (value instanceof Integer) {
  232. return BigDecimal.valueOf((Integer) value);
  233. }
  234. if (value instanceof Double) {
  235. return BigDecimal.valueOf((Double) value);
  236. }
  237. if (value instanceof String) {
  238. return new BigDecimal((String) value);
  239. }
  240. if (value instanceof Long) {
  241. return BigDecimal.valueOf((Long) value);
  242. }
  243. } catch (Exception e) {
  244. log.error(e.getMessage(), e);
  245. }
  246. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> BigDecimal]");
  247. }
  248. public static Date objToDate(Object value) {
  249. return objToDate(value, null, "yyyy-MM-dd hh:mm:ss");
  250. }
  251. public static Date objToDate(Object value, String dateFormat) {
  252. return objToDate(value, null, dateFormat);
  253. }
  254. public static Date objToDate(Object value, Date defaultValue) {
  255. return objToDate(value, defaultValue, "yyyy-MM-dd hh:mm:ss");
  256. }
  257. public static Date objToDate(Object value, Date defaultValue, String dateFormat) {
  258. if (value == null) {
  259. return defaultValue;
  260. }
  261. try {
  262. if (value instanceof Date) {
  263. return (Date) value;
  264. }
  265. if (value instanceof Calendar) {
  266. return ((Calendar) value).getTime();
  267. }
  268. if (value instanceof String && dateFormat != null) {
  269. return new SimpleDateFormat(dateFormat).parse((String) value);
  270. }
  271. if (value instanceof Number) {
  272. String temp = value.toString();
  273. if (temp.length() == 13) {
  274. return new Date(((Number) value).longValue());
  275. } else {
  276. return new Date(((Number) value).longValue() * 1000);
  277. }
  278. }
  279. if (value instanceof LocalDateTime) {
  280. return Date.from(((LocalDateTime) value).atZone(ZoneOffset.ofHours(8)).toInstant());
  281. }
  282. if (value instanceof LocalDate) {
  283. return Date.from(((LocalDate) value).atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
  284. }
  285. } catch (Exception e) {
  286. log.error(e.getMessage(), e);
  287. }
  288. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> Date]");
  289. }
  290. public static LocalDate objToLocalDate(Object value) {
  291. return objToLocalDate(value, null, null);
  292. }
  293. public static LocalDate objToLocalDate(Object value, LocalDate defaultValue) {
  294. return objToLocalDate(value, defaultValue, null);
  295. }
  296. public static LocalDate objToLocalDate(Object value, String dateFormat) {
  297. return objToLocalDate(value, null, dateFormat);
  298. }
  299. public static LocalDate objToLocalDate(Object value, LocalDate defaultValue, String dateFormat) {
  300. if (value == null) {
  301. return defaultValue;
  302. }
  303. if (value instanceof LocalDate) {
  304. return (LocalDate) value;
  305. }
  306. if (value instanceof LocalDateTime) {
  307. return ((LocalDateTime) value).toLocalDate();
  308. }
  309. if (value instanceof String) {
  310. if (StringUtils.isBlank(dateFormat)) {
  311. return DateUtil.parseLocalDate((String) value);
  312. }
  313. return LocalDate.parse((String) value, DateTimeFormatter.ofPattern(dateFormat));
  314. }
  315. if (value instanceof Number) {
  316. String temp = value.toString();
  317. if (temp.length() == 13) {
  318. return DateUtil.milliToLocalDate(((Number) value).longValue());
  319. } else {
  320. return DateUtil.secondToLocalDate(((Number) value).intValue());
  321. }
  322. }
  323. if (value instanceof Date) {
  324. return ((Date) value).toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
  325. }
  326. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> LocalDate]");
  327. }
  328. public static LocalDateTime objToLocalDateTime(Object value) {
  329. return objToLocalDateTime(value, null, null);
  330. }
  331. public static LocalDateTime objToLocalDateTime(Object value, LocalDateTime defaultValue) {
  332. return objToLocalDateTime(value, defaultValue, null);
  333. }
  334. public static LocalDateTime objToLocalDateTime(Object value, String dateFormat) {
  335. return objToLocalDateTime(value, null, dateFormat);
  336. }
  337. public static LocalDateTime objToLocalDateTime(Object value, LocalDateTime defaultValue, String dateFormat) {
  338. if (value == null) {
  339. return defaultValue;
  340. }
  341. if (value instanceof LocalDate) {
  342. return LocalDateTime.of((LocalDate) value, LocalTime.MIDNIGHT);
  343. }
  344. if (value instanceof LocalDateTime) {
  345. return (LocalDateTime) value;
  346. }
  347. if (value instanceof String) {
  348. if (StringUtils.isBlank(dateFormat)) {
  349. return DateUtil.parseLocalDateTime((String) value);
  350. }
  351. return LocalDateTime.parse((String) value, DateTimeFormatter.ofPattern(dateFormat));
  352. }
  353. if (value instanceof Number) {
  354. String temp = value.toString();
  355. if (temp.length() == 13) {
  356. return DateUtil.milliToLocalDateTime(((Number) value).longValue());
  357. } else {
  358. return DateUtil.secondToLocalDateTime(((Number) value).intValue());
  359. }
  360. }
  361. if (value instanceof Date) {
  362. return ((Date) value).toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
  363. }
  364. throw new RuntimeException("Unsupported data type[" + value.getClass().getName() + "(" + value + ")" + " --> LocalDateTime]");
  365. }
  366. }