提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import java.text.SimpleDateFormat; |
|
4 |
import java.util.Date; |
|
5 |
|
|
6 |
import javax.servlet.http.HttpServletRequest; |
|
7 |
import javax.servlet.http.HttpServletResponse; |
|
8 |
|
|
9 |
|
|
10 |
public class TenpayUtil { |
|
11 |
|
|
12 |
/** |
|
13 |
* �Ѷ���ת�����ַ� |
|
14 |
* @param obj |
|
15 |
* @return String ת�����ַ�,������Ϊnull,�ؿ��ַ�. |
|
16 |
*/ |
|
17 |
public static String toString(Object obj) { |
|
18 |
if(obj == null) |
|
19 |
return ""; |
|
20 |
|
|
21 |
return obj.toString(); |
|
22 |
} |
|
23 |
|
|
24 |
/** |
|
25 |
* �Ѷ���ת��Ϊint��ֵ. |
|
26 |
* |
|
27 |
* @param obj |
|
28 |
* �����ֵĶ���. |
|
29 |
* @return int ת�������ֵ,�Բ���ת���Ķ���0�� |
|
30 |
*/ |
|
31 |
public static int toInt(Object obj) { |
|
32 |
int a = 0; |
|
33 |
try { |
|
34 |
if (obj != null) |
|
35 |
a = Integer.parseInt(obj.toString()); |
|
36 |
} catch (Exception e) { |
|
37 |
|
|
38 |
} |
|
39 |
return a; |
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* ��ȡ��ǰʱ�� yyyyMMddHHmmss |
|
44 |
* @return String |
|
45 |
*/ |
|
46 |
public static String getCurrTime() { |
|
47 |
Date now = new Date(); |
|
48 |
SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
|
49 |
String s = outFormat.format(now); |
|
50 |
return s; |
|
51 |
} |
|
52 |
|
|
53 |
/** |
|
54 |
* ��ȡ��ǰ���� yyyyMMdd |
|
55 |
* @param date |
|
56 |
* @return String |
|
57 |
*/ |
|
58 |
public static String formatDate(Date date) { |
|
59 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); |
|
60 |
String strDate = formatter.format(date); |
|
61 |
return strDate; |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* ȡ��һ��ָ�����ȴ�С�����������. |
|
66 |
* |
|
67 |
* @param length |
|
68 |
* int �趨��ȡ�������ij��ȡ�lengthС��11 |
|
69 |
* @return int ������ɵ������ |
|
70 |
*/ |
|
71 |
public static int buildRandom(int length) { |
|
72 |
int num = 1; |
|
73 |
double random = Math.random(); |
|
74 |
if (random < 0.1) { |
|
75 |
random = random + 0.1; |
|
76 |
} |
|
77 |
for (int i = 0; i < length; i++) { |
|
78 |
num = num * 10; |
|
79 |
} |
|
80 |
return (int) ((random * num)); |
|
81 |
} |
|
82 |
|
|
83 |
/** |
|
84 |
* ��ȡ�����ַ� |
|
85 |
* @param request |
|
86 |
* @param response |
|
87 |
* @return String |
|
88 |
*/ |
|
89 |
public static String getCharacterEncoding(HttpServletRequest request, |
|
90 |
HttpServletResponse response) { |
|
91 |
|
|
92 |
if(null == request || null == response) { |
|
93 |
return "utf-8"; |
|
94 |
} |
|
95 |
|
|
96 |
String enc = request.getCharacterEncoding(); |
|
97 |
if(null == enc || "".equals(enc)) { |
|
98 |
enc = response.getCharacterEncoding(); |
|
99 |
} |
|
100 |
|
|
101 |
if(null == enc || "".equals(enc)) { |
|
102 |
enc = "utf-8"; |
|
103 |
} |
|
104 |
|
|
105 |
return enc; |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* ��ȡunixʱ�䣬��1970-01-01 00:00:00��ʼ������ |
|
110 |
* @param date |
|
111 |
* @return long |
|
112 |
*/ |
|
113 |
public static long getUnixTime(Date date) { |
|
114 |
if( null == date ) { |
|
115 |
return 0; |
|
116 |
} |
|
117 |
|
|
118 |
return date.getTime()/1000; |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* ʱ��ת�����ַ� |
|
123 |
* @param date ʱ�� |
|
124 |
* @param formatType ��ʽ������ |
|
125 |
* @return String |
|
126 |
*/ |
|
127 |
public static String date2String(Date date, String formatType) { |
|
128 |
SimpleDateFormat sdf = new SimpleDateFormat(formatType); |
|
129 |
return sdf.format(date); |
|
130 |
} |
|
131 |
|
|
132 |
} |