DataUtils.java
5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
149
150
151
152
153
154
155
156
157
158
159
160
161
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
187
188
189
190
191
192
193
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;
import java.util.Map;
import java.util.Random;
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) {
if (object == null) {
return null;
} else {
return object.toString();
}
}
/**
* 将对象转为Integer
* @param object
* @return
*/
public static Integer getInteger(Object object) {
if (object == null) {
return null;
} else {
return Integer.valueOf(object.toString());
}
}
/**
* 将对象转为BigDecimal
* @param object
* @return
*/
public static BigDecimal getBigDecimal(Object object) {
if (object == null) {
return null;
} else {
return new BigDecimal(object.toString());
}
}
/**
* 将对象转为Double
* @param object
* @return
*/
public static Double getDouble(Object object) {
if (object == null) {
return null;
} else {
return new Double(object.toString());
}
}
/**
* 将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;
}
}
}
}
/**
* 格式化map
* @param map
* @return
*/
public static String sendGetFormat(Map<String, Object> map) {
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);
}
/**
* 流水码
* 根据类型生成单号 规则:入库单类型 + 年月日 + 5位(排序号 + 1)
* @dataType code的前缀
* @maxCode code当前表的最后一个值
* @return
*/
public String createCode(String dataType, String maxCode) {
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()));
code = dataType + df.format(now) + String.format("%05d", Count + i++);
} else {
code = dataType + df.format(now) + String.format("%05d", 0000 + i++);
}
return code;
}
/**
* 流水码
* 根据类型生成单号 规则:入库单类型 + 年月日 + 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());
}
}