- 浏览: 39582 次
- 性别:
- 来自: 南京
文章分类
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. }
发表评论
-
java String和Date转换,以及时间的比较
2009-10-30 20:59 2571SimpleDateFormat函数语法: ... -
零碎小知识
2009-10-18 22:50 803Java读写文件。二进制类用InputStream/Outpu ... -
全面分析Java的垃圾回收机制
2009-10-17 13:17 613Java的堆是一个运 ... -
Java 多线程之同步并发基础
2009-10-14 23:32 1609一: 慎用volatile关键字 volatile关 ... -
Java多线程之基础操作
2009-10-14 20:54 1534参考自http://developer.51cto.com/a ... -
StringBuffer类及线程运行测试
2009-10-12 09:37 884虽然平时常常看到,毕竟很少用。居然在笔试的时候碰到了,而且还写 ... -
Java手动抛异常
2009-10-12 09:10 12503想不到居然忘了如何自动抛出异常,汗,幸好有阿润帮忙,so re ...
相关推荐
js通用日期格式化工具,直接引用该工具类就可以使用,方便快捷,建议大家收藏此工具类,该工具很好的解决了前端页面显示日期格式问题
包含了货币工具类(将数字货币转成大写)、日期工具类(日期格式化、日期相加减、日期差等)、半角和全角字符相互转换、JSON工具类(利用gson工具包)、字符串工具类(非空判断等)
日期操作是日常开发当中最常见的操作之一,故开放下自己封装的常用工具操作方法。 在java中队日期类型的处理并不方便,通常都需要借助java.text.SimpleDateFormat类来实现日期类型 和字符串类型之间的转换,但是在...
Java日期工具类 1、日期的各种格式化 2、获取当前年度 3、获取当前月份 4、获取当前天号数 5、获取当前时间往前推一个月的时间 6、获取上个月("MMM yyyy") 7、获取上个月("yyyymm") 8、获取上个月("yyyy-mm") 9、...
无需安装,直接运行,将json格式的数据,复制粘贴到里面,点击Format工具,就自动格式化,如果json格式有问题,也会报红线,给提示。
时间格式化及获取特殊时间工具类 主要包含但不限于: 当前星期的第一天 当前月最后一天 获取时间到今日结束还剩多少秒 获取 是否大于当前时间 相关的交易日 (年处理)入参yyyy 查询距离当前系统时间的本周星期一、...
* 在工具类中经常使用到工具类的格式化描述,这个主要是一个日期的操作类,所以日志格式主要使用 SimpleDateFormat的定义格式. * * 格式的意义如下: 日期和时间模式 * 日期和时间格式由日期和时间模式...
该文件主要实现iOS日期格式化输出,输出YYYY-MM-dd HH:mm:ss、或者字符串作为文件名称(保证不重名)
在Java程序设计过程中,对应日期和时间的格式化,还有一个简单的格式化方式,就是java.text.SimpleDateFormat,该类中用字符串指定日期和时间的格式,字符串中的字符称为模式字符,模式字符区分大小写。常见的模式...
一个php一个常用工具类,js 弹窗并且跳转,js 弹窗返回 ,页面跳转,弹窗关闭,系统基本参数上传图片专用,数据库输入过滤,清理session,验证是否为空,验证是否相同,格式化字符串,格式化时间,获得真实IP地址,...
1.字符串转换为其他类型 2.字符串首字母转换为大写 3.取得一个随机字符串,包含数字和字符 4.转码 5.替换特殊字符串 6.公式格式化
主要讲解了System类、Object类、Arrays类、Cloneable接口、IO系统输入输出类及装饰类、IO系统文本读写工具类、IO系统二进制读写工具类、对象序列化工具类、File类及文件搜索工具类、java异常机制及自定义异常类、...
java常用日期工具类方法. 包含格式化日期,字符串转日期格式,日期格式转字符串,获取当天是本月第几天、是当年多少天等
HiJson官方版是一款Json格式化工具,HiJson官方版用于格式化JSON,XML转JSON,MAP转JSON,格式化XML,节点查找及文本查找,以树的形式展示,方便json字符串查看,支持各种常用操作,支持多标签。(解压:4efcb17e4b...
JS常用工具函数(压缩包中含有MD文件可自行查看)--包含:通用格式化时间、字符串验证-验证邮箱、手机号、电话号码、url地址、严格校验身份证号码,判断数据类型-是否是字符串、数字、Boolean、函数、对象、数组...
介绍一个php一个常用工具类,js 弹窗并且跳转,js 弹窗返回 ,页面跳转,弹窗关闭,系统基本参数上传图片专用,数据库输入过滤,清理session,验证是否为空,验证是否相同,格式化字符串,格式化时间,获得真实IP...
该工具提供各种方法格式化JSON数据,该工具还演示DLL的使用,该工具使用方便,该工具占用小,还有演示的列子对 Newtonsoft.Json.dll和JsonViewer.dll的使用 JSON源代码 JSON字符串格式化 JSON JSON类
日期格式化和解析:日期工具类可以提供方法来格式化和解析日期字符串。使用DateTimeFormatter类可以定义自定义的日期格式,并在日期和字符串之间进行转换。工具类可以封装常见的日期格式,如年-月-日、月/日/年等,...
DateFormatUtils 时间工具类 1、获得格式化后的日期(yyy-MM-dd)的系统时间 2、将DATE转成想要的string 3、获得格式化后的日期(yyyyMMddHHmmss)的系统时间 4、获得格式化后的日期(yyyMMdd)的系统时间 5、获得...