• JAVA时间格式转换大全


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