Blame view

src/main/java/com/huaheng/common/utils/DataUtils.java 5.76 KB
tangying authored
1
2
package com.huaheng.common.utils;
3
4
import org.apache.commons.lang3.time.DateFormatUtils;
mahuandong authored
5
import java.io.UnsupportedEncodingException;
tangying authored
6
7
8
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
mahuandong authored
9
import java.net.URLEncoder;
tangying authored
10
11
import java.text.ParseException;
import java.text.SimpleDateFormat;
12
import java.util.Calendar;
tangying authored
13
import java.util.Date;
mahuandong authored
14
import java.util.Map;
tangying authored
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

public class DataUtils {

    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /***
     *  转化为Example方法名
     * @param filedName  字段名
     * @param methodSuffix  方法后缀
     * @return
     */
    public static String ConvertMethodName(String filedName, String methodSuffix) {
        String returnValue = null;
        if (filedName.length() > 0)
        {
            char[] nameChar = filedName.toCharArray();
            if (nameChar[0] >= 'a' && nameChar[0] <= 'z') {
                nameChar[0] = (char) (nameChar[0] - 32);
            }
            returnValue = "and" +   String.valueOf(nameChar) + methodSuffix;
        }
        return returnValue;
    }

    /**
     *  将对象转为字符串
     * @param object
     * @return
     */
    public static String getString(Object object)
    {
xqs authored
46
47
48
49
50
        if (object == null) {
            return null;
        } else {
            return object.toString();
        }
tangying authored
51
52
53
54
55
56
57
58
59
    }

    /**
     *  将对象转为Integer
     * @param object
     * @return
     */
    public static Integer getInteger(Object object)
    {
xqs authored
60
61
62
63
64
        if (object == null) {
            return null;
        } else {
            return Integer.valueOf(object.toString());
        }
tangying authored
65
66
67
68
69
70
71
72
73
    }

    /**
     *  将对象转为BigDecimal
     * @param object
     * @return
     */
    public static BigDecimal getBigDecimal(Object object)
    {
xqs authored
74
75
76
77
78
        if (object == null) {
            return null;
        } else {
            return new BigDecimal(object.toString());
        }
tangying authored
79
80
81
82
83
84
85
86
87
    }

    /**
     *  将对象转为Double
     * @param object
     * @return
     */
    public static Double getDouble(Object object)
    {
xqs authored
88
89
90
91
92
        if (object == null) {
            return null;
        } else {
            return new Double(object.toString());
        }
tangying authored
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    }

    /**
     *  long转为Integer
     * @param object
     * @return
     */
    public static Integer getInteger(long object)
    {
        return Integer.valueOf(String.valueOf(object));
    }

    /**
     *  将对象转为Date
     * @param object
     * @return
     */
    public static Date getDateTime(Object object)  throws ParseException
    {
        if (object == null)
        {
            return null;
        }
        else
        {
            return format.parse(object.toString());
        }
    }

    /**
     * 通过一个实体类,给另一个实体类赋值
     * @param source  源实体类
     * @param target  目标实体类
     * @param ignoreProperties 忽略字段(多个字段用逗号隔开)
     * @throws Exception
     */
    public static void CopyDataByNotNull (Object source, Object target, String ignoreProperties) throws Exception {
        Field[] sourceFields  =  source.getClass().getDeclaredFields();
        Field[] targetFields  =  target.getClass().getDeclaredFields();
        Field.setAccessible(targetFields, true);

        for (int i = 0; i < sourceFields.length; i++)   {
            String sourceName = sourceFields[i].getName();
            Object sourceValue = sourceFields[i].get(source);
            if (sourceValue == null || ignoreProperties.indexOf(sourceName) > -1) {
                continue;
            }
            for (Field targetField : targetFields) {
                if (targetField.getName().equals(sourceName)) {
                    targetField.set(target, sourceValue);
                    break;
                }
            }
        }
    }
mahuandong authored
149
150
151
152
153
154
    /**
     * 格式化map
     * @param map
     * @return
     */
    public static String sendGetFormat(Map<String, Object> map){
tangying authored
155
mahuandong authored
156
157
158
159
160
161
        StringBuilder reslut = new StringBuilder();
        for (Map.Entry<String, Object> a : map.entrySet()) {
            reslut.append(a.getKey()).append("=").append(EncodingUtil.encodeURIComponent(String.valueOf(a.getValue()))+"&");
        }
        return String.valueOf(reslut);
    }
tangying authored
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    /**
     *
     * @param pre 前缀
     * @param maxCode  表的最大流水后缀
     *  addOne  1自动加10不加
     * @return
     */
    public static  String createSerialNumber(String pre,String maxCode,int addOne){
        String result=null;
        Date date=new Date();
        String datestr=DateFormatUtils.format(date, "yyyyMMdd");
        Integer count=0;
        if(StringUtils.isNotEmpty(maxCode)&&!maxCode.equals("0")){
            count = Integer.valueOf(maxCode.substring(maxCode.length() - 4, maxCode.length()));
        }
        if(addOne==1){
            result=pre+datestr+ String.format("%04d", count + 1);
        }else{
            result=pre+datestr+ String.format("%04d", count );
        }
        return result;
    }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    /**
     *再生批号 addOne0则是昨天得
     * @param pre 前缀
     *  addOne  1自动加10不加
     * @return
     */
    public static  String createSerialNumberForRegenerationCode(String pre,int addOne){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String result=null;
        Date date=new Date();
        String datestr=format.format(date);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        Date start = c.getTime();
        String qyt= format.format(start);//前一天
        if(addOne==1){
            result=pre+datestr;
        }else{
            result=pre+qyt;
        }
        return result;
    }
210
211
212
    public static void main(String[] args) {
        System.out.println(createSerialNumber("P1","CGRKD2022042700001",1));
    }
tangying authored
213
214

}