有用的java日期操作函数

今天google日期函数的时候,搜到这个,很全面强大。拷贝过来,以便下次查询用。

/**

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