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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值