1.
2.
3.public static final String FormatDayToChinaTime24H = "yyyy年MM月dd日";
4.
5. /**
6. * 将yyyy-mm-dd转为yyyy-m-d
7. *
8. * @param day
9. * 距离现在之后的天数
10. * @return Date:距离现在之后的若干天的日期;
11. */
12. public static String getYMDDays(String day){
13. String dayTime=day;
14. if(String.valueOf(day.charAt(8)).equals("0"))dayTime=day.substring(0,8)+day.substring(9);
15. if(String.valueOf(day.charAt(5)).equals("0"))dayTime=dayTime.substring(0,5)+dayTime.substring(6);
16. return dayTime;
17. }
18.
19.
20. /**
21. * 两个日期间的天数
22. *
23. * @param days
24. * 距离现在之后的天数
25. * @return Date:距离现在之后的若干天的日期;
26. */
27. public static int getIntervalDays(Date startday,Date endday){
28. if(startday.after(endday)){
29. Date cal=startday;
30. startday=endday;
31. endday=cal;
32. }
33. long sl=startday.getTime();
34. long el=endday.getTime();
35. long ei=el-sl;
36. return (int)(ei/(1000*60*60*24));
37. }
38.
39.
40.
41. /**
42. * 取得距离现在多少天(距离现在之后的若干天)
43. *
44. * @param days
45. * 距离现在之后的天数
46. * @return Date:距离现在之后的若干天的日期;
47. */
48. public static Date getDate(int days) {
49. Date dateresult = new Date();
50. try {
51. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
52. // Create our Gregorian Calendar.
53. GregorianCalendar cal = new GregorianCalendar();
54.
55. // Set the date and time of our calendar
56. // to the system&s date and time
57. cal.setTime(new Date());
58. cal.add(GregorianCalendar.DAY_OF_MONTH, days);
59. dateresult = cal.getTime();
60. } catch (Exception e) {
61. System.out.println("exception" + e.toString());
62. }
63. return dateresult;
64. }
65.
66. /**
67. * @根据当前日期计算n天后的日期
68. * @return String
69. */
70. public static Date afterNDay(Date date, int n) {
71. Calendar c = Calendar.getInstance();
72. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
73. c.setTime(date);
74. c.add(Calendar.DATE, n);
75. Date d2 = c.getTime();
76. // String s=df.format(d2);
77. return d2;
78. }
79.
80. /**
81. * @根据当前日期计算n天后的日期
82. * @return String
83. */
84. public static String afterDay(Date date, int n) {
85. Calendar c = Calendar.getInstance();
86. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
87. // String s=df.format(d2);
88. return df.toString();
89. }
90.
91. /**
92. * @获取当前时间是星期几,“星期日”
93. * @return String
94. */
95. public static String getDayInWeek() {
96. Date today = new Date(System.currentTimeMillis());
97. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
98. return simpleDateFormat.format(today);
99. }
100. /**
101. *
102. * @跟具字符串时间来获得星期几,(SimpleDateFormat("yyyy-MM-dd"))
103. * @return
104. */
105.public static String getDayInWeek(String mydatestring) {
106. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
107. Date date = null;
108. String showDate ="";
109. try {
110. date = dateFormat.parse(mydatestring);
111. } catch (ParseException e) {
112.
113. }
114. Calendar cd = Calendar.getInstance();
115. cd.setTime(date);
116. int mydate = cd.get(Calendar.DAY_OF_WEEK);
117.
118. switch (mydate) {
119. case 1:
120. showDate = "星期日";
121. break;
122. case 2:
123. showDate = "星期一";
124. break;
125. case 3:
126. showDate = "星期二";
127. break;
128. case 4:
129. showDate = "星期三";
130. break;
131. case 5:
132. showDate = "星期四";
133. break;
134. case 6:
135. showDate = "星期五";
136. break;
137. default:
138. showDate = "星期六";
139. break;
140. }
141.
142. return showDate;
143.
144.}
145. /**
146. * @根据日期时间,获取当前时间是星期几,“星期日”
147. * @return String
148. */
149. public static String getDayInWeek(Date date) {
150. if (date == null)
151. return "";
152. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
153. return simpleDateFormat.format(date);
154. }
155.
156. /**
157. * @获取当前时间在这个月的天
158. * @return String
159. */
160. public static String getDayInMonth() {
161. Date today = new Date(System.currentTimeMillis());
162. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d");
163. return simpleDateFormat.format(today);
164. }
165.
166. /**
167. * @根据日期,获取当前时间在这个月的天
168. * @return String
169. */
170. public static String getDayInMonth(Date date) {
171. if (date == null)
172. return "";
173. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d");
174. return simpleDateFormat.format(date);
175. }
176.
177. /**
178. * @根据在本年当中,获取月份格式"M"
179. * @return String
180. */
181. public static String getMonthInYear() {
182. Date today = new Date(System.currentTimeMillis());
183. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M");
184. return simpleDateFormat.format(today);
185. }
186.
187. /**
188. * @根据在本年当中,获取月份格式"MM"
189. * @return String
190. */
191. public static String getMonthInYear2() {
192. java.util.Date today = new Date(System.currentTimeMillis());
193. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
194. return simpleDateFormat.format(today);
195. }
196.
197. /**
198. * @根据日期,获取月份格式"MM"
199. * @return String
200. */
201. public static String getMonthInYear2(String formatDate) {
202. java.util.Date day = getDate(formatDate, "yyyy-MM-dd");
203. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
204. return simpleDateFormat.format(day);
205. }
206.
207. /**
208. * @获取当前时间的年,格式"yyyy"
209. * @return String
210. */
211. public static String getYear() {
212. java.util.Date today = new java.util.Date(System.currentTimeMillis());
213. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
214. return simpleDateFormat.format(today);
215. }
216.
217. /**
218. * @根据时间,获取年,格式"yyyy"
219. * @return String
220. */
221. public static String getYear(String formatDate) {
222. java.util.Date day = getDate(formatDate, "yyyy-MM-dd");
223. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
224. return simpleDateFormat.format(day);
225. }
226.
227. /**
228. * @根据日期以及日期格式,获取日期字符串表达
229. * @return String
230. */
231. public static String getFormatDate(java.util.Date thisDate, String format) {
232. if (thisDate == null)
233. return "";
234. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
235. return simpleDateFormat.format(thisDate);
236. }
237.
238. /**
239. * @根据日期以及日期格式,获取日期
240. * @return String
241. */
242. public static java.util.Date getDate(String date, String format) {
243. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
244. try {
245. return simpleDateFormat.parse(date);
246. } catch (ParseException ex) {
247. return null;
248. }
249. }
250.
251. /**
252. * @返回系统当前月的第一天所表示的日期
253. * @param monthDate
254. * Date
255. * @return Date
256. */
257. public static java.util.Date getFirstDateMonth() {
258. try {
259. String date_s = getYear() + "-" + getMonthInYear2() + "-01";
260. return getDate(date_s, "yyyy-MM-dd");
261. } catch (Exception er) {
262. er.printStackTrace();
263. }
264. return null;
265. }
266.
267. /**
268. * @返回系统当前月的第一天所表示的日期
269. * @param monthDate
270. * Date
271. * @return Date
272. */
273. public static java.util.Date getFirstDateMonth(String formatDate) {
274. try {
275. String date_s = getYear(formatDate) + "-"
276. + getMonthInYear2(formatDate) + "-01";
277. return getDate(date_s, "yyyy-MM-dd");
278. } catch (Exception er) {
279. er.printStackTrace();
280. }
281. return null;
282. }
283.
284. /**
285. * @返回系统当前月的第一天所表示的日期
286. * @param monthDate
287. * Date
288. * @return Date
289. */
290. public static String getFirstDateMonthToString() {
291. try {
292. return getYear() + "-" + getMonthInYear2() + "-01";
293. } catch (Exception er) {
294. er.printStackTrace();
295. }
296. return null;
297. }
298.
299. /**
300. * @返回系统当前月的第一天所表示的日期
301. * @param monthDate
302. * Date
303. * @return Date
304. */
305. public static String getFirstDateMonthToString(String formatDate) {
306. try {
307. return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
308. + "-01";
309. } catch (Exception er) {
310. er.printStackTrace();
311. }
312. return null;
313. }
314.
315. /**
316. * @返回系统当前月的最后一天所表示的日期
317. * @return Date
318. */
319. public static java.util.Date getLastDateMonth() {
320. try {
321. int lastday = getDaySumInTheMonth();
322. String date_s;
323. if (lastday < 10)
324. date_s = getYear() + "-" + getMonthInYear2() + "-0" + lastday;
325. else
326. date_s = getYear() + "-" + getMonthInYear2() + "-" + lastday;
327. return getDate(date_s, "yyyy-MM-d");
328. } catch (Exception er) {
329. er.printStackTrace();
330. }
331. return null;
332. }
333.
334. /**
335. * @返回系统当前月的最后一天所表示的日期
336. * @return Date
337. */
338. public static String getLastDateMonthToString(String formatDate) {
339. try {
340. int lastday = getDaySumInTheMonth(formatDate);
341. if (lastday < 10)
342. return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
343. + "-0" + lastday;
344. else
345. return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
346. + "-" + lastday;
347. } catch (Exception er) {
348. er.printStackTrace();
349. }
350. return null;
351. }
352.
353. /**
354. * @根据时间,返回最后一天所表示的日期
355. * @param formatDate
356. * String
357. * @return Date
358. */
359. public static java.util.Date getLastDateMonth(String formatDate) {
360. try {
361. int lastday = getDaySumInTheMonth(formatDate);
362. String month = getMonthInYear2(formatDate);
363. String year = getYear(formatDate);
364. String date_s;
365. if (lastday < 10)
366. date_s = year + "-" + month + "-0" + lastday;
367. else
368. date_s = year + "-" + month + "-" + lastday;
369.
370. System.out.println(date_s);
371.
372. return getDate(date_s, "yyyy-MM-d");
373. } catch (Exception er) {
374. er.printStackTrace();
375. }
376. return null;
377. }
378.
379. /**
380. * @获得这个月的天数
381. * @return int
382. */
383. public static int getDaySumInTheMonth() {
384. String month = DayFormat.getMonthInYear();
385. if (month.equalsIgnoreCase("12"))
386. return 31;
387. int mon = Integer.parseInt(month);
388. java.util.Date date = DayFormat.getDate(DayFormat.getYear() + "-"
389. + (mon + 1) + "-" + "1", "yyyy-MM-d");
390. Calendar cal = Calendar.getInstance();
391. cal.setTime(date);
392. cal.add(Calendar.DATE, -1);
393. return Integer.parseInt(new SimpleDateFormat("dd")
394. .format(cal.getTime()));
395. }
396.
397. /**
398. * @获得所在日期月份的天数
399. * @return int
400. */
401. public static int getDaySumInTheMonth(java.util.Date date) {
402. String month = new SimpleDateFormat("MM").format(date);
403. if (month.equalsIgnoreCase("12"))
404. return 31;
405. int mon = Integer.parseInt(month);
406. java.util.Date date2 = DayFormat.getDate(new SimpleDateFormat("yyyy")
407. .format(date)
408. + "-" + (mon + 1) + "-" + "1", "yyyy-MM-d");
409. Calendar cal = Calendar.getInstance();
410. cal.setTime(date2);
411. cal.add(Calendar.DATE, -1);
412. return Integer.parseInt(new SimpleDateFormat("dd")
413. .format(cal.getTime()));
414. }
415.
416. /**
417. * @获得所在日期月份的天数
418. * @return int
419. */
420. public static int getDaySumInTheMonth(String formatDate) {
421. String month = DayFormat.getMonthInYear2(formatDate);
422. if (month.equalsIgnoreCase("12"))
423. return 31;
424. int mon = Integer.parseInt(month);
425. java.util.Date date = DayFormat.getDate(DayFormat.getYear(formatDate)
426. + "-" + (mon + 1) + "-" + "1", "yyyy-MM-d");
427. Calendar cal = Calendar.getInstance();
428. cal.setTime(date);
429. cal.add(Calendar.DATE, -1);
430. return Integer.parseInt(new SimpleDateFormat("dd")
431. .format(cal.getTime()));
432. }
433.
434. /**
435. * @把日期格式转换成字符串格式,格式为'yyyy-MM-dd'
436. * @param date
437. * Date
438. * @return String
439. */
440. public static String getFormatDate(java.util.Date date) {
441. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
442. return simpleDateFormat.format(date);
443. }
444.
445. public final static String FormatDay1 = "yyyy-MM-dd";
446. public final static String FormatDay2 = "yyyy年MM月dd日";
447. public final static String FormatDay3 = "yyyy年MM月dd日 HH:mm:ss";
448. public final static String FormatDay4 = "yyyy-MM-dd HH:mm";
449.
450. public static final String Format24Datetime1 = "yyyy-MM-dd HH:mm:ss";// 24
451.
452. public static final String Format12Datetime1 = "yyyy-MM-dd hh:mm:ss";// 12
453.
454. /**
455. * 获取当前时间
456. *
457. * @return
458. */
459. public static String getToday() {
460. return getToday(FormatDay1);
461. }
462.
463. public static java.sql.Timestamp getCurrentTimestamp() {
464. return new Timestamp(System.currentTimeMillis());
465. }
466.
467. public static java.util.Date getCurrentUtilDate() {
468. return new java.util.Date(System.currentTimeMillis());
469. }
470.
471. public static String getToday(String formatDay) {
472. java.util.Date today = new java.util.Date();
473. SimpleDateFormat df = new SimpleDateFormat(formatDay);
474. return df.format(today).toString();
475. }
476.
477. public static String get24DateTime() {
478. return get24DateTime(Format24Datetime1);
479. }
480.
481. public static String get24DateTime(String format24DateTime) {
482. java.util.Date today = new java.util.Date();
483. SimpleDateFormat df = new SimpleDateFormat(format24DateTime);
484. return df.format(today).toString();
485. }
486.
487. public static String get12DateTime() {
488. return get12DateTime(Format12Datetime1);
489. }
490.
491. public static String get12DateTime(String format12DateTime) {
492. java.util.Date today = new java.util.Date();
493. SimpleDateFormat df = new SimpleDateFormat(format12DateTime);
494. return df.format(today).toString();
495. }
496.
497. public static String get24DateTime(Timestamp timestamp) {
498. SimpleDateFormat df = new SimpleDateFormat(Format24Datetime1);
499. return df.format(timestamp).toString();
500. }
501.
502. public static String get12DateTime(Timestamp timestamp) {
503. SimpleDateFormat df = new SimpleDateFormat(Format12Datetime1);
504. return df.format(timestamp).toString();
505. }
506.
507. // STRING到日期
508. public static java.sql.Date stringToDate(String dateStr) {
509. return java.sql.Date.valueOf(dateStr);
510. }
511.
512. public static java.sql.Timestamp stringToTimestamp(String timestampStr) {
513. if (timestampStr == null || timestampStr.length() < 1)
514. return null;
515. return java.sql.Timestamp.valueOf(timestampStr);
516. }
517.
518. public static java.sql.Timestamp stringToTimestamp2(String dateStr) {
519. if (dateStr == null || dateStr.length() < 1)
520. return null;
521. return java.sql.Timestamp.valueOf(dateStr + " 00:00:00.000000000");
522. }
523.
524. public static java.sql.Timestamp stringToTimestamp3(String dateStr) {
525. if (dateStr == null || dateStr.length() < 1)
526. return null;
527. return java.sql.Timestamp.valueOf(dateStr + ":00.000000000");
528. }
529.
530. public static java.sql.Time stringToTime(String timeStr) {
531. return java.sql.Time.valueOf(timeStr);
532. }
533.
534. // 日期到STRING
535. public static String dateToString(java.sql.Date datee) {
536. return datee.toString();
537. }
538.
539. public static String timestampToString(java.sql.Timestamp timestampe) {
540. return timestampe.toString();
541. }
542.
543. public static String timestampToStringForFormat(Timestamp timestamp,
544. String format) {
545. if (timestamp == null)
546. return "";
547. SimpleDateFormat df = new SimpleDateFormat(format);
548. return df.format(timestamp).toString();
549. }
550.
551. public static String getTimestampToDateTime15Len(
552. java.sql.Timestamp timestampe) {
553. if (timestampe == null || timestampe.toString().length() < 1)
554. return "";
555. return timestampe.toString().substring(0, 16);
556. }
557.
558. public static String timeToString(java.sql.Time timee) {
559. return timee.toString();
560. }
561.
562. public static java.sql.Timestamp StringToTimestamp(String dateString)
563. throws ParseException {
564. java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
565. FormatDay1);
566. formatter.setLenient(false);
567. return new java.sql.Timestamp((formatter.parse(dateString).getTime()));
568. }
569.
570. public static java.sql.Timestamp StringToTimestampAll(String dateString)
571. throws ParseException {
572. return java.sql.Timestamp.valueOf(dateString);
573. }
574. //获得周日的日期
575. public static String getSunday(Date date){
576. Calendar c = Calendar.getInstance();
577. c.setTime(date);
578. c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
579. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
580. }
581. //获得周一的日期
582. public static String getMonday(Date date){
583. Calendar c = Calendar.getInstance();
584. c.setTime(date);
585. c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
586. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
587. }
588. //获得周二的日期
589. public static String getTuesday(Date date){
590. Calendar c = Calendar.getInstance();
591. c.setTime(date);
592. c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);
593. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
594. }
595. //获得周三的日期
596. public static String getWednesday(Date date){
597. Calendar c = Calendar.getInstance();
598. c.setTime(date);
599. c.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);
600. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
601. }
602. //获得周四的日期
603. public static String getThursday(Date date){
604. Calendar c = Calendar.getInstance();
605. c.setTime(date);
606. c.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);
607. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
608. }
609. //获得周五的日期
610. public static String getFriday(Date date){
611. Calendar c = Calendar.getInstance();
612. c.setTime(date);
613. c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
614. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
615. }
616. //获得周六的日期
617. public static String getSaturday(Date date){
618. Calendar c = Calendar.getInstance();
619. c.setTime(date);
620. c.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
621. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
622. }
623.
624. public static void main(String args[]) {
625. try {
626. String d = "2007-05-15 19:23:48.703";
627. System.out.println(d);
628. java.sql.Timestamp timestampe = java.sql.Timestamp.valueOf(d);
629. System.out.println(timestampe.toString());
630. } catch (Exception er) {
631. er.printStackTrace();
632. }
633. }
转自:http://blog.csdn.net/yongbuyanqi_zy/archive/2009/02/10/3874138.aspx