java时间转换大全

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值