java中Date转换大全

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