• Java时间日期格式转换Date转String和String转Date


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