| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- package com.minpay.common.util;
- import org.springframework.util.StringUtils;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Date;
- import java.util.Map;
- /**
- * 字符串工具类
- */
- public class StringUtil extends StringUtils {
- /**
- * obj 转 String
- * @param obj
- * @return
- */
- public static String ObjectToString(Object obj){
- if(obj == null || "".equals(obj)){
- return "";
- }else{
- return String.valueOf(obj).replaceAll(" ","");
- }
- }
- /**
- * obj转int
- * @param obj
- * @return
- */
- public static Integer ObjToInt(Object obj){
- if(obj == null || "".equals(obj)){
- return 0;
- }
- if(obj instanceof Integer){
- return Integer.valueOf(String.valueOf(obj));
- }else{
- try {
- return Integer.valueOf(String.valueOf(obj).replaceAll("月份","").replaceAll("月",""));
- } catch (Exception e){
- return 0;
- }
- }
- }
- /**
- * obj转float
- * @param obj
- * @return
- */
- public static Float ObjToFloat(Object obj){
- if(obj == null || "".equals(obj)){
- return 0f;
- }
- if(obj instanceof Float){
- return Float.valueOf(String.valueOf(obj));
- }else{
- try {
- return Float.valueOf(obj.toString().replaceAll("%",""));
- }catch (Exception e){
- return 0f;
- }
- }
- }
- /**
- * obj转double
- * @param obj
- * @return
- */
- public static Double ObjToDouble(Object obj){
- if(obj == null || "".equals(obj)){
- return 0d;
- }
- if(obj instanceof Double){
- return Double.valueOf(String.valueOf(obj));
- }else{
- try {
- return Double.valueOf(String.valueOf(obj));
- } catch (Exception e){
- return 0d;
- }
- }
- }
- /**
- * obj转date
- * 入参格式yyyy/MM/dd hh:mm:ss
- * 入参格式yyyy/mm/dd
- * @param obj
- * @return
- */
- public static Date ObjToDate(Object obj,String type) throws ParseException {
- if(obj == null || "".equals(obj)){
- return null;
- }
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
- SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
- DateTimeFormatter fa = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- if(obj instanceof Date){
- if("1".equals(type)){//年月日
- return simpleDateFormat.parse(obj.toString().replaceAll("/","-"));
- }else if ("2".equals(type)){//年月日 时分秒
- return simpleDateFormat2.parse(obj.toString().replaceAll("/","-"));
- }
- }else{
- if(obj.toString().endsWith(".0")){
- if("1".equals(type)){
- return simpleDateFormat.parse(obj.toString().substring(0,obj.toString().length()-2));
- }else if("2".equals(type)){
- return simpleDateFormat2.parse(obj.toString().substring(0,obj.toString().length()-2));
- }
- }else {
- LocalDateTime ldt = LocalDateTime.parse(obj.toString().replaceAll(" ","")
- .replaceAll("-","")
- .replaceAll("/","")
- .replaceAll(":",""),dtf);
- String datetime = ldt.format(fa);
- if("1".equals(type)){
- return simpleDateFormat.parse(datetime);
- }else if("2".equals(type)){
- return simpleDateFormat2.parse(datetime);
- }
- return null;
- }
- }
- return null;
- }
- /**
- * 入参格式yyyyMMdd hh:mm:ss
- * 出参格式yyyy-mm-dd hh:mm:ss
- * @param obj
- * @return
- */
- public static Date ObjNumToDate(Object obj) throws ParseException {
- if(obj == null || "".equals(obj)){
- return null;
- }
- //去空格
- String tempStr = obj.toString().replaceAll(" ","");
- if(tempStr.length() != 14){
- return null;
- }
- StringBuffer dateStr = new StringBuffer();
- //组装日期格式
- dateStr.append(tempStr.substring(0,4))
- .append("-")
- .append(tempStr.substring(4,6))
- .append("-")
- .append(tempStr.substring(6,8))
- .append(" ")
- .append(tempStr.substring(8,10))
- .append(":")
- .append(tempStr.substring(10,12))
- .append(":")
- .append(tempStr.substring(12,14));
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
- return simpleDateFormat.parse(dateStr.toString());
- }
- /**
- * 日期格式补全
- * @param obj
- * @return
- */
- public static String ObjDateRangeCompletion(Object obj){
- if(obj == null || "".equals(obj)){
- return "";
- }
- String start ="";
- String end = "";
- String temp = obj.toString();
- temp = temp.replaceAll(" ","").replaceAll("-","").replaceAll("/","");
- if(temp.length() == 16){
- start = temp.substring(0,8)+"000000";
- end = temp.substring(8,16)+"235959";
- }
- return start+end;
- }
- /**
- * 时间计算
- * @param start
- * @param end
- * @return
- */
- public static float dateCalculate(String start, String end){
- try {
- int minute = 0,surplus=0,mt = 60 * 1000;
- float second = 0f;
- Date s = new Date(start.replaceAll("-","/"));
- Date e = new Date(end.replaceAll("-","/"));
- long total = e.getTime() - s.getTime();
- minute = (int) (total / mt);
- surplus = (int) (total - minute * mt);
- second = ArithUtils.div(String.valueOf(surplus),String.valueOf(mt));
- return ArithUtils.add(String.valueOf(minute),String.valueOf(second) );
- }catch (Exception ee){
- ee.printStackTrace();
- return 0f;
- }
- }
- /**
- * 时间计算
- * @param start
- * @param end
- * @return
- */
- public static float dateCalculate2(Long start, Long end){
- try {
- int minute = 0,surplus=0,mt = 60 * 1000;
- float second = 0f;
- long total = end - start;
- minute = (int) (total / mt);
- surplus = (int) (total - minute * mt);
- second = ArithUtils.div(String.valueOf(surplus),String.valueOf(mt));
- return ArithUtils.add(String.valueOf(minute),String.valueOf(second) );
- }catch (Exception ee){
- ee.printStackTrace();
- return 0f;
- }
- }
- /**
- * 时间计算
- * @param range
- * @param timeRange = 000000 - 235959
- * @return
- */
- public static String getStartAndEndDatetime(Object range, Object timeRange,String type){
- String str1 = StringUtil.ObjectToString(range);
- String str2 = StringUtil.ObjectToString(timeRange);
- if("1".equals(type)){
- return str1.split("~")[0]+" "+str2.split("~")[0];
- }else if("2".equals(type)){
- return str1.split("~")[1]+" "+str2.split("~")[1];
- }
- return "";
- }
- public static void main(String[] args) {
- String a = "2021-2-25 10:43:11";
- String b = "2021-2-25 10:57:00";
- }
- }
|