Java时间格式转换大全

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