Blame view

src/main/java/com/huaheng/common/utils/DataUtils.java 5.61 KB
tangying authored
1
2
3
4
5
6
7
package com.huaheng.common.utils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
mahuandong authored
8
import java.util.Map;
9
import java.util.Random;
tangying authored
10
11
12
13
14
15

public class DataUtils {

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

    /***
16
17
18
     * 转化为Example方法名
     * @param  filedName    字段名
     * @param  methodSuffix 方法后缀
tangying authored
19
20
21
22
     * @return
     */
    public static String ConvertMethodName(String filedName, String methodSuffix) {
        String returnValue = null;
23
        if (filedName.length() > 0) {
tangying authored
24
25
            char[] nameChar = filedName.toCharArray();
            if (nameChar[0] >= 'a' && nameChar[0] <= 'z') {
26
                nameChar[0] = (char)(nameChar[0] - 32);
tangying authored
27
            }
28
            returnValue = "and" + String.valueOf(nameChar) + methodSuffix;
tangying authored
29
30
31
32
33
        }
        return returnValue;
    }

    /**
34
35
     * 将对象转为字符串
     * @param  object
tangying authored
36
37
     * @return
     */
38
    public static String getString(Object object) {
xqs authored
39
40
41
42
43
        if (object == null) {
            return null;
        } else {
            return object.toString();
        }
tangying authored
44
45
46
    }

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

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

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

    /**
86
87
     * long转为Integer
     * @param  object
tangying authored
88
89
     * @return
     */
90
    public static Integer getInteger(long object) {
tangying authored
91
92
93
94
        return Integer.valueOf(String.valueOf(object));
    }

    /**
95
96
     * 将对象转为Date
     * @param  object
tangying authored
97
98
     * @return
     */
99
100
    public static Date getDateTime(Object object) throws ParseException {
        if (object == null) {
tangying authored
101
            return null;
102
        } else {
tangying authored
103
104
105
106
107
108
            return format.parse(object.toString());
        }
    }

    /**
     * 通过一个实体类,给另一个实体类赋值
109
110
111
     * @param  source           源实体类
     * @param  target           目标实体类
     * @param  ignoreProperties 忽略字段(多个字段用逗号隔开)
tangying authored
112
113
     * @throws Exception
     */
114
115
116
    public static void CopyDataByNotNull(Object source, Object target, String ignoreProperties) throws Exception {
        Field[] sourceFields = source.getClass().getDeclaredFields();
        Field[] targetFields = target.getClass().getDeclaredFields();
tangying authored
117
118
        Field.setAccessible(targetFields, true);
119
        for (int i = 0; i < sourceFields.length; i++) {
tangying authored
120
121
122
123
124
125
126
127
128
129
130
131
132
133
            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
134
135
    /**
     * 格式化map
136
     * @param  map
mahuandong authored
137
138
     * @return
     */
139
    public static String sendGetFormat(Map<String, Object> map) {
tangying authored
140
mahuandong authored
141
142
        StringBuilder reslut = new StringBuilder();
        for (Map.Entry<String, Object> a : map.entrySet()) {
143
            reslut.append(a.getKey()).append("=").append(EncodingUtil.encodeURIComponent(String.valueOf(a.getValue())) + "&");
mahuandong authored
144
145
146
        }
        return String.valueOf(reslut);
    }
tangying authored
147
周鸿 authored
148
149
    /**
     * 流水码
150
     * 根据类型生成单号 规则:入库单类型 + 年月日 + 5(排序号 + 1)
周鸿 authored
151
152
153
154
     * @dataType code的前缀
     * @maxCode  code当前表的最后一个值
     * @return
     */
155
    public String createCode(String dataType, String maxCode) {
周鸿 authored
156
157
158
159
160
161
        String code = null;
        int i = 1;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now))) {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
162
            code = dataType + df.format(now) + String.format("%05d", Count + i++);
周鸿 authored
163
        } else {
164
            code = dataType + df.format(now) + String.format("%05d", 0000 + i++);
周鸿 authored
165
166
167
        }
        return code;
    }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

    /**
     * 流水码
     * 根据类型生成单号 规则:入库单类型 + 年月日 + 5(排序号 + 1)
     * @dataType code的前缀
     * @maxCode  code当前表的最后一个值
     * @return
     */
    public static String createUniqueId() {
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("MMdd");
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            int number = random.nextInt(10);
            sb.append(number);
        }
        String str = df.format(now) + sb.toString();
        return str;
    }

    public static void main(String[] args) {
        System.out.println(createUniqueId());
    }
tangying authored
193
}