1 Java时间格式转换大全2

3 import java.text.*;4 importjava.util.Calendar;5 public classVeDate {6 /**

7 * 获取现在时间8 *9 *@return返回时间类型 yyyy-MM-dd HH:mm:ss10 */

11 public staticDate getNowDate() {12 Date currentTime = newDate();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 returncurrentTime_2;18 }19 /**

20 * 获取现在时间21 *22 * @return返回短时间格式 yyyy-MM-dd23 */

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:ss41 */

42 public staticString getStringDate() {43 Date currentTime = newDate();44 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");45 String dateString =formatter.format(currentTime);46 returndateString;47 }48 /**

49 * 获取现在时间50 *51 *@return返回短时间字符串格式yyyy-MM-dd52 */

53 public staticString getStringDateShort() {54 Date currentTime = newDate();55 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");56 String dateString =formatter.format(currentTime);57 returndateString;58 }59 /**

60 * 获取时间 小时:分;秒 HH:mm:ss61 *62 *@return

63 */

64 public staticString getTimeShort() {65 SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");66 Date currentTime = newDate();67 String dateString =formatter.format(currentTime);68 returndateString;69 }70 /**

71 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss72 *73 *@paramstrDate74 *@return

75 */

76 public staticDate 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 returnstrtodate;81 }82 /**

83 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss84 *85 *@paramdateDate86 *@return

87 */

88 public staticString dateToStrLong(java.util.Date dateDate) {89 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");90 String dateString =formatter.format(dateDate);91 returndateString;92 }93 /**

94 * 将短时间格式时间转换为字符串 yyyy-MM-dd95 *96 *@paramdateDate97 *@paramk98 *@return

99 */

100 public staticString dateToStr(java.util.Date dateDate) {101 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");102 String dateString =formatter.format(dateDate);103 returndateString;104 }105 /**

106 * 将短时间格式字符串转换为时间 yyyy-MM-dd107 *108 *@paramstrDate109 *@return

110 */

111 public staticDate 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 returnstrtodate;116 }117 /**

118 * 得到现在时间119 *120 *@return

121 */

122 public staticDate getNow() {123 Date currentTime = newDate();124 returncurrentTime;125 }126 /**

127 * 提取一个月中的最后一天128 *129 *@paramday130 *@return

131 */

132 public static Date getLastDate(longday) {133 Date date = newDate();134 long date_3_hm = date.getTime() - 3600000 * 34 *day;135 Date date_3_hm_date = newDate(date_3_hm);136 returndate_3_hm_date;137 }138 /**

139 * 得到现在时间140 *141 *@return字符串 yyyyMMdd HHmmss142 */

143 public staticString getStringToday() {144 Date currentTime = newDate();145 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");146 String dateString =formatter.format(currentTime);147 returndateString;148 }149 /**

150 * 得到现在小时151 */

152 public staticString getHour() {153 Date currentTime = newDate();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 returnhour;159 }160 /**

161 * 得到现在分钟162 *163 *@return

164 */

165 public staticString getTime() {166 Date currentTime = newDate();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 returnmin;172 }173 /**

174 * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。175 *176 *@paramsformat177 * yyyyMMddhhmmss178 *@return

179 */

180 public staticString getUserDate(String sformat) {181 Date currentTime = newDate();182 SimpleDateFormat formatter = newSimpleDateFormat(sformat);183 String dateString =formatter.format(currentTime);184 returndateString;185 }186 --------------------------------------------------------------------------------------------------------------------------------

187

188 做成方法189

190 import java.util.*;191 import java.text.*;192 importjava.util.Calendar;193

194 public classVeDate {195 /**

196 * 获取现在时间197 *198 *@return返回时间类型 yyyy-MM-dd HH:mm:ss199 */

200 public staticDate getNowDate() {201 Date currentTime = newDate();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 returncurrentTime_2;207 }208

209 /**

210 * 获取现在时间211 *212 * @return返回短时间格式 yyyy-MM-dd213 */

214 public staticDate getNowDateShort() {215 Date currentTime = newDate();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 returncurrentTime_2;221 }222

223 /**

224 * 获取现在时间225 *226 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss227 */

228 public staticString getStringDate() {229 Date currentTime = newDate();230 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");231 String dateString =formatter.format(currentTime);232 returndateString;233 }234

235 /**

236 * 获取现在时间237 *238 *@return返回短时间字符串格式yyyy-MM-dd239 */

240 public staticString getStringDateShort() {241 Date currentTime = newDate();242 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");243 String dateString =formatter.format(currentTime);244 returndateString;245 }246

247 /**

248 * 获取时间 小时:分;秒 HH:mm:ss249 *250 *@return

251 */

252 public staticString getTimeShort() {253 SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");254 Date currentTime = newDate();255 String dateString =formatter.format(currentTime);256 returndateString;257 }258

259 /**

260 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss261 *262 *@paramstrDate263 *@return

264 */

265 public staticDate 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 returnstrtodate;270 }271

272 /**

273 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss274 *275 *@paramdateDate276 *@return

277 */

278 public staticString dateToStrLong(java.util.Date dateDate) {279 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");280 String dateString =formatter.format(dateDate);281 returndateString;282 }283

284 /**

285 * 将短时间格式时间转换为字符串 yyyy-MM-dd286 *287 *@paramdateDate288 *@paramk289 *@return

290 */

291 public staticString dateToStr(java.util.Date dateDate) {292 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");293 String dateString =formatter.format(dateDate);294 returndateString;295 }296

297 /**

298 * 将短时间格式字符串转换为时间 yyyy-MM-dd299 *300 *@paramstrDate301 *@return

302 */

303 public staticDate 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 returnstrtodate;308 }309

310 /**

311 * 得到现在时间312 *313 *@return

314 */

315 public staticDate getNow() {316 Date currentTime = newDate();317 returncurrentTime;318 }319

320 /**

321 * 提取一个月中的最后一天322 *323 *@paramday324 *@return

325 */

326 public static Date getLastDate(longday) {327 Date date = newDate();328 long date_3_hm = date.getTime() - 3600000 * 34 *day;329 Date date_3_hm_date = newDate(date_3_hm);330 returndate_3_hm_date;331 }332

333 /**

334 * 得到现在时间335 *336 *@return字符串 yyyyMMdd HHmmss337 */

338 public staticString getStringToday() {339 Date currentTime = newDate();340 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");341 String dateString =formatter.format(currentTime);342 returndateString;343 }344

345 /**

346 * 得到现在小时347 */

348 public staticString getHour() {349 Date currentTime = newDate();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 returnhour;355 }356

357 /**

358 * 得到现在分钟359 *360 *@return

361 */

362 public staticString getTime() {363 Date currentTime = newDate();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 returnmin;369 }370

371 /**

372 * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。373 *374 *@paramsformat375 * yyyyMMddhhmmss376 *@return

377 */

378 public staticString getUserDate(String sformat) {379 Date currentTime = newDate();380 SimpleDateFormat formatter = newSimpleDateFormat(sformat);381 String dateString =formatter.format(currentTime);382 returndateString;383 }384

385 /**

386 * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟387 */

388 public staticString 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 staticString 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 staticString 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 returnmydate1;435 }436

437 /**

438 * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数439 */

440 public staticString 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 returnmdate;449 }catch(Exception e){450 return "";451 }452 }453

454 /**

455 * 判断是否润年456 *457 *@paramddate458 *@return

459 */

460 public static booleanisLeapYear(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 2006483 *484 *@paramstr485 *@return

486 */

487 public staticString 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 *@paramdat500 *@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 returnstr;518 }519

520 /**

521 * 判断二个时间是否在同一个周522 *523 *@paramdate1524 *@paramdate2525 *@return

526 */

527 public static booleanisSameWeekDates(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 staticString 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 *@paramsdate565 *@paramnum566 *@return

567 */

568 public staticString 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 *@paramsdate594 *@return

595 */

596 public staticString 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~7603 //1=星期日 7=星期六,其他类推

604 return new SimpleDateFormat("EEEE").format(c.getTime());605 }606 public staticString 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 returnstr;625 }626

627 /**

628 * 两个时间之间的天数629 *630 *@paramdate1631 *@paramdate2632 *@return

633 */

634 public static longgetDays(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 returnday;650 }651

652 /**

653 * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间654 * 此函数返回该日历第一行星期日所在的日期655 *656 *@paramsdate657 *@return

658 */

659 public staticString 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 returnnewday;670 }671

672 /**

673 * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数674 *675 *@paramk676 * 表示是取几位随机数,可以自己定677 */

678

679 public static String getNo(intk) {680

681 return getUserDate("yyyyMMddhhmmss") +getRandom(k);682 }683

684 /**

685 * 返回一个随机数686 *687 *@parami688 *@return

689 */

690 public static String getRandom(inti) {691 Random jjj = newRandom();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 returnjj;700 }701

702 /**

703 *704 *@paramargs705 */

706 public static booleanRightDate(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表示返回的格式 1727 * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回728 **************************************************************************/

729 public staticString getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {730 Date currentTime = newDate();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 = newroc.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 returnsreturn;807 }808

809 public static String getNextMonthDay(String sdate, intm) {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 staticString getOKDate(String sdate) {830 if (sdate == null || sdate.equals(""))831 returngetStringDateShort();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 returndateString;846 }847

848 public static void main(String[] args) throwsException {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 newException();853 }854 //System.out.println("sss");

855 }

java日期转字符串_Java时间日期格式转换Date转String和String转Date相关推荐

  1. 字符串与时间的格式转换

    字符串与时间的格式转换 这里可以使用hutool的DateUtil进行转换,支持多种格式的时间进行转换 import cn.hutool.core.date.DateTime; import cn.h ...

  2. java时间格式转换_Java时间日期格式转换

    突然忘记了时间格式怎么转换,特此做个记录 Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate ...

  3. java格式化日期时分秒_java的日期格式使用

    java.util.Date java.sql.Date  java.sql.Time  java.sql.Timestamp java.text.SimpleDateFormat java.util ...

  4. java 正则表达式 判断 日期_怎么在java中利用正则表达式对时间日期进行判断

    怎么在java中利用正则表达式对时间日期进行判断 发布时间:2020-12-08 16:18:34 来源:亿速云 阅读:105 作者:Leah 这篇文章给大家介绍怎么在java中利用正则表达式对时间日 ...

  5. date日期相减 java_03时间日期类

    Java8 在 java.time 包中增加了时间日期相关的API,弥补了 Java8 以前对日期.时间处理的不足. 在介绍Java8新的时间日期API前,先看看 java8 以前我们操作日期时间常用 ...

  6. stata:时间变量格式转换

    stata:时间变量格式转换 时间序列是经济金融类研究最常用的数据类型,时间变量的转换和设定一般是初学者最为头大的问题,本文在这里详尽的展示有关时间变量处理.转换以及格式设定的相关问题,话不多说直奔主 ...

  7. java转日期_Java时间日期格式转换

    import java.util.*;import java.text.*;importjava.util.Calendar;public classVeDate {/*** 获取现在时间 * *@r ...

  8. java int转日期_Java时间日期格式转换

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

  9. java 日期处理 口诀_java时间处理常用方法工具类

    java时间处理常用方法工具类 . import java.io.PrintStream; 2 import java.sql.Time; 3 import java.sql.Timestamp; 4 ...

最新文章

  1. 颠覆性技术的兴起及其影响
  2. Facebook开源AI硬件 联手NVIDIA开发人工智能
  3. 【新技术】不用开发者账号申请ios证书真机调试
  4. 从Java 8中的java.util.stream.Stream检索列表
  5. 计算机对英语口语考试,计算机辅助高考英语口语考试中题型的设计与交际能力的实现.pdf...
  6. vsftpd配置文件详细介绍
  7. php简单文件上传类
  8. linux进程作为服务,将一个监视进程做成linux系统服务
  9. 车险三者险可以垫付医药费吗?
  10. AI 人才缺失催生跨境猎头:人才年薪高达 300 万,猎头直赚 100 万
  11. Java 高效编程之 Builder 模式
  12. 只精通python_七步精通Python机器学习--转载
  13. BZOJ_4196_[NOI2015]_软件包管理器_(树链剖分)
  14. python中node函数的意思_Node.js 函数
  15. 魅族插了卡显示无服务器,魅族手机SD卡无法读取怎么办解决方案
  16. 工作效率低如何解决?
  17. Deepin和Windows10双系统,如何修改默认启动项
  18. python函数教程:len()方法
  19. [侯捷]C++ STL 体系结构与内核分析--从平地到万丈高楼(数据结构)
  20. LeetCode - 1419 数青蛙

热门文章

  1. 帝国cms更新报错解决办法
  2. 怎么申请微信小程序流程_小程序发布需要什么资质
  3. 基于内容的图像检索技术
  4. 计算机网络宽带越大 网速越快,100M宽带, 网速却很慢, 测速只有60M? 一分钟学会解决办法...
  5. Android 如何优雅的实现控件计时功能
  6. python中flatten_Python中flatten用法
  7. 论文总结-交通预测(未完成)
  8. [AHK]在当前目录中运行DOS命令行--DosHere
  9. ZJM 与纸条(KMP算法)
  10. 使用Pandas绘制股票均线