1. import java.text.SimpleDateFormat;
2. import java.text.ParseException;
3. import java.util.Calendar;
4. import java.util.Date;
5. import java.text.DecimalFormat;
6.
7. import org.apache.commons.lang.StringUtils;
8.
9. public class StrTool {
10.
11. /**
12. * 日期格式化
13. *
14. * @param date
15. * Date
16. * @param formatstr
17. * String
18. * @return String
19. */
20. public static String formatDate(java.util.Date date, String formatstr) {
21. if (date == null || formatstr == null)
22. return null;
23. java.text.SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr);
24. return dateFormat.format(date);
25. }
26.
27. public static String formatDate(java.util.Date date) {
28. return formatDate(date, "yyyy-MM-dd");
29. }
30.
31. /**
32. * 格式当前日期
33. *
34. * @return String
35. */
36. public static String formatDateByNow() {
37. return formatDate(new java.util.Date());
38. }
39.
40. public static String formatTime(java.util.Date date) {
41. return formatDate(date, "kk:mm:ss");
42. }
43.
44. public static String formatDateTime(java.util.Date date) {
45. return formatDate(date, "yyyy-MM-dd kk:mm");
46. }
47.
48. /**
49. * 解释日期yyyy-MM-dd kk:mm
50. *
51. * @param strdate
52. * String
53. * @return Date
54. * @throws ParseException
55. */
56. public static java.util.Date parseDateTime(String strdate)
57. throws ParseException {
58. return parseDateTimeByFormatstr(strdate, "yyyy-MM-dd kk:mm");
59. }
60.
61. /**
62. * 解释日期yyyy-MM-dd
63. *
64. * @param strdate
65. * String
66. * @return Date
67. * @throws ParseException
68. */
69. public static java.util.Date parseDate(String strdate)
70. throws ParseException {
71. return parseDateTimeByFormatstr(strdate, "yyyy-MM-dd");
72. }
73.
74. /**
75. * 根据传入的格式化字串来解释日期
76. *
77. * @param strdate
78. * String
79. * @param formatstr
80. * String
81. * @return Date
82. * @throws ParseException
83. */
84. public static java.util.Date parseDateTimeByFormatstr(String strdate,
85. String formatstr) throws ParseException {
86. java.text.SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr);
87. return dateFormat.parse(strdate);
88. }
89.
90. /**
91. * 格式数字类型
92. *
93. * @param f
94. * float
95. * @param sacl
96. * int
97. * @return String
98. */
99. public static String formatNumber(double f, String pattern) {
100. java.text.DecimalFormat num = new DecimalFormat(pattern);
101. return num.format(f);
102. }
103.
104. /**
105. * 格式化为钱格式显示如:1,234,555.00
106. *
107. * @param money
108. * double
109. * @return String
110. */
111. public static String formatMoney(double money) {
112. return formatNumber(money, "#,##0.00");
113. }
114.
115. public static String indexUpStr(String str) {
116. if (str == null || str.length() < 1) {
117. return str;
118. }
119. String tmstr = "";
120. tmstr = str.substring(0, 1);
121. tmstr = tmstr.toUpperCase();
122. tmstr += str.substring(1);
123. return tmstr;
124. }
125.
126. /**
127. * 根据分隔符,将数字字符转换成整型数组
128. *
129. * @param str
130. * String
131. * @param regex
132. * String
133. * @return int[]
134. */
135. public static int[] toIntsByRegex(String str, String regex) {
136. if (str == null || "".equals(str.trim()))
137. return null;
138. String[] tms = str.split(regex);
139. int[] tmi = new int[tms.length];
140. for (int i = 0; i < tms.length; i++) {
141. tmi[i] = Integer.parseInt(tms[i]);
142. }
143. return tmi;
144. }
145.
146. public static int[] toInts(String str) {
147. return toIntsByRegex(str, ",");
148. }
149.
150. /**
151. * 将格式化的数字还原,如:1,234,567.00——>1234567.00
152. *
153. * @param formattedNumber
154. * @return parseString
155. */
156. public static String parseFormattedNumber(String formattedNumber) {
157. java.text.DecimalFormat num = new DecimalFormat();
158.
159. String parseString = "";
160. try {
161. parseString = formatNumber(Double.parseDouble(num.parse(
162. formattedNumber).toString()), "#0.00");
163. } catch (ParseException e) {
164. e.printStackTrace();
165. }
166. return parseString;
167. }
168.
169. public static void main(String[] args) throws ParseException {
170. // System.out.println(indexUpStr("")+";");
171. // toIntsByRegex(" ",",");
172.
173. // Date date = parseDateTime("2004-04-12 13:24");
174. // System.out.println(formatDateTime(date));
175. java.text.DecimalFormat num = new DecimalFormat();
176. System.out.println(formatNumber(5234556, "#,##0.00%"));
177. System.out.println(parseFormattedNumber("99999999"));
178. System.out.println("interval: "
179. + getIntervalOfDate("2005-05-01", "2006-06-12"));
180. }
181.
182. /**
183. * 将字符串数组转化为字符串,并用","连接
184. *
185. * @author 李奉学
186. * @param arrayString
187. * @return 带","分割的字符串
188. */
189. public static String convertArrayToString(String[] arrayString) {
190. String returnString = "";
191.
192. if (arrayString != null) {
193. for (int i = 0; i < arrayString.length; i++) {
194. // 第一个元素前不能加","
195. if (StringUtils.isEmpty(returnString)) {
196. returnString = returnString + arrayString[i];
197. } else {
198. returnString = returnString + "," + arrayString[i];
199. }
200. }
201. }
202. return returnString;
203. }
204.
205. public static String getFileExt(String filename) {
206. String fileext = "";
207. String tmfilename = filename;
208. if (tmfilename != null) {
209. int l = tmfilename.indexOf(".") + 1;
210. if (l == 0) {
211. return "";
212. }
213. int tml = l;
214. while (tml != 0) {
215. try {
216. l = tml;
217. tml = tmfilename.indexOf(".", tml) + 1;
218. } catch (Exception e) {
219. tml = 0;
220. }
221. }
222. int length = tmfilename.length();
223. if (l <= length && l != 1) {
224. fileext = tmfilename.substring(l);
225. } else {
226. fileext = "";
227. }
228. }
229. return fileext;
230. }
231.
232. /**
233. * 补齐字符长度
234. *
235. * @param str
236. * @param length
237. * @param repair
238. * @return
239. */
240. public static String fillStrLength(String str, int length, String repair) {
241. for (int i = str.length(); i < length; i++) {
242. str = repair + str;
243. }
244. return str;
245. }
246.
247. /**
248. * 比较字符串大小
249. *
250. * @param str1
251. * @param str2
252. * @return
253. */
254. public static int strComp(String str1, String str2) {
255. if (str1 == null || str2 == null) {
256. return 0;
257. }
258. int str1Length = str1.length();
259. int str2Length = str2.length();
260. if (str2Length > str1Length) {
261. str1 = fillStrLength(str1, str2Length, "0");
262. } else if (str2Length < str1Length) {
263. str1 = fillStrLength(str2, str1Length, "0");
264. }
265. int result = str1.compareTo(str2);
266. if (result >= 1)
267. result = 1;
268. if (result <= -1)
269. result = -1;
270. return result;
271. }
272.
273. /**
274. * xuxu 返回毫秒
275. *
276. * @param date
277. * @return
278. */
279. public static long getMillis(Date date) {
280. Calendar c = Calendar.getInstance();
281. c.setTime(date);
282. return c.getTimeInMillis();
283. }
284.
285. /**
286. * 日期相减
287. *
288. * @param date
289. * 日期
290. * @param day
291. * 天数
292. * @return
293. */
294. public static Date subDate(Date date, int day) {
295. Calendar c = Calendar.getInstance();
296. c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
297. return c.getTime();
298. }
299.
300. /**
301. * 返回同一年内两个日期相差的天数
302. *
303. * @author 李奉学
304. * @param date1
305. * @param date2
306. * @return 相差的天数
307. * @throws ParseException
308. */
309. public static int getIntervalOfDate(String date1, String date2)
310. throws ParseException {
311. int interval = 0;
312. // 设置日期
313. Calendar cal1 = Calendar.getInstance();
314. cal1.setTime(parseDate(date1));
315.
316. Calendar cal2 = Calendar.getInstance();
317. cal2.setTime(parseDate(date2));
318.
319. // 相差的天数
320. interval = Math.abs(cal1.get(Calendar.DAY_OF_YEAR)
321. - cal2.get(Calendar.DAY_OF_YEAR));
322.
323. return interval;
324. }
325.
326. /**
327. * 当前日期在一年中的周序号
328. *
329. * @param date
330. * @return 周序号
331. * @throws ParseException
332. */
333. public static int getWeekIndexOfYear(String date) throws ParseException {
334. int index = -1;
335.
336. Calendar cal = Calendar.getInstance();
337. cal.setTime(parseDate(date));
338.
339. index=cal.get(Calendar.WEEK_OF_YEAR);
340.
341. return index;
342. }
343.
344. }
2. import java.text.ParseException;
3. import java.util.Calendar;
4. import java.util.Date;
5. import java.text.DecimalFormat;
6.
7. import org.apache.commons.lang.StringUtils;
8.
9. public class StrTool {
10.
11. /**
12. * 日期格式化
13. *
14. * @param date
15. * Date
16. * @param formatstr
17. * String
18. * @return String
19. */
20. public static String formatDate(java.util.Date date, String formatstr) {
21. if (date == null || formatstr == null)
22. return null;
23. java.text.SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr);
24. return dateFormat.format(date);
25. }
26.
27. public static String formatDate(java.util.Date date) {
28. return formatDate(date, "yyyy-MM-dd");
29. }
30.
31. /**
32. * 格式当前日期
33. *
34. * @return String
35. */
36. public static String formatDateByNow() {
37. return formatDate(new java.util.Date());
38. }
39.
40. public static String formatTime(java.util.Date date) {
41. return formatDate(date, "kk:mm:ss");
42. }
43.
44. public static String formatDateTime(java.util.Date date) {
45. return formatDate(date, "yyyy-MM-dd kk:mm");
46. }
47.
48. /**
49. * 解释日期yyyy-MM-dd kk:mm
50. *
51. * @param strdate
52. * String
53. * @return Date
54. * @throws ParseException
55. */
56. public static java.util.Date parseDateTime(String strdate)
57. throws ParseException {
58. return parseDateTimeByFormatstr(strdate, "yyyy-MM-dd kk:mm");
59. }
60.
61. /**
62. * 解释日期yyyy-MM-dd
63. *
64. * @param strdate
65. * String
66. * @return Date
67. * @throws ParseException
68. */
69. public static java.util.Date parseDate(String strdate)
70. throws ParseException {
71. return parseDateTimeByFormatstr(strdate, "yyyy-MM-dd");
72. }
73.
74. /**
75. * 根据传入的格式化字串来解释日期
76. *
77. * @param strdate
78. * String
79. * @param formatstr
80. * String
81. * @return Date
82. * @throws ParseException
83. */
84. public static java.util.Date parseDateTimeByFormatstr(String strdate,
85. String formatstr) throws ParseException {
86. java.text.SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr);
87. return dateFormat.parse(strdate);
88. }
89.
90. /**
91. * 格式数字类型
92. *
93. * @param f
94. * float
95. * @param sacl
96. * int
97. * @return String
98. */
99. public static String formatNumber(double f, String pattern) {
100. java.text.DecimalFormat num = new DecimalFormat(pattern);
101. return num.format(f);
102. }
103.
104. /**
105. * 格式化为钱格式显示如:1,234,555.00
106. *
107. * @param money
108. * double
109. * @return String
110. */
111. public static String formatMoney(double money) {
112. return formatNumber(money, "#,##0.00");
113. }
114.
115. public static String indexUpStr(String str) {
116. if (str == null || str.length() < 1) {
117. return str;
118. }
119. String tmstr = "";
120. tmstr = str.substring(0, 1);
121. tmstr = tmstr.toUpperCase();
122. tmstr += str.substring(1);
123. return tmstr;
124. }
125.
126. /**
127. * 根据分隔符,将数字字符转换成整型数组
128. *
129. * @param str
130. * String
131. * @param regex
132. * String
133. * @return int[]
134. */
135. public static int[] toIntsByRegex(String str, String regex) {
136. if (str == null || "".equals(str.trim()))
137. return null;
138. String[] tms = str.split(regex);
139. int[] tmi = new int[tms.length];
140. for (int i = 0; i < tms.length; i++) {
141. tmi[i] = Integer.parseInt(tms[i]);
142. }
143. return tmi;
144. }
145.
146. public static int[] toInts(String str) {
147. return toIntsByRegex(str, ",");
148. }
149.
150. /**
151. * 将格式化的数字还原,如:1,234,567.00——>1234567.00
152. *
153. * @param formattedNumber
154. * @return parseString
155. */
156. public static String parseFormattedNumber(String formattedNumber) {
157. java.text.DecimalFormat num = new DecimalFormat();
158.
159. String parseString = "";
160. try {
161. parseString = formatNumber(Double.parseDouble(num.parse(
162. formattedNumber).toString()), "#0.00");
163. } catch (ParseException e) {
164. e.printStackTrace();
165. }
166. return parseString;
167. }
168.
169. public static void main(String[] args) throws ParseException {
170. // System.out.println(indexUpStr("")+";");
171. // toIntsByRegex(" ",",");
172.
173. // Date date = parseDateTime("2004-04-12 13:24");
174. // System.out.println(formatDateTime(date));
175. java.text.DecimalFormat num = new DecimalFormat();
176. System.out.println(formatNumber(5234556, "#,##0.00%"));
177. System.out.println(parseFormattedNumber("99999999"));
178. System.out.println("interval: "
179. + getIntervalOfDate("2005-05-01", "2006-06-12"));
180. }
181.
182. /**
183. * 将字符串数组转化为字符串,并用","连接
184. *
185. * @author 李奉学
186. * @param arrayString
187. * @return 带","分割的字符串
188. */
189. public static String convertArrayToString(String[] arrayString) {
190. String returnString = "";
191.
192. if (arrayString != null) {
193. for (int i = 0; i < arrayString.length; i++) {
194. // 第一个元素前不能加","
195. if (StringUtils.isEmpty(returnString)) {
196. returnString = returnString + arrayString[i];
197. } else {
198. returnString = returnString + "," + arrayString[i];
199. }
200. }
201. }
202. return returnString;
203. }
204.
205. public static String getFileExt(String filename) {
206. String fileext = "";
207. String tmfilename = filename;
208. if (tmfilename != null) {
209. int l = tmfilename.indexOf(".") + 1;
210. if (l == 0) {
211. return "";
212. }
213. int tml = l;
214. while (tml != 0) {
215. try {
216. l = tml;
217. tml = tmfilename.indexOf(".", tml) + 1;
218. } catch (Exception e) {
219. tml = 0;
220. }
221. }
222. int length = tmfilename.length();
223. if (l <= length && l != 1) {
224. fileext = tmfilename.substring(l);
225. } else {
226. fileext = "";
227. }
228. }
229. return fileext;
230. }
231.
232. /**
233. * 补齐字符长度
234. *
235. * @param str
236. * @param length
237. * @param repair
238. * @return
239. */
240. public static String fillStrLength(String str, int length, String repair) {
241. for (int i = str.length(); i < length; i++) {
242. str = repair + str;
243. }
244. return str;
245. }
246.
247. /**
248. * 比较字符串大小
249. *
250. * @param str1
251. * @param str2
252. * @return
253. */
254. public static int strComp(String str1, String str2) {
255. if (str1 == null || str2 == null) {
256. return 0;
257. }
258. int str1Length = str1.length();
259. int str2Length = str2.length();
260. if (str2Length > str1Length) {
261. str1 = fillStrLength(str1, str2Length, "0");
262. } else if (str2Length < str1Length) {
263. str1 = fillStrLength(str2, str1Length, "0");
264. }
265. int result = str1.compareTo(str2);
266. if (result >= 1)
267. result = 1;
268. if (result <= -1)
269. result = -1;
270. return result;
271. }
272.
273. /**
274. * xuxu 返回毫秒
275. *
276. * @param date
277. * @return
278. */
279. public static long getMillis(Date date) {
280. Calendar c = Calendar.getInstance();
281. c.setTime(date);
282. return c.getTimeInMillis();
283. }
284.
285. /**
286. * 日期相减
287. *
288. * @param date
289. * 日期
290. * @param day
291. * 天数
292. * @return
293. */
294. public static Date subDate(Date date, int day) {
295. Calendar c = Calendar.getInstance();
296. c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
297. return c.getTime();
298. }
299.
300. /**
301. * 返回同一年内两个日期相差的天数
302. *
303. * @author 李奉学
304. * @param date1
305. * @param date2
306. * @return 相差的天数
307. * @throws ParseException
308. */
309. public static int getIntervalOfDate(String date1, String date2)
310. throws ParseException {
311. int interval = 0;
312. // 设置日期
313. Calendar cal1 = Calendar.getInstance();
314. cal1.setTime(parseDate(date1));
315.
316. Calendar cal2 = Calendar.getInstance();
317. cal2.setTime(parseDate(date2));
318.
319. // 相差的天数
320. interval = Math.abs(cal1.get(Calendar.DAY_OF_YEAR)
321. - cal2.get(Calendar.DAY_OF_YEAR));
322.
323. return interval;
324. }
325.
326. /**
327. * 当前日期在一年中的周序号
328. *
329. * @param date
330. * @return 周序号
331. * @throws ParseException
332. */
333. public static int getWeekIndexOfYear(String date) throws ParseException {
334. int index = -1;
335.
336. Calendar cal = Calendar.getInstance();
337. cal.setTime(parseDate(date));
338.
339. index=cal.get(Calendar.WEEK_OF_YEAR);
340.
341. return index;
342. }
343.
344. }