Java日期格式转换 Date转String、String转Date

缺乏、安全感 2021-11-11 10:38 463阅读 0赞
  1. Java时间格式转换大全
  2. import java.text.*;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. public class VeDate {
  6. /**
  7. * 获取现在时间
  8. *
  9. * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  10. */
  11. public static Date getNowDate() {
  12. Date currentTime = new Date();
  13. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  14. String dateString = formatter.format(currentTime);
  15. ParsePosition pos = new ParsePosition(8);
  16. Date currentTime_2 = formatter.parse(dateString, pos);
  17. return currentTime_2;
  18. }
  19. /**
  20. * 获取现在时间
  21. *
  22. * @return返回短时间格式 yyyy-MM-dd
  23. */
  24. DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
  25. DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
  26. Date date = null;
  27. String str = null;
  28. // String转Date
  29. str = "2007-1-18";
  30. try {
  31. date = format1.parse(str);
  32. data = format2.parse(str);
  33. } catch (ParseException e) {
  34. e.printStackTrace();
  35. }
  36. /**
  37. * 获取现在时间
  38. *
  39. * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  40. */
  41. public static String getStringDate() {
  42. Date currentTime = new Date();
  43. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  44. String dateString = formatter.format(currentTime);
  45. return dateString;
  46. }
  47. /**
  48. * 获取现在时间
  49. *
  50. * @return 返回短时间字符串格式yyyy-MM-dd
  51. */
  52. public static String getStringDateShort() {
  53. Date currentTime = new Date();
  54. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  55. String dateString = formatter.format(currentTime);
  56. return dateString;
  57. }
  58. /**
  59. * 获取时间 小时:分;秒 HH:mm:ss
  60. *
  61. * @return
  62. */
  63. public static String getTimeShort() {
  64. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  65. Date currentTime = new Date();
  66. String dateString = formatter.format(currentTime);
  67. return dateString;
  68. }
  69. /**
  70. * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  71. *
  72. * @param strDate
  73. * @return
  74. */
  75. public static Date strToDateLong(String strDate) {
  76. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  77. ParsePosition pos = new ParsePosition(0);
  78. Date strtodate = formatter.parse(strDate, pos);
  79. return strtodate;
  80. }
  81. /**
  82. * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
  83. *
  84. * @param dateDate
  85. * @return
  86. */
  87. public static String dateToStrLong(java.util.Date dateDate) {
  88. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  89. String dateString = formatter.format(dateDate);
  90. return dateString;
  91. }
  92. /**
  93. * 将短时间格式时间转换为字符串 yyyy-MM-dd
  94. *
  95. * @param dateDate
  96. * @param k
  97. * @return
  98. */
  99. public static String dateToStr(java.util.Date dateDate) {
  100. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  101. String dateString = formatter.format(dateDate);
  102. return dateString;
  103. }
  104. /**
  105. * 将短时间格式字符串转换为时间 yyyy-MM-dd
  106. *
  107. * @param strDate
  108. * @return
  109. */
  110. public static Date strToDate(String strDate) {
  111. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  112. ParsePosition pos = new ParsePosition(0);
  113. Date strtodate = formatter.parse(strDate, pos);
  114. return strtodate;
  115. }
  116. /**
  117. * 得到现在时间
  118. *
  119. * @return
  120. */
  121. public static Date getNow() {
  122. Date currentTime = new Date();
  123. return currentTime;
  124. }
  125. /**
  126. * 提取一个月中的最后一天
  127. *
  128. * @param day
  129. * @return
  130. */
  131. public static Date getLastDate(long day) {
  132. Date date = new Date();
  133. long date_3_hm = date.getTime() - 3600000 * 34 * day;
  134. Date date_3_hm_date = new Date(date_3_hm);
  135. return date_3_hm_date;
  136. }
  137. /**
  138. * 得到现在时间
  139. *
  140. * @return 字符串 yyyyMMdd HHmmss
  141. */
  142. public static String getStringToday() {
  143. Date currentTime = new Date();
  144. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  145. String dateString = formatter.format(currentTime);
  146. return dateString;
  147. }
  148. /**
  149. * 得到现在小时
  150. */
  151. public static String getHour() {
  152. Date currentTime = new Date();
  153. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  154. String dateString = formatter.format(currentTime);
  155. String hour;
  156. hour = dateString.substring(11, 13);
  157. return hour;
  158. }
  159. /**
  160. * 得到现在分钟
  161. *
  162. * @return
  163. */
  164. public static String getTime() {
  165. Date currentTime = new Date();
  166. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  167. String dateString = formatter.format(currentTime);
  168. String min;
  169. min = dateString.substring(14, 16);
  170. return min;
  171. }
  172. /**
  173. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  174. *
  175. * @param sformat
  176. * yyyyMMddhhmmss
  177. * @return
  178. */
  179. public static String getUserDate(String sformat) {
  180. Date currentTime = new Date();
  181. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  182. String dateString = formatter.format(currentTime);
  183. return dateString;
  184. }
  185. --------------------------------------------------------------------------------------------------------------------------------
  186. 做成方法
  187. import java.util.*;
  188. import java.text.*;
  189. import java.util.Calendar;
  190. public class VeDate {
  191. /**
  192. * 获取现在时间
  193. *
  194. * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  195. */
  196. public static Date getNowDate() {
  197. Date currentTime = new Date();
  198. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  199. String dateString = formatter.format(currentTime);
  200. ParsePosition pos = new ParsePosition(8);
  201. Date currentTime_2 = formatter.parse(dateString, pos);
  202. return currentTime_2;
  203. }
  204. /**
  205. * 获取现在时间
  206. *
  207. * @return返回短时间格式 yyyy-MM-dd
  208. */
  209. public static Date getNowDateShort() {
  210. Date currentTime = new Date();
  211. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  212. String dateString = formatter.format(currentTime);
  213. ParsePosition pos = new ParsePosition(8);
  214. Date currentTime_2 = formatter.parse(dateString, pos);
  215. return currentTime_2;
  216. }
  217. /**
  218. * 获取现在时间
  219. *
  220. * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  221. */
  222. public static String getStringDate() {
  223. Date currentTime = new Date();
  224. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  225. String dateString = formatter.format(currentTime);
  226. return dateString;
  227. }
  228. /**
  229. * 获取现在时间
  230. *
  231. * @return 返回短时间字符串格式yyyy-MM-dd
  232. */
  233. public static String getStringDateShort() {
  234. Date currentTime = new Date();
  235. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  236. String dateString = formatter.format(currentTime);
  237. return dateString;
  238. }
  239. /**
  240. * 获取时间 小时:分;秒 HH:mm:ss
  241. *
  242. * @return
  243. */
  244. public static String getTimeShort() {
  245. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  246. Date currentTime = new Date();
  247. String dateString = formatter.format(currentTime);
  248. return dateString;
  249. }
  250. /**
  251. * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  252. *
  253. * @param strDate
  254. * @return
  255. */
  256. public static Date strToDateLong(String strDate) {
  257. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  258. ParsePosition pos = new ParsePosition(0);
  259. Date strtodate = formatter.parse(strDate, pos);
  260. return strtodate;
  261. }
  262. /**
  263. * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
  264. *
  265. * @param dateDate
  266. * @return
  267. */
  268. public static String dateToStrLong(java.util.Date dateDate) {
  269. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  270. String dateString = formatter.format(dateDate);
  271. return dateString;
  272. }
  273. /**
  274. * 将短时间格式时间转换为字符串 yyyy-MM-dd
  275. *
  276. * @param dateDate
  277. * @param k
  278. * @return
  279. */
  280. public static String dateToStr(java.util.Date dateDate) {
  281. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  282. String dateString = formatter.format(dateDate);
  283. return dateString;
  284. }
  285. /**
  286. * 将短时间格式字符串转换为时间 yyyy-MM-dd
  287. *
  288. * @param strDate
  289. * @return
  290. */
  291. public static Date strToDate(String strDate) {
  292. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  293. ParsePosition pos = new ParsePosition(0);
  294. Date strtodate = formatter.parse(strDate, pos);
  295. return strtodate;
  296. }
  297. /**
  298. * 得到现在时间
  299. *
  300. * @return
  301. */
  302. public static Date getNow() {
  303. Date currentTime = new Date();
  304. return currentTime;
  305. }
  306. /**
  307. * 提取一个月中的最后一天
  308. *
  309. * @param day
  310. * @return
  311. */
  312. public static Date getLastDate(long day) {
  313. Date date = new Date();
  314. long date_3_hm = date.getTime() - 3600000 * 34 * day;
  315. Date date_3_hm_date = new Date(date_3_hm);
  316. return date_3_hm_date;
  317. }
  318. /**
  319. * 得到现在时间
  320. *
  321. * @return 字符串 yyyyMMdd HHmmss
  322. */
  323. public static String getStringToday() {
  324. Date currentTime = new Date();
  325. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  326. String dateString = formatter.format(currentTime);
  327. return dateString;
  328. }
  329. /**
  330. * 得到现在小时
  331. */
  332. public static String getHour() {
  333. Date currentTime = new Date();
  334. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  335. String dateString = formatter.format(currentTime);
  336. String hour;
  337. hour = dateString.substring(11, 13);
  338. return hour;
  339. }
  340. /**
  341. * 得到现在分钟
  342. *
  343. * @return
  344. */
  345. public static String getTime() {
  346. Date currentTime = new Date();
  347. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  348. String dateString = formatter.format(currentTime);
  349. String min;
  350. min = dateString.substring(14, 16);
  351. return min;
  352. }
  353. /**
  354. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  355. *
  356. * @param sformat
  357. * yyyyMMddhhmmss
  358. * @return
  359. */
  360. public static String getUserDate(String sformat) {
  361. Date currentTime = new Date();
  362. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  363. String dateString = formatter.format(currentTime);
  364. return dateString;
  365. }
  366. /**
  367. * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  368. */
  369. public static String getTwoHour(String st1, String st2) {
  370. String[] kk = null;
  371. String[] jj = null;
  372. kk = st1.split(":");
  373. jj = st2.split(":");
  374. if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
  375. return "0";
  376. else {
  377. double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
  378. double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
  379. if ((y - u) > 0)
  380. return y - u + "";
  381. else
  382. return "0";
  383. }
  384. }
  385. /**
  386. * 得到二个日期间的间隔天数
  387. */
  388. public static String getTwoDay(String sj1, String sj2) {
  389. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  390. long day = 0;
  391. try {
  392. java.util.Date date = myFormatter.parse(sj1);
  393. java.util.Date mydate = myFormatter.parse(sj2);
  394. day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  395. } catch (Exception e) {
  396. return "";
  397. }
  398. return day + "";
  399. }
  400. /**
  401. * 时间前推或后推分钟,其中JJ表示分钟.
  402. */
  403. public static String getPreTime(String sj1, String jj) {
  404. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  405. String mydate1 = "";
  406. try {
  407. Date date1 = format.parse(sj1);
  408. long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
  409. date1.setTime(Time * 1000);
  410. mydate1 = format.format(date1);
  411. } catch (Exception e) {
  412. }
  413. return mydate1;
  414. }
  415. /**
  416. * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  417. */
  418. public static String getNextDay(String nowdate, String delay) {
  419. try{
  420. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  421. String mdate = "";
  422. Date d = strToDate(nowdate);
  423. long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  424. d.setTime(myTime * 1000);
  425. mdate = format.format(d);
  426. return mdate;
  427. }catch(Exception e){
  428. return "";
  429. }
  430. }
  431. /**
  432. * 判断是否润年
  433. *
  434. * @param ddate
  435. * @return
  436. */
  437. public static boolean isLeapYear(String ddate) {
  438. /**
  439. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  440. * 3.能被4整除同时能被100整除则不是闰年
  441. */
  442. Date d = strToDate(ddate);
  443. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  444. gc.setTime(d);
  445. int year = gc.get(Calendar.YEAR);
  446. if ((year % 400) == 0)
  447. return true;
  448. else if ((year % 4) == 0) {
  449. if ((year % 100) == 0)
  450. return false;
  451. else
  452. return true;
  453. } else
  454. return false;
  455. }
  456. /**
  457. * 返回美国时间格式 26 Apr 2006
  458. *
  459. * @param str
  460. * @return
  461. */
  462. public static String getEDate(String str) {
  463. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  464. ParsePosition pos = new ParsePosition(0);
  465. Date strtodate = formatter.parse(str, pos);
  466. String j = strtodate.toString();
  467. String[] k = j.split(" ");
  468. return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  469. }
  470. /**
  471. * 获取一个月的最后一天
  472. *
  473. * @param dat
  474. * @return
  475. */
  476. public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  477. String str = dat.substring(0, 8);
  478. String month = dat.substring(5, 7);
  479. int mon = Integer.parseInt(month);
  480. if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
  481. str += "31";
  482. } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
  483. str += "30";
  484. } else {
  485. if (isLeapYear(dat)) {
  486. str += "29";
  487. } else {
  488. str += "28";
  489. }
  490. }
  491. return str;
  492. }
  493. /**
  494. * 判断二个时间是否在同一个周
  495. *
  496. * @param date1
  497. * @param date2
  498. * @return
  499. */
  500. public static boolean isSameWeekDates(Date date1, Date date2) {
  501. Calendar cal1 = Calendar.getInstance();
  502. Calendar cal2 = Calendar.getInstance();
  503. cal1.setTime(date1);
  504. cal2.setTime(date2);
  505. int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  506. if (0 == subYear) {
  507. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  508. return true;
  509. } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  510. // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  511. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  512. return true;
  513. } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  514. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  515. return true;
  516. }
  517. return false;
  518. }
  519. /**
  520. * 产生周序列,即得到当前时间所在的年度是第几周
  521. *
  522. * @return
  523. */
  524. public static String getSeqWeek() {
  525. Calendar c = Calendar.getInstance(Locale.CHINA);
  526. String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  527. if (week.length() == 1)
  528. week = "0" + week;
  529. String year = Integer.toString(c.get(Calendar.YEAR));
  530. return year + week;
  531. }
  532. /**
  533. * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  534. *
  535. * @param sdate
  536. * @param num
  537. * @return
  538. */
  539. public static String getWeek(String sdate, String num) {
  540. // 再转换为时间
  541. Date dd = VeDate.strToDate(sdate);
  542. Calendar c = Calendar.getInstance();
  543. c.setTime(dd);
  544. if (num.equals("1")) // 返回星期一所在的日期
  545. c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  546. else if (num.equals("2")) // 返回星期二所在的日期
  547. c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  548. else if (num.equals("3")) // 返回星期三所在的日期
  549. c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  550. else if (num.equals("4")) // 返回星期四所在的日期
  551. c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  552. else if (num.equals("5")) // 返回星期五所在的日期
  553. c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  554. else if (num.equals("6")) // 返回星期六所在的日期
  555. c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  556. else if (num.equals("0")) // 返回星期日所在的日期
  557. c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  558. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  559. }
  560. /**
  561. * 根据一个日期,返回是星期几的字符串
  562. *
  563. * @param sdate
  564. * @return
  565. */
  566. public static String getWeek(String sdate) {
  567. // 再转换为时间
  568. Date date = VeDate.strToDate(sdate);
  569. Calendar c = Calendar.getInstance();
  570. c.setTime(date);
  571. // int hour=c.get(Calendar.DAY_OF_WEEK);
  572. // hour中存的就是星期几了,其范围 1~7
  573. // 1=星期日 7=星期六,其他类推
  574. return new SimpleDateFormat("EEEE").format(c.getTime());
  575. }
  576. public static String getWeekStr(String sdate){
  577. String str = "";
  578. str = VeDate.getWeek(sdate);
  579. if("1".equals(str)){
  580. str = "星期日";
  581. }else if("2".equals(str)){
  582. str = "星期一";
  583. }else if("3".equals(str)){
  584. str = "星期二";
  585. }else if("4".equals(str)){
  586. str = "星期三";
  587. }else if("5".equals(str)){
  588. str = "星期四";
  589. }else if("6".equals(str)){
  590. str = "星期五";
  591. }else if("7".equals(str)){
  592. str = "星期六";
  593. }
  594. return str;
  595. }
  596. /**
  597. * 两个时间之间的天数
  598. *
  599. * @param date1
  600. * @param date2
  601. * @return
  602. */
  603. public static long getDays(String date1, String date2) {
  604. if (date1 == null || date1.equals(""))
  605. return 0;
  606. if (date2 == null || date2.equals(""))
  607. return 0;
  608. // 转换为标准时间
  609. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  610. java.util.Date date = null;
  611. java.util.Date mydate = null;
  612. try {
  613. date = myFormatter.parse(date1);
  614. mydate = myFormatter.parse(date2);
  615. } catch (Exception e) {
  616. }
  617. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  618. return day;
  619. }
  620. /**
  621. * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  622. * 此函数返回该日历第一行星期日所在的日期
  623. *
  624. * @param sdate
  625. * @return
  626. */
  627. public static String getNowMonth(String sdate) {
  628. // 取该时间所在月的一号
  629. sdate = sdate.substring(0, 8) + "01";
  630. // 得到这个月的1号是星期几
  631. Date date = VeDate.strToDate(sdate);
  632. Calendar c = Calendar.getInstance();
  633. c.setTime(date);
  634. int u = c.get(Calendar.DAY_OF_WEEK);
  635. String newday = VeDate.getNextDay(sdate, (1 - u) + "");
  636. return newday;
  637. }
  638. /**
  639. * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  640. *
  641. * @param k
  642. * 表示是取几位随机数,可以自己定
  643. */
  644. public static String getNo(int k) {
  645. return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  646. }
  647. /**
  648. * 返回一个随机数
  649. *
  650. * @param i
  651. * @return
  652. */
  653. public static String getRandom(int i) {
  654. Random jjj = new Random();
  655. // int suiJiShu = jjj.nextInt(9);
  656. if (i == 0)
  657. return "";
  658. String jj = "";
  659. for (int k = 0; k < i; k++) {
  660. jj = jj + jjj.nextInt(9);
  661. }
  662. return jj;
  663. }
  664. /**
  665. *
  666. * @param args
  667. */
  668. public static boolean RightDate(String date) {
  669. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  670. ;
  671. if (date == null)
  672. return false;
  673. if (date.length() > 10) {
  674. sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  675. } else {
  676. sdf = new SimpleDateFormat("yyyy-MM-dd");
  677. }
  678. try {
  679. sdf.parse(date);
  680. } catch (ParseException pe) {
  681. return false;
  682. }
  683. return true;
  684. }
  685. /***************************************************************************
  686. * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
  687. * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
  688. **************************************************************************/
  689. public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
  690. Date currentTime = new Date();
  691. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  692. String dateString = formatter.format(currentTime);
  693. String s_nd = dateString.substring(0, 4); // 年份
  694. String s_yf = dateString.substring(5, 7); // 月份
  695. String s_rq = dateString.substring(8, 10); // 日期
  696. String sreturn = "";
  697. roc.util.MyChar mc = new roc.util.MyChar();
  698. if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况
  699. if (nd.equals("1")) {
  700. sreturn = s_nd;
  701. // 处理间隔符
  702. if (format.equals("1"))
  703. sreturn = sreturn + "年";
  704. else if (format.equals("2"))
  705. sreturn = sreturn + "-";
  706. else if (format.equals("3"))
  707. sreturn = sreturn + "/";
  708. else if (format.equals("5"))
  709. sreturn = sreturn + ".";
  710. }
  711. // 处理月份
  712. if (yf.equals("1")) {
  713. sreturn = sreturn + s_yf;
  714. if (format.equals("1"))
  715. sreturn = sreturn + "月";
  716. else if (format.equals("2"))
  717. sreturn = sreturn + "-";
  718. else if (format.equals("3"))
  719. sreturn = sreturn + "/";
  720. else if (format.equals("5"))
  721. sreturn = sreturn + ".";
  722. }
  723. // 处理日期
  724. if (rq.equals("1")) {
  725. sreturn = sreturn + s_rq;
  726. if (format.equals("1"))
  727. sreturn = sreturn + "日";
  728. }
  729. } else {
  730. // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
  731. sdate = roc.util.RocDate.getOKDate(sdate);
  732. s_nd = sdate.substring(0, 4); // 年份
  733. s_yf = sdate.substring(5, 7); // 月份
  734. s_rq = sdate.substring(8, 10); // 日期
  735. if (nd.equals("1")) {
  736. sreturn = s_nd;
  737. // 处理间隔符
  738. if (format.equals("1"))
  739. sreturn = sreturn + "年";
  740. else if (format.equals("2"))
  741. sreturn = sreturn + "-";
  742. else if (format.equals("3"))
  743. sreturn = sreturn + "/";
  744. else if (format.equals("5"))
  745. sreturn = sreturn + ".";
  746. }
  747. // 处理月份
  748. if (yf.equals("1")) {
  749. sreturn = sreturn + s_yf;
  750. if (format.equals("1"))
  751. sreturn = sreturn + "月";
  752. else if (format.equals("2"))
  753. sreturn = sreturn + "-";
  754. else if (format.equals("3"))
  755. sreturn = sreturn + "/";
  756. else if (format.equals("5"))
  757. sreturn = sreturn + ".";
  758. }
  759. // 处理日期
  760. if (rq.equals("1")) {
  761. sreturn = sreturn + s_rq;
  762. if (format.equals("1"))
  763. sreturn = sreturn + "日";
  764. }
  765. }
  766. return sreturn;
  767. }
  768. public static String getNextMonthDay(String sdate, int m) {
  769. sdate = getOKDate(sdate);
  770. int year = Integer.parseInt(sdate.substring(0, 4));
  771. int month = Integer.parseInt(sdate.substring(5, 7));
  772. month = month + m;
  773. if (month < 0) {
  774. month = month + 12;
  775. year = year - 1;
  776. } else if (month > 12) {
  777. month = month - 12;
  778. year = year + 1;
  779. }
  780. String smonth = "";
  781. if (month < 10)
  782. smonth = "0" + month;
  783. else
  784. smonth = "" + month;
  785. return year + "-" + smonth + "-10";
  786. }
  787. public static String getOKDate(String sdate) {
  788. if (sdate == null || sdate.equals(""))
  789. return getStringDateShort();
  790. if (!VeStr.Isdate(sdate)) {
  791. sdate = getStringDateShort();
  792. }
  793. // 将“/”转换为“-”
  794. sdate = VeStr.Replace(sdate, "/", "-");
  795. // 如果只有8位长度,则要进行转换
  796. if (sdate.length() == 8)
  797. sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
  798. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  799. ParsePosition pos = new ParsePosition(0);
  800. Date strtodate = formatter.parse(sdate, pos);
  801. String dateString = formatter.format(strtodate);
  802. return dateString;
  803. }
  804. public static void main(String[] args) throws Exception {
  805. try {
  806. //System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10", "2006-11-02 11:22:09")));
  807. } catch (Exception e) {
  808. throw new Exception();
  809. }
  810. //System.out.println("sss");
  811. }

发表评论

表情:
评论列表 (有 0 条评论,463人围观)

还没有评论,来说两句吧...

相关阅读