非常有用的Java日期时间操作函数代码一览

来源:互联网 发布:js引用js文件 编辑:程序博客网 时间:2024/05/17 01:32
 

今天写的一个Log输出类,需要对日期时间进行特殊的格式化操作–这样的操作实际上经常需要用到,不过一直都没怎么保留过代码,所以又是一番查找,费时又费力。于是决定把用到的函数稍做整理,相信可以节省一些人的时间,注意并不是所有的函数都是必需的,代码也并非本人原创,而是取材于网络,放在这里纯粹是为了方便……大家可以看自己的情况按需拿取,具体函数罗列在下面:

<SCRIPT type=text/javascript><!--google_ad_client = "pub-2748932162110627";/* 1st-Minidx-Article-336-280-01 */google_ad_slot = "5298684134";google_ad_width = 336;google_ad_height = 280;//--></SCRIPT>
<SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT>
  1. /**
  2. 日期类
  3. * @date  
  4. * @version 1.0
  5. */
  6. import java.util.*;
  7. import java.text.*;
  8. import java.util.Calendar;
  9. public class VeDate {
  10.  /**
  11.   * 获取现在时间
  12.   *
  13.   * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  14.   */
  15.  public static Date getNowDate() {
  16.   Date currentTime = new Date();
  17.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  18.   String dateString = formatter.format(currentTime);
  19.   ParsePosition pos = new ParsePosition(8);
  20.   Date currentTime_2 = formatter.parse(dateString, pos);
  21.   return currentTime_2;
  22.  }
  23.  /**
  24.   * 获取现在时间
  25.   *
  26.   * @return返回短时间格式 yyyy-MM-dd
  27.   */
  28.  public static Date getNowDateShort() {
  29.   Date currentTime = new Date();
  30.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  31.   String dateString = formatter.format(currentTime);
  32.   ParsePosition pos = new ParsePosition(8);
  33.   Date currentTime_2 = formatter.parse(dateString, pos);
  34.   return currentTime_2;
  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.   * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  187.   */
  188.  public static String getTwoHour(String st1, String st2) {
  189.   String[] kk = null;
  190.   String[] jj = null;
  191.   kk = st1.split(":");
  192.   jj = st2.split(":");
  193.   if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
  194.    return "0";
  195.   else {
  196.    double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
  197.    double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
  198.    if ((y - u) > 0)
  199.     return y - u + "";
  200.    else
  201.     return "0";
  202.   }
  203.  }
  204.  /**
  205.   * 得到二个日期间的间隔天数
  206.   */
  207.  public static String getTwoDay(String sj1, String sj2) {
  208.   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  209.   long day = 0;
  210.   try {
  211.    java.util.Date date = myFormatter.parse(sj1);
  212.    java.util.Date mydate = myFormatter.parse(sj2);
  213.    day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  214.   } catch (Exception e) {
  215.    return "";
  216.   }
  217.   return day + "";
  218.  }
  219.  /**
  220.   * 时间前推或后推分钟,其中JJ表示分钟.
  221.   */
  222.  public static String getPreTime(String sj1, String jj) {
  223.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  224.   String mydate1 = "";
  225.   try {
  226.    Date date1 = format.parse(sj1);
  227.    long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
  228.    date1.setTime(Time * 1000);
  229.    mydate1 = format.format(date1);
  230.   } catch (Exception e) {
  231.   }
  232.   return mydate1;
  233.  }
  234.  /**
  235.   * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  236.   */
  237.  public static String getNextDay(String nowdate, String delay) {
  238.   try{
  239.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  240.   String mdate = "";
  241.   Date d = strToDate(nowdate);
  242.   long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  243.   d.setTime(myTime * 1000);
  244.   mdate = format.format(d);
  245.   return mdate;
  246.   }catch(Exception e){
  247.    return "";
  248.   }
  249.  }
  250.  /**
  251.   * 判断是否润年
  252.   *
  253.   * @param ddate
  254.   * @return
  255.   */
  256.  public static boolean isLeapYear(String ddate) {
  257.   /**
  258.    * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  259.    * 3.能被4整除同时能被100整除则不是闰年
  260.    */
  261.   Date d = strToDate(ddate);
  262.   GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  263.   gc.setTime(d);
  264.   int year = gc.get(Calendar.YEAR);
  265.   if ((year % 400) == 0)
  266.    return true;
  267.   else if ((year % 4) == 0) {
  268.    if ((year % 100) == 0)
  269.     return false;
  270.    else
  271.     return true;
  272.   } else
  273.    return false;
  274.  }
  275.  /**
  276.   * 返回美国时间格式 26 Apr 2006
  277.   *
  278.   * @param str
  279.   * @return
  280.   */
  281.  public static String getEDate(String str) {
  282.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  283.   ParsePosition pos = new ParsePosition(0);
  284.   Date strtodate = formatter.parse(str, pos);
  285.   String j = strtodate.toString();
  286.   String[] k = j.split(" ");
  287.   return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  288.  }
  289.  /**
  290.   * 获取一个月的最后一天
  291.   *
  292.   * @param dat
  293.   * @return
  294.   */
  295.  public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  296.   String str = dat.substring(0, 8);
  297.   String month = dat.substring(5, 7);
  298.   int mon = Integer.parseInt(month);
  299.   if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
  300.    str += "31";
  301.   } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
  302.    str += "30";
  303.   } else {
  304.    if (isLeapYear(dat)) {
  305.     str += "29";
  306.    } else {
  307.     str += "28";
  308.    }
  309.   }
  310.   return str;
  311.  }
  312.  /**
  313.   * 判断二个时间是否在同一个周
  314.   *
  315.   * @param date1
  316.   * @param date2
  317.   * @return
  318.   */
  319.  public static boolean isSameWeekDates(Date date1, Date date2) {
  320.   Calendar cal1 = Calendar.getInstance();
  321.   Calendar cal2 = Calendar.getInstance();
  322.   cal1.setTime(date1);
  323.   cal2.setTime(date2);
  324.   int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  325.   if (0 == subYear) {
  326.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  327.     return true;
  328.   } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  329.    // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  330.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  331.     return true;
  332.   } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  333.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  334.     return true;
  335.   }
  336.   return false;
  337.  }
  338.  /**
  339.   * 产生周序列,即得到当前时间所在的年度是第几周
  340.   *
  341.   * @return
  342.   */
  343.  public static String getSeqWeek() {
  344.   Calendar c = Calendar.getInstance(Locale.CHINA);
  345.   String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  346.   if (week.length() == 1)
  347.    week = "0" + week;
  348.   String year = Integer.toString(c.get(Calendar.YEAR));
  349.   return year + week;
  350.  }
  351.  /**
  352.   * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  353.   *
  354.   * @param sdate
  355.   * @param num
  356.   * @return
  357.   */
  358.  public static String getWeek(String sdate, String num) {
  359.   // 再转换为时间
  360.   Date dd = VeDate.strToDate(sdate);
  361.   Calendar c = Calendar.getInstance();
  362.   c.setTime(dd);
  363.   if (num.equals("1")) // 返回星期一所在的日期
  364.    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  365.   else if (num.equals("2")) // 返回星期二所在的日期
  366.    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  367.   else if (num.equals("3")) // 返回星期三所在的日期
  368.    c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  369.   else if (num.equals("4")) // 返回星期四所在的日期
  370.    c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  371.   else if (num.equals("5")) // 返回星期五所在的日期
  372.    c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  373.   else if (num.equals("6")) // 返回星期六所在的日期
  374.    c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  375.   else if (num.equals("0")) // 返回星期日所在的日期
  376.    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  377.   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  378.  }
  379.  /**
  380.   * 根据一个日期,返回是星期几的字符串
  381.   *
  382.   * @param sdate
  383.   * @return
  384.   */
  385.  public static String getWeek(String sdate) {
  386.   // 再转换为时间
  387.   Date date = VeDate.strToDate(sdate);
  388.   Calendar c = Calendar.getInstance();
  389.   c.setTime(date);
  390.   // int hour=c.get(Calendar.DAY_OF_WEEK);
  391.   // hour中存的就是星期几了,其范围 1~7
  392.   // 1=星期日 7=星期六,其他类推
  393.   return new SimpleDateFormat("EEEE").format(c.getTime());
  394.  }
  395.  public static String getWeekStr(String sdate){
  396.   String str = "";
  397.   str = VeDate.getWeek(sdate);
  398.   if("1".equals(str)){
  399.    str = "星期日";
  400.   }else if("2".equals(str)){
  401.    str = "星期一";
  402.   }else if("3".equals(str)){
  403.    str = "星期二";
  404.   }else if("4".equals(str)){
  405.    str = "星期三";
  406.   }else if("5".equals(str)){
  407.    str = "星期四";
  408.   }else if("6".equals(str)){
  409.    str = "星期五";
  410.   }else if("7".equals(str)){
  411.    str = "星期六";
  412.   }
  413.   return str;
  414.  }
  415.  /**
  416.   * 两个时间之间的天数
  417.   *
  418.   * @param date1
  419.   * @param date2
  420.   * @return
  421.   */
  422.  public static long getDays(String date1, String date2) {
  423.   if (date1 == null || date1.equals(""))
  424.    return 0;
  425.   if (date2 == null || date2.equals(""))
  426.    return 0;
  427.   // 转换为标准时间
  428.   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  429.   java.util.Date date = null;
  430.   java.util.Date mydate = null;
  431.   try {
  432.    date = myFormatter.parse(date1);
  433.    mydate = myFormatter.parse(date2);
  434.   } catch (Exception e) {
  435.   }
  436.   long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  437.   return day;
  438.  }
  439.  /**
  440.   * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  441.   * 此函数返回该日历第一行星期日所在的日期
  442.   *
  443.   * @param sdate
  444.   * @return
  445.   */
  446.  public static String getNowMonth(String sdate) {
  447.   // 取该时间所在月的一号
  448.   sdate = sdate.substring(0, 8) + "01";
  449.   // 得到这个月的1号是星期几
  450.   Date date = VeDate.strToDate(sdate);
  451.   Calendar c = Calendar.getInstance();
  452.   c.setTime(date);
  453.   int u = c.get(Calendar.DAY_OF_WEEK);
  454.   String newday = VeDate.getNextDay(sdate, (1 - u) + "");
  455.   return newday;
  456.  }
  457.  /**
  458.   * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  459.   *
  460.   * @param k
  461.   *            表示是取几位随机数,可以自己定
  462.   */
  463.  public static String getNo(int k) {
  464.   return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  465.  }
  466.  /**
  467.   * 返回一个随机数
  468.   *
  469.   * @param i
  470.   * @return
  471.   */
  472.  public static String getRandom(int i) {
  473.   Random jjj = new Random();
  474.   // int suiJiShu = jjj.nextInt(9);
  475.   if (i == 0)
  476.    return "";
  477.   String jj = "";
  478.   for (int k = 0; k < i; k++) {
  479.    jj = jj + jjj.nextInt(9);
  480.   }
  481.   return jj;
  482.  }
  483.  /**
  484.   *
  485.   * @param args
  486.   */
  487.  public static boolean RightDate(String date) {
  488.   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  489.   ;
  490.   if (date == null)
  491.    return false;
  492.   if (date.length() > 10) {
  493.    sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  494.   } else {
  495.    sdf = new SimpleDateFormat("yyyy-MM-dd");
  496.   }
  497.   try {
  498.    sdf.parse(date);
  499.   } catch (ParseException pe) {
  500.    return false;
  501.   }
  502.   return true;
  503.  }
  504.  /***************************************************************************
  505.   * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
  506.   * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
  507.   **************************************************************************/
  508.  public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
  509.   Date currentTime = new Date();
  510.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  511.   String dateString = formatter.format(currentTime);
  512.   String s_nd = dateString.substring(0, 4); // 年份
  513.   String s_yf = dateString.substring(5, 7); // 月份
  514.   String s_rq = dateString.substring(8, 10); // 日期
  515.   String sreturn = "";
  516.   roc.util.MyChar mc = new roc.util.MyChar();
  517.   if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况
  518.    if (nd.equals("1")) {
  519.     sreturn = s_nd;
  520.     // 处理间隔符
  521.     if (format.equals("1"))
  522.      sreturn = sreturn + "";
  523.     else if (format.equals("2"))
  524.      sreturn = sreturn + "-";
  525.     else if (format.equals("3"))
  526.      sreturn = sreturn + "/";
  527.     else if (format.equals("5"))
  528.      sreturn = sreturn + ".";
  529.    }
  530.    // 处理月份
  531.    if (yf.equals("1")) {
  532.     sreturn = sreturn + s_yf;
  533.     if (format.equals("1"))
  534.      sreturn = sreturn + "";
  535.     else if (format.equals("2"))
  536.      sreturn = sreturn + "-";
  537.     else if (format.equals("3"))
  538.      sreturn = sreturn + "/";
  539.     else if (format.equals("5"))
  540.      sreturn = sreturn + ".";
  541.    }
  542.    // 处理日期
  543.    if (rq.equals("1")) {
  544.     sreturn = sreturn + s_rq;
  545.     if (format.equals("1"))
  546.      sreturn = sreturn + "";
  547.    }
  548.   } else {
  549.    // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
  550.    sdate = roc.util.RocDate.getOKDate(sdate);
  551.    s_nd = sdate.substring(0, 4); // 年份
  552.    s_yf = sdate.substring(5, 7); // 月份
  553.    s_rq = sdate.substring(8, 10); // 日期
  554.    if (nd.equals("1")) {
  555.     sreturn = s_nd;
  556.     // 处理间隔符
  557.     if (format.equals("1"))
  558.      sreturn = sreturn + "";
  559.     else if (format.equals("2"))
  560.      sreturn = sreturn + "-";
  561.     else if (format.equals("3"))
  562.      sreturn = sreturn + "/";
  563.     else if (format.equals("5"))
  564.      sreturn = sreturn + ".";
  565.    }
  566.    // 处理月份
  567.    if (yf.equals("1")) {
  568.     sreturn = sreturn + s_yf;
  569.     if (format.equals("1"))
  570.      sreturn = sreturn + "";
  571.     else if (format.equals("2"))
  572.      sreturn = sreturn + "-";
  573.     else if (format.equals("3"))
  574.      sreturn = sreturn + "/";
  575.     else if (format.equals("5"))
  576.      sreturn = sreturn + ".";
  577.    }
  578.    // 处理日期
  579.    if (rq.equals("1")) {
  580.     sreturn = sreturn + s_rq;
  581.     if (format.equals("1"))
  582.      sreturn = sreturn + "";
  583.    }
  584.   }
  585.   return sreturn;
  586.  }
  587.  public static String getNextMonthDay(String sdate, int m) {
  588.   sdate = getOKDate(sdate);
  589.   int year = Integer.parseInt(sdate.substring(0, 4));
  590.   int month = Integer.parseInt(sdate.substring(5, 7));
  591.   month = month + m;
  592.   if (month < 0) {
  593.    month = month + 12;
  594.    year = year - 1;
  595.   } else if (month > 12) {
  596.    month = month - 12;
  597.    year = year + 1;
  598.   }
  599.   String smonth = "";
  600.   if (month < 10)
  601.    smonth = "0" + month;
  602.   else
  603.    smonth = "" + month;
  604.   return year + "-" + smonth + "-10";
  605.  }
  606.  public static String getOKDate(String sdate) {
  607.   if (sdate == null || sdate.equals(""))
  608.    return getStringDateShort();
  609.   if (!VeStr.Isdate(sdate)) {
  610.    sdate = getStringDateShort();
  611.   }
  612.   // 将“/”转换为“-”
  613.   sdate = VeStr.Replace(sdate, "/", "-");
  614.   // 如果只有8位长度,则要进行转换
  615.   if (sdate.length() == 8)
  616.    sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
  617.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  618.   ParsePosition pos = new ParsePosition(0);
  619.   Date strtodate = formatter.parse(sdate, pos);
  620.   String dateString = formatter.format(strtodate);
  621.   return dateString;
  622.  }
  623.  public static void main(String[] args) throws Exception {
  624.   try {
  625.    //System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10", "2006-11-02 11:22:09")));
  626.   } catch (Exception e) {
  627.    throw new Exception();
  628.   }
  629.   //System.out.println("sss");
  630.  }
  631. }
原创粉丝点击