|
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;
|
|
8
|
import java.util.Map;
|
|
9
|
import java.util.Random;
|
|
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 方法后缀
|
|
19
20
21
22
|
* @return
*/
public static String ConvertMethodName(String filedName, String methodSuffix) {
String returnValue = null;
|
|
23
|
if (filedName.length() > 0) {
|
|
24
25
|
char[] nameChar = filedName.toCharArray();
if (nameChar[0] >= 'a' && nameChar[0] <= 'z') {
|
|
26
|
nameChar[0] = (char)(nameChar[0] - 32);
|
|
27
|
}
|
|
28
|
returnValue = "and" + String.valueOf(nameChar) + methodSuffix;
|
|
29
30
31
32
33
|
}
return returnValue;
}
/**
|
|
34
35
|
* 将对象转为字符串
* @param object
|
|
36
37
|
* @return
*/
|
|
38
|
public static String getString(Object object) {
|
|
39
40
41
42
43
|
if (object == null) {
return null;
} else {
return object.toString();
}
|
|
44
45
46
|
}
/**
|
|
47
48
|
* 将对象转为Integer
* @param object
|
|
49
50
|
* @return
*/
|
|
51
|
public static Integer getInteger(Object object) {
|
|
52
53
54
55
56
|
if (object == null) {
return null;
} else {
return Integer.valueOf(object.toString());
}
|
|
57
58
59
|
}
/**
|
|
60
61
|
* 将对象转为BigDecimal
* @param object
|
|
62
63
|
* @return
*/
|
|
64
|
public static BigDecimal getBigDecimal(Object object) {
|
|
65
66
67
68
69
|
if (object == null) {
return null;
} else {
return new BigDecimal(object.toString());
}
|
|
70
71
72
|
}
/**
|
|
73
74
|
* 将对象转为Double
* @param object
|
|
75
76
|
* @return
*/
|
|
77
|
public static Double getDouble(Object object) {
|
|
78
79
80
81
82
|
if (object == null) {
return null;
} else {
return new Double(object.toString());
}
|
|
83
84
85
|
}
/**
|
|
86
87
|
* 将long转为Integer
* @param object
|
|
88
89
|
* @return
*/
|
|
90
|
public static Integer getInteger(long object) {
|
|
91
92
93
94
|
return Integer.valueOf(String.valueOf(object));
}
/**
|
|
95
96
|
* 将对象转为Date
* @param object
|
|
97
98
|
* @return
*/
|
|
99
100
|
public static Date getDateTime(Object object) throws ParseException {
if (object == null) {
|
|
101
|
return null;
|
|
102
|
} else {
|
|
103
104
105
106
107
108
|
return format.parse(object.toString());
}
}
/**
* 通过一个实体类,给另一个实体类赋值
|
|
109
110
111
|
* @param source 源实体类
* @param target 目标实体类
* @param ignoreProperties 忽略字段(多个字段用逗号隔开)
|
|
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();
|
|
117
118
|
Field.setAccessible(targetFields, true);
|
|
119
|
for (int i = 0; i < sourceFields.length; i++) {
|
|
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;
}
}
}
}
|
|
134
135
|
/**
* 格式化map
|
|
136
|
* @param map
|
|
137
138
|
* @return
*/
|
|
139
|
public static String sendGetFormat(Map<String, Object> map) {
|
|
140
|
|
|
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())) + "&");
|
|
144
145
146
|
}
return String.valueOf(reslut);
}
|
|
147
|
|
|
148
149
|
/**
* 流水码
|
|
150
|
* 根据类型生成单号 规则:入库单类型 + 年月日 + 5位(排序号 + 1)
|
|
151
152
153
154
|
* @dataType code的前缀
* @maxCode code当前表的最后一个值
* @return
*/
|
|
155
|
public String createCode(String dataType, String maxCode) {
|
|
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++);
|
|
163
|
} else {
|
|
164
|
code = dataType + df.format(now) + String.format("%05d", 0000 + i++);
|
|
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());
}
|
|
193
|
}
|