StringUtil.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package com.minpay.common.util;
  2. import org.springframework.util.StringUtils;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.time.LocalDateTime;
  6. import java.time.format.DateTimeFormatter;
  7. import java.util.Date;
  8. import java.util.Map;
  9. /**
  10. * 字符串工具类
  11. */
  12. public class StringUtil extends StringUtils {
  13. /**
  14. * obj 转 String
  15. * @param obj
  16. * @return
  17. */
  18. public static String ObjectToString(Object obj){
  19. if(obj == null || "".equals(obj)){
  20. return "";
  21. }else{
  22. return String.valueOf(obj).replaceAll(" ","");
  23. }
  24. }
  25. /**
  26. * obj转int
  27. * @param obj
  28. * @return
  29. */
  30. public static Integer ObjToInt(Object obj){
  31. if(obj == null || "".equals(obj)){
  32. return 0;
  33. }
  34. if(obj instanceof Integer){
  35. return Integer.valueOf(String.valueOf(obj));
  36. }else{
  37. try {
  38. return Integer.valueOf(String.valueOf(obj).replaceAll("月份","").replaceAll("月",""));
  39. } catch (Exception e){
  40. return 0;
  41. }
  42. }
  43. }
  44. /**
  45. * obj转float
  46. * @param obj
  47. * @return
  48. */
  49. public static Float ObjToFloat(Object obj){
  50. if(obj == null || "".equals(obj)){
  51. return 0f;
  52. }
  53. if(obj instanceof Float){
  54. return Float.valueOf(String.valueOf(obj));
  55. }else{
  56. try {
  57. return Float.valueOf(obj.toString().replaceAll("%",""));
  58. }catch (Exception e){
  59. return 0f;
  60. }
  61. }
  62. }
  63. /**
  64. * obj转double
  65. * @param obj
  66. * @return
  67. */
  68. public static Double ObjToDouble(Object obj){
  69. if(obj == null || "".equals(obj)){
  70. return 0d;
  71. }
  72. if(obj instanceof Double){
  73. return Double.valueOf(String.valueOf(obj));
  74. }else{
  75. try {
  76. return Double.valueOf(String.valueOf(obj));
  77. } catch (Exception e){
  78. return 0d;
  79. }
  80. }
  81. }
  82. /**
  83. * obj转date
  84. * 入参格式yyyy/MM/dd hh:mm:ss
  85. * 入参格式yyyy/mm/dd
  86. * @param obj
  87. * @return
  88. */
  89. public static Date ObjToDate(Object obj,String type) throws ParseException {
  90. if(obj == null || "".equals(obj)){
  91. return null;
  92. }
  93. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
  94. SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
  95. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  96. DateTimeFormatter fa = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  97. if(obj instanceof Date){
  98. if("1".equals(type)){//年月日
  99. return simpleDateFormat.parse(obj.toString().replaceAll("/","-"));
  100. }else if ("2".equals(type)){//年月日 时分秒
  101. return simpleDateFormat2.parse(obj.toString().replaceAll("/","-"));
  102. }
  103. }else{
  104. if(obj.toString().endsWith(".0")){
  105. if("1".equals(type)){
  106. return simpleDateFormat.parse(obj.toString().substring(0,obj.toString().length()-2));
  107. }else if("2".equals(type)){
  108. return simpleDateFormat2.parse(obj.toString().substring(0,obj.toString().length()-2));
  109. }
  110. }else {
  111. LocalDateTime ldt = LocalDateTime.parse(obj.toString().replaceAll(" ","")
  112. .replaceAll("-","")
  113. .replaceAll("/","")
  114. .replaceAll(":",""),dtf);
  115. String datetime = ldt.format(fa);
  116. if("1".equals(type)){
  117. return simpleDateFormat.parse(datetime);
  118. }else if("2".equals(type)){
  119. return simpleDateFormat2.parse(datetime);
  120. }
  121. return null;
  122. }
  123. }
  124. return null;
  125. }
  126. /**
  127. * 入参格式yyyyMMdd hh:mm:ss
  128. * 出参格式yyyy-mm-dd hh:mm:ss
  129. * @param obj
  130. * @return
  131. */
  132. public static Date ObjNumToDate(Object obj) throws ParseException {
  133. if(obj == null || "".equals(obj)){
  134. return null;
  135. }
  136. //去空格
  137. String tempStr = obj.toString().replaceAll(" ","");
  138. if(tempStr.length() != 14){
  139. return null;
  140. }
  141. StringBuffer dateStr = new StringBuffer();
  142. //组装日期格式
  143. dateStr.append(tempStr.substring(0,4))
  144. .append("-")
  145. .append(tempStr.substring(4,6))
  146. .append("-")
  147. .append(tempStr.substring(6,8))
  148. .append(" ")
  149. .append(tempStr.substring(8,10))
  150. .append(":")
  151. .append(tempStr.substring(10,12))
  152. .append(":")
  153. .append(tempStr.substring(12,14));
  154. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
  155. return simpleDateFormat.parse(dateStr.toString());
  156. }
  157. /**
  158. * 日期格式补全
  159. * @param obj
  160. * @return
  161. */
  162. public static String ObjDateRangeCompletion(Object obj){
  163. if(obj == null || "".equals(obj)){
  164. return "";
  165. }
  166. String start ="";
  167. String end = "";
  168. String temp = obj.toString();
  169. temp = temp.replaceAll(" ","").replaceAll("-","").replaceAll("/","");
  170. if(temp.length() == 16){
  171. start = temp.substring(0,8)+"000000";
  172. end = temp.substring(8,16)+"235959";
  173. }
  174. return start+end;
  175. }
  176. /**
  177. * 时间计算
  178. * @param start
  179. * @param end
  180. * @return
  181. */
  182. public static float dateCalculate(String start, String end){
  183. try {
  184. int minute = 0,surplus=0,mt = 60 * 1000;
  185. float second = 0f;
  186. Date s = new Date(start.replaceAll("-","/"));
  187. Date e = new Date(end.replaceAll("-","/"));
  188. long total = e.getTime() - s.getTime();
  189. minute = (int) (total / mt);
  190. surplus = (int) (total - minute * mt);
  191. second = ArithUtils.div(String.valueOf(surplus),String.valueOf(mt));
  192. return ArithUtils.add(String.valueOf(minute),String.valueOf(second) );
  193. }catch (Exception ee){
  194. ee.printStackTrace();
  195. return 0f;
  196. }
  197. }
  198. /**
  199. * 时间计算
  200. * @param start
  201. * @param end
  202. * @return
  203. */
  204. public static float dateCalculate2(Long start, Long end){
  205. try {
  206. int minute = 0,surplus=0,mt = 60 * 1000;
  207. float second = 0f;
  208. long total = end - start;
  209. minute = (int) (total / mt);
  210. surplus = (int) (total - minute * mt);
  211. second = ArithUtils.div(String.valueOf(surplus),String.valueOf(mt));
  212. return ArithUtils.add(String.valueOf(minute),String.valueOf(second) );
  213. }catch (Exception ee){
  214. ee.printStackTrace();
  215. return 0f;
  216. }
  217. }
  218. /**
  219. * 时间计算
  220. * @param range
  221. * @param timeRange = 000000 - 235959
  222. * @return
  223. */
  224. public static String getStartAndEndDatetime(Object range, Object timeRange,String type){
  225. String str1 = StringUtil.ObjectToString(range);
  226. String str2 = StringUtil.ObjectToString(timeRange);
  227. if("1".equals(type)){
  228. return str1.split("~")[0]+" "+str2.split("~")[0];
  229. }else if("2".equals(type)){
  230. return str1.split("~")[1]+" "+str2.split("~")[1];
  231. }
  232. return "";
  233. }
  234. public static void main(String[] args) {
  235. String a = "2021-2-25 10:43:11";
  236. String b = "2021-2-25 10:57:00";
  237. }
  238. }