MyTools.java
15 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
*
*/
package com.bjivt.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.PropertyUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*
*/
public class MyTools {
public static final String FORMAT_CNDATE = "yyyy年MM月dd日";
public static final String FORMAT_DATE1 = "yyyy-MM-dd";
public static final String FORMAT_DATE2 = "yyyyMMdd";
public static final String FORMAT_DATE3 = "/yyyy/MM/dd/";
public static final String FORMAT_DATE4 = "yyyy.MM.dd";
public static final String FORMAT_DATE_TIME1 = "yyyy-MM-dd HH:mm";
public static final String FORMAT_DATE_TIME2 = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_DATE_TIME3 = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String ENCODE_GBK = "GBK";
public static final String ENCODE_ISO88591 = "ISO8859_1";
public static final String ENCODE_UTF8 = "UTF-8";
/**
* 格式化日期
* @param date 日期
* @param pattern 转换日期格式
* @return
*/
public String formatDate(Date date, String pattern) {
String returnValue = "";
pattern = clearNull(pattern).equals("") ? FORMAT_CNDATE : pattern;
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* 类型转换成Date类型
*
* @param argDate String 要转换的日期字符串
* @param pattern String 日期字符串str的匹配形式
* @return Timestamp 转换后的Timestamp
* @throws AppException 应用异常
*
*/
public Date getDateFormatByString(String argDate, String pattern) {
java.util.Date dt = null;
try {
//int lng = argDate.trim().length();
java.text.SimpleDateFormat objSDF = new java.text.SimpleDateFormat(pattern);
//objSDF.applyPattern(pattern.trim().substring(0, lng));
if(!clearNull(argDate).equals("")) {
dt = new java.util.Date(objSDF.parse(argDate).getTime());
}
} catch (ParseException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return dt;
}
/**
* 检测字符串值,如果值为null,则返回空字符串
*
* @String checkValue 源字符串
* @return String 返回修改后的字符串
*/
public String clearNull(Object checkValue) {
if (checkValue == null) {
return "";
} else {
return checkValue.toString();
}
}
/**
* 检测字符串值,如果值为null,则返回字符串0
*
* @String checkValue 源字符串
* @return String 返回修改后的字符串
*/
public String clearNullNumber(Object checkValue) {
if (clearNull(checkValue).equals("")) {
return "0";
} else {
return checkValue.toString();
}
}
/**
* 获取session属性值
* @param req
* @param sessionKey 属性名称
* @return
*/
public String getSessionAttribute(HttpServletRequest req, String sessionKey) {
return clearNull(req.getSession().getAttribute(sessionKey));
}
/**
* 获取request属性值
* @param req
* @param paramKey 属性名称
* @return
*/
public String getRequestParam(HttpServletRequest req, String paramKey) {
//System.out.println("@@@@@@@MyTools:"+parseStrEncodeIsoUtf(req.getParameter(paramKey)));
// return clearNull(parseStrEncodeIsoUtf(req.getParameter(paramKey)));
//处理编码改为过滤器 2014-01-12
return clearNull(req.getParameter(paramKey));
}
/**
* 获取request属性值
* @param req
* @param paramKey 属性名称
* @return
*/
public String getRequestParam2(HttpServletRequest req, String paramKey) {
// String re = clearNull(parseStrEncodeIsoUtf(req.getParameter(paramKey)));
String re = clearNull(req.getParameter(paramKey));
if(re.equals("")) {
return null;
}
if(!isChinese(re)) {
re = clearNull(req.getParameter(paramKey));
}
return re;
}
/**
* 获取Ajax request属性值
* @param req
* @param paramKey 属性名称
* @return
*/
public String getAjaxParam(HttpServletRequest req, String paramKey) {
return clearNull(req.getParameter(paramKey));
}
/**
* 获取Ajax request属性值
* @param req
* @param paramKey 属性名称
* @return
*/
public String getAjaxParam2(HttpServletRequest req, String paramKey) {
String re = clearNull(req.getParameter(paramKey));
// String re = clearNull(parseStrEncodeIsoUtf(req.getParameter(paramKey)));
if(re.equals("")) {
return null;
}
if(!isChinese(re)) {
re = clearNull(req.getParameter(paramKey));
}
return re;
}
/**
* 字节数组转换字符串
* @param b
* @return
*/
public String byteArrayToString(byte[] b) {
BASE64Encoder enc = new BASE64Encoder();
return enc.encode(b);
}
/**
* 字符串转换字节数组
* @param str
* @return
*/
public byte[] stringToByteArray(String str) {
BASE64Decoder dec = new BASE64Decoder();
try {
return dec.decodeBuffer(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new byte[]{};
}
}
/**
* 从GBK转为ISO8859_1
* @param str String
* @return String
*/
public String parseStrEncodeGBKIso(String str) {
return parseStrEncode(str, MyTools.ENCODE_GBK, MyTools.ENCODE_ISO88591);
}
/**
* 从ISO8859_1转为GBK
* @param str String
* @return String
*/
public String parseStrEncodeIsoGBK(String str) {
return parseStrEncode(str, MyTools.ENCODE_ISO88591, MyTools.ENCODE_GBK);
}
/**
* 从ISO8859_1转为utf-8
* @param str String
* @return String
*/
public String parseStrEncodeIsoUtf(String str) {
return parseStrEncode(str, MyTools.ENCODE_ISO88591, MyTools.ENCODE_UTF8);
}
/**
* 从utf-8转为ISO8859_1
* @param str String
* @return String
*/
public String parseStrEncodeUtfIso(String str) {
return parseStrEncode(str, MyTools.ENCODE_UTF8, MyTools.ENCODE_ISO88591);
}
/**
* 字符串编码转换
* @param str String
* @param fromEncode 现在编码
* @param toEncode String 要转的编码
* @return String
*/
public String parseStrEncode(String str, String fromEncode, String toEncode) {
if(str == null) {
return null;
}
try {
if(clearNull(fromEncode).equals("")) {
return str;
}
else if(clearNull(toEncode).equals("")) {
str = new String(str.getBytes(fromEncode));
}
else {
str = new String(str.getBytes(fromEncode),toEncode);
}
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
return str;
}
/**
* 转换当前页为当前行数
* @param rowNum 每页行数
* @param curRow 当前页
* @return
*/
public String parseCurrentRow(String rowNum, String curRow) {
int curPage = Integer.parseInt(clearNullNumber(curRow));
if(curPage <= 0) {
curPage = 0;
}
else {
curPage = curPage - 1;
}
curRow = String.valueOf(curPage * Integer.parseInt(rowNum));
return curRow;
}
/**
* 转换当前页为当前行数
* @param curPage 当前页
* @param maxPage 最大页
* @return
*/
public String parseCurrentPage(String curPage, int maxPage) {
int cp = Integer.parseInt(clearNullNumber(curPage));
if(cp <= 1) {
cp = 1;
}
else if(cp >= maxPage){
cp = maxPage;
}
return String.valueOf(cp);
}
public int getMaxPage(int totalCount, int rowNum) {
Double __tp = new Double(0);
Double _totalCount = Double.valueOf(totalCount);
Double _rowNum = Double.valueOf(rowNum);
if(rowNum > 0) {
__tp = _totalCount / _rowNum;
}
Double _tp = Double.valueOf(Math.ceil(__tp));
return _tp.intValue();
}
/**
* 取随机数
* @param length
* @return
*/
public String getRandomString(int length){
StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
StringBuffer sb = new StringBuffer();
java.util.Random r = new java.util.Random();
int range = buffer.length();
for (int i = 0; i < length; i ++)
{
sb.append(buffer.charAt(r.nextInt(range)));
}
return sb.toString();
}
/**
* 复制been
* @param fromBeen
* @param toBeen
* @throws Exception
*/
public void copyBeen(Object fromBeen,Object toBeen) throws Exception{
Method[] fromBeenMethod = fromBeen.getClass().getMethods();
Method[] toBeenMethod = toBeen.getClass().getMethods();
for(int i=0; i<fromBeenMethod.length; i++) {
String fromName = fromBeenMethod[i].getName();
if(fromName.startsWith("get")) {
String _fromName = fromName.substring(3, fromName.length());
for(int j=0; j<toBeenMethod.length; j++) {
String toName = toBeenMethod[j].getName();
if(toName.startsWith("set")) {
String _toName = toName.substring(3, toName.length());
if(_fromName.equals(_toName)) {
Method fromMethod = fromBeenMethod[i];
Method toMethod = toBeenMethod[j];
toMethod.invoke(toBeen, new Object[] {fromMethod.invoke(fromBeen, new Object[0])});
}
}
}
}
}
}
/**
* 判断有没有中文
* @param c
* @return
*/
public boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* 判断有没有中文
* @param strName
* @return
*/
public boolean isChinese(String strName) {
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c) == true) {
return true;
}
}
return false;
}
/**
* ip校验
* @param s
* @return
*/
public boolean isIpAddress(String s){
String regex = "(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
return m.matches();
}
/**
* 获取客户端ip
* @param request
* @return
*/
public String getClientAddress(HttpServletRequest req) {
String address = req.getHeader("X-Forwarded-For");
if (address != null && isIpAddress(address)) {
return address;
}
return req.getRemoteAddr();
}
public Integer getRandomNum(Integer beginIndex,Integer endIndex){
Random r = new Random();
Integer num = r.nextInt(endIndex-beginIndex+1)+beginIndex;
return num;
}
/**
* 读取properties
* @param fileName
* @return
*/
public Properties getProperties(String fileName){
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
return p;
}
/**
* 判断是否数字类型
* @param str
* @return
*/
public static boolean isNumeric1(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
/**
*
* @Description: 通过经纬度获得两点之间距离
* @param lat_a a点维度
* @param lng_a a点经度
* @param lat_b b点维度
* @param lng_b b点经度
* @return
* @return String 两点之间距离 km
* @throws
* @author JASON.CHEN
* @date 2016年3月13日
*/
public static Double getDistanceFromXtoY(Double lat_a,Double lng_a, Double lat_b, Double lng_b){
try {
double pk = 180 / 3.14169 ;
double a1 = lat_a/ pk ;
double a2 = lng_a/ pk ;
double b1 = lat_b/ pk ;
double b2 = lng_b / pk ;
double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2) ;
double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2) ;
double t3 = Math.sin(a1) * Math.sin(b1) ;
double tt = Math.acos(t1 + t2 + t3) ;
return new BigDecimal(tt*6370996.81/1000).setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
} catch (Exception e) {
return 999999d;
}
}
private final static int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22 };
private final static String[] constellationArr = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座" };
/**
*
* @author JASON_CHEN
*
* @desc:根据生日获得星座
* String
* @param birthday
* @param split
* @return
* 2016年3月25日
*/
public static String getConstellation(String birthday,String split) {
int month=Integer.valueOf(birthday.split(split)[1]);
int day=Integer.valueOf(birthday.split(split)[2]);
return day < dayArr[month - 1] ? constellationArr[month - 1] : constellationArr[month];
}
/**
* bean相同字段copy赋值
*
* @param toObject
* @param fromObject
* @return 赋值后的toObject
*/
public static Object copyProperties(Object toObject, Object fromObject) {
if (fromObject == null)
return null;
if (toObject == null)
return null;
try {
PropertyUtils.copyProperties(toObject, fromObject);
} catch (Exception e) {
e.printStackTrace();
}
return toObject;
}
public static void main(String arg[]){
/*MyTools tools = new MyTools();
Integer num = tools.getRandomNum(0, 2);
System.out.println("num:"+num);*/
//System.out.println(getDistanceFromXtoY(39.91164592820338,116.4015939609623,39.91839082993028,116.39417064209509));
//System.out.println(getDistanceFromXtoY(39.91839082993028,116.39417064209509,39.91164592820338,116.4015939609623));
String ss="2022-3-02";
System.out.println(getConstellation(ss,"-"));
}
}