为什么日期相减的值的value

您所在的位置:网站首页 excel输入公式后显示value 为什么日期相减的值的value

为什么日期相减的值的value

#为什么日期相减的值的value| 来源: 网络整理| 查看: 265

1 public class DateUtils extendsPropertyEditorSupport {2 //各种时间格式

3 public static final SimpleDateFormat date_sdf = newSimpleDateFormat(4 "yyyy-MM-dd");5 //各种时间格式

6 public static final SimpleDateFormat yyyyMMdd = newSimpleDateFormat(7 "yyyyMMdd");8 //各种时间格式

9 public static final SimpleDateFormat date_sdf_wz = newSimpleDateFormat(10 "yyyy年MM月dd日");11 public static final SimpleDateFormat time_sdf = newSimpleDateFormat(12 "yyyy-MM-dd HH:mm");13 public static final SimpleDateFormat yyyymmddhhmmss = newSimpleDateFormat(14 "yyyyMMddHHmmss");15 public static final SimpleDateFormat short_time_sdf = newSimpleDateFormat(16 "HH:mm");17 public static final SimpleDateFormat datetimeFormat = newSimpleDateFormat(18 "yyyy-MM-dd HH:mm:ss");19 //以毫秒表示的时间

20 private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;21 private static final long HOUR_IN_MILLIS = 3600 * 1000;22 private static final long MINUTE_IN_MILLIS = 60 * 1000;23 private static final long SECOND_IN_MILLIS = 1000;24 //指定模式的时间格式

25 private staticSimpleDateFormat getSDFormat(String pattern) {26 return newSimpleDateFormat(pattern);27 }28

29 /**

30 * 当前日历,这里用中国时间表示31 *32 *@return以当地时区表示的系统当前日历33 */

34 public staticCalendar getCalendar() {35 returnCalendar.getInstance();36 }37

38 /**

39 * 指定毫秒数表示的日历40 *41 *@parammillis42 * 毫秒数43 *@return指定毫秒数表示的日历44 */

45 public static Calendar getCalendar(longmillis) {46 Calendar cal =Calendar.getInstance();47 cal.setTime(newDate(millis));48 returncal;49 }50 /**

51 * 时间转字符串52 *@return

53 */

54 public staticString date2SStr()55 {56 Date date=getDate();57 if (null ==date) {58 return null;59 }60 returnyyyyMMdd.format(date);61 }62

63 //

64 //getDate65 //各种方式获取的Date66 // 67

68 /**

69 * 当前日期70 *71 *@return系统当前时间72 */

73 public staticDate getDate() {74 return newDate();75 }76

77 /**

78 * 指定毫秒数表示的日期79 *80 *@parammillis81 * 毫秒数82 *@return指定毫秒数表示的日期83 */

84 public static Date getDate(longmillis) {85 return newDate(millis);86 }87

88 /**

89 * 时间戳转换为字符串90 *91 *@paramtime92 *@return

93 */

94 public staticString timestamptoStr(Timestamp time) {95 Date date = null;96 if (null !=time) {97 date = newDate(time.getTime());98 }99 returndate2Str(date_sdf);100 }101

102 /**

103 * 字符串转换时间戳104 *105 *@paramstr106 *@return

107 */

108 public staticTimestamp str2Timestamp(String str) {109 Date date =str2Date(str, date_sdf);110 return newTimestamp(date.getTime());111 }112 /**

113 * 字符串转换成日期114 *@paramstr115 *@paramsdf116 *@return

117 */

118 public staticDate str2Date(String str, SimpleDateFormat sdf) {119 if (null == str || "".equals(str)) {120 return null;121 }122 Date date = null;123 try{124 date =sdf.parse(str);125 returndate;126 } catch(ParseException e) {127 e.printStackTrace();128 }129 return null;130 }131

132 /**

133 * 日期转换为字符串134 *135 *@paramdate136 * 日期137 *@paramformat138 * 日期格式139 *@return字符串140 */

141 public staticString date2Str(SimpleDateFormat date_sdf) {142 Date date=getDate();143 if (null ==date) {144 return null;145 }146 returndate_sdf.format(date);147 }148 /**

149 * 格式化时间150 *@paramdata151 *@paramformat152 *@return

153 */

154 public staticString dataformat(String data,String format)155 {156 SimpleDateFormat sformat = newSimpleDateFormat(format);157 Date date=null;158 try{159 date=sformat.parse(data);160 } catch(ParseException e) {161 //TODO Auto-generated catch block

162 e.printStackTrace();163 }164 returnsformat.format(date);165 }166 /**

167 * 日期转换为字符串168 *169 *@paramdate170 * 日期171 *@paramformat172 * 日期格式173 *@return字符串174 */

175 public staticString date2Str(Date date, SimpleDateFormat date_sdf) {176 if (null ==date) {177 return null;178 }179 returndate_sdf.format(date);180 }181 /**

182 * 日期转换为字符串183 *184 *@paramdate185 * 日期186 *@paramformat187 * 日期格式188 *@return字符串189 */

190 public staticString getDate(String format) {191 Date date=newDate();192 if (null ==date) {193 return null;194 }195 SimpleDateFormat sdf = newSimpleDateFormat(format);196 returnsdf.format(date);197 }198

199 /**

200 * 指定毫秒数的时间戳201 *202 *@parammillis203 * 毫秒数204 *@return指定毫秒数的时间戳205 */

206 public static Timestamp getTimestamp(longmillis) {207 return newTimestamp(millis);208 }209

210 /**

211 * 以字符形式表示的时间戳212 *213 *@paramtime214 * 毫秒数215 *@return以字符形式表示的时间戳216 */

217 public staticTimestamp getTimestamp(String time) {218 return newTimestamp(Long.parseLong(time));219 }220

221 /**

222 * 系统当前的时间戳223 *224 *@return系统当前的时间戳225 */

226 public staticTimestamp getTimestamp() {227 return new Timestamp(newDate().getTime());228 }229

230 /**

231 * 指定日期的时间戳232 *233 *@paramdate234 * 指定日期235 *@return指定日期的时间戳236 */

237 public staticTimestamp getTimestamp(Date date) {238 return newTimestamp(date.getTime());239 }240

241 /**

242 * 指定日历的时间戳243 *244 *@paramcal245 * 指定日历246 *@return指定日历的时间戳247 */

248 public staticTimestamp getCalendarTimestamp(Calendar cal) {249 return newTimestamp(cal.getTime().getTime());250 }251

252 public staticTimestamp gettimestamp() {253 Date dt = newDate();254 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");255 String nowTime =df.format(dt);256 java.sql.Timestamp buydate =java.sql.Timestamp.valueOf(nowTime);257 returnbuydate;258 }259

260 //

261 //getMillis262 //各种方式获取的Millis263 // 264

265 /**

266 * 系统时间的毫秒数267 *268 *@return系统时间的毫秒数269 */

270 public static longgetMillis() {271 return newDate().getTime();272 }273

274 /**

275 * 指定日历的毫秒数276 *277 *@paramcal278 * 指定日历279 *@return指定日历的毫秒数280 */

281 public static longgetMillis(Calendar cal) {282 returncal.getTime().getTime();283 }284

285 /**

286 * 指定日期的毫秒数287 *288 *@paramdate289 * 指定日期290 *@return指定日期的毫秒数291 */

292 public static longgetMillis(Date date) {293 returndate.getTime();294 }295

296 /**

297 * 指定时间戳的毫秒数298 *299 *@paramts300 * 指定时间戳301 *@return指定时间戳的毫秒数302 */

303 public static longgetMillis(Timestamp ts) {304 returnts.getTime();305 }306

307 //

308 //formatDate309 //将日期按照一定的格式转化为字符串310 // 311

312 /**

313 * 默认方式表示的系统当前日期,具体格式:年-月-日314 *315 *@return默认日期按“年-月-日“格式显示316 */

317 public staticString formatDate() {318 returndate_sdf.format(getCalendar().getTime());319 }320 /**

321 * 获取时间字符串322 */

323 public staticString getDataString(SimpleDateFormat formatstr) {324 returnformatstr.format(getCalendar().getTime());325 }326 /**

327 * 指定日期的默认显示,具体格式:年-月-日328 *329 *@paramcal330 * 指定的日期331 *@return指定日期按“年-月-日“格式显示332 */

333 public staticString formatDate(Calendar cal) {334 returndate_sdf.format(cal.getTime());335 }336

337 /**

338 * 指定日期的默认显示,具体格式:年-月-日339 *340 *@paramdate341 * 指定的日期342 *@return指定日期按“年-月-日“格式显示343 */

344 public staticString formatDate(Date date) {345 returndate_sdf.format(date);346 }347

348 /**

349 * 指定毫秒数表示日期的默认显示,具体格式:年-月-日350 *351 *@parammillis352 * 指定的毫秒数353 *@return指定毫秒数表示日期按“年-月-日“格式显示354 */

355 public static String formatDate(longmillis) {356 return date_sdf.format(newDate(millis));357 }358

359 /**

360 * 默认日期按指定格式显示361 *362 *@parampattern363 * 指定的格式364 *@return默认日期按指定格式显示365 */

366 public staticString formatDate(String pattern) {367 returngetSDFormat(pattern).format(getCalendar().getTime());368 }369

370 /**

371 * 指定日期按指定格式显示372 *373 *@paramcal374 * 指定的日期375 *@parampattern376 * 指定的格式377 *@return指定日期按指定格式显示378 */

379 public staticString formatDate(Calendar cal, String pattern) {380 returngetSDFormat(pattern).format(cal.getTime());381 }382

383 /**

384 * 指定日期按指定格式显示385 *386 *@paramdate387 * 指定的日期388 *@parampattern389 * 指定的格式390 *@return指定日期按指定格式显示391 */

392 public staticString formatDate(Date date, String pattern) {393 returngetSDFormat(pattern).format(date);394 }395

396 //

397 //formatTime398 //将日期按照一定的格式转化为字符串399 // 400

401 /**

402 * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分403 *404 *@return默认日期按“年-月-日 时:分“格式显示405 */

406 public staticString formatTime() {407 returntime_sdf.format(getCalendar().getTime());408 }409 /**

410 * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分:秒411 *412 *@return默认日期按“年-月-日 时:分:秒“格式显示413 */

414 public static String formatTimeyyyyMMddHHmmss(longmillis) {415 return datetimeFormat.format(newDate(millis));416 }417 /**

418 * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分419 *420 *@parammillis421 * 指定的毫秒数422 *@return指定毫秒数表示日期按“年-月-日 时:分“格式显示423 */

424 public static String formatTime(longmillis) {425 return time_sdf.format(newDate(millis));426 }427

428 /**

429 * 指定日期的默认显示,具体格式:年-月-日 时:分430 *431 *@paramcal432 * 指定的日期433 *@return指定日期按“年-月-日 时:分“格式显示434 */

435 public staticString formatTime(Calendar cal) {436 returntime_sdf.format(cal.getTime());437 }438

439 /**

440 * 指定日期的默认显示,具体格式:年-月-日 时:分441 *442 *@paramdate443 * 指定的日期444 *@return指定日期按“年-月-日 时:分“格式显示445 */

446 public staticString formatTime(Date date) {447 returntime_sdf.format(date);448 }449

450 //

451 //formatShortTime452 //将日期按照一定的格式转化为字符串453 // 454

455 /**

456 * 默认方式表示的系统当前日期,具体格式:时:分457 *458 *@return默认日期按“时:分“格式显示459 */

460 public staticString formatShortTime() {461 returnshort_time_sdf.format(getCalendar().getTime());462 }463

464 /**

465 * 指定毫秒数表示日期的默认显示,具体格式:时:分466 *467 *@parammillis468 * 指定的毫秒数469 *@return指定毫秒数表示日期按“时:分“格式显示470 */

471 public static String formatShortTime(longmillis) {472 return short_time_sdf.format(newDate(millis));473 }474

475 /**

476 * 指定日期的默认显示,具体格式:时:分477 *478 *@paramcal479 * 指定的日期480 *@return指定日期按“时:分“格式显示481 */

482 public staticString formatShortTime(Calendar cal) {483 returnshort_time_sdf.format(cal.getTime());484 }485

486 /**

487 * 指定日期的默认显示,具体格式:时:分488 *489 *@paramdate490 * 指定的日期491 *@return指定日期按“时:分“格式显示492 */

493 public staticString formatShortTime(Date date) {494 returnshort_time_sdf.format(date);495 }496

497 //

498 //parseDate499 //parseCalendar500 //parseTimestamp501 //将字符串按照一定的格式转化为日期或时间502 // 503

504 /**

505 * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间506 *507 *@paramsrc508 * 将要转换的原始字符窜509 *@parampattern510 * 转换的匹配格式511 *@return如果转换成功则返回转换后的日期512 *@throwsParseException513 *@throwsAIDateFormatException514 */

515 public staticDate parseDate(String src, String pattern)516 throwsParseException {517 returngetSDFormat(pattern).parse(src);518

519 }520

521 /**

522 * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间523 *524 *@paramsrc525 * 将要转换的原始字符窜526 *@parampattern527 * 转换的匹配格式528 *@return如果转换成功则返回转换后的日期529 *@throwsParseException530 *@throwsAIDateFormatException531 */

532 public staticCalendar parseCalendar(String src, String pattern)533 throwsParseException {534

535 Date date =parseDate(src, pattern);536 Calendar cal =Calendar.getInstance();537 cal.setTime(date);538 returncal;539 }540

541 public static String formatAddDate(String src, String pattern, intamount)542 throwsParseException {543 Calendar cal;544 cal =parseCalendar(src, pattern);545 cal.add(Calendar.DATE, amount);546 returnformatDate(cal);547 }548

549 /**

550 * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间551 *552 *@paramsrc553 * 将要转换的原始字符窜554 *@parampattern555 * 转换的匹配格式556 *@return如果转换成功则返回转换后的时间戳557 *@throwsParseException558 *@throwsAIDateFormatException559 */

560 public staticTimestamp parseTimestamp(String src, String pattern)561 throwsParseException {562 Date date =parseDate(src, pattern);563 return newTimestamp(date.getTime());564 }565

566 //

567 //dateDiff568 //计算两个日期之间的差值569 // 570

571 /**

572 * 计算两个时间之间的差值,根据标志的不同而不同573 *574 *@paramflag575 * 计算标志,表示按照年/月/日/时/分/秒等计算576 *@paramcalSrc577 * 减数578 *@paramcalDes579 * 被减数580 *@return两个日期之间的差值581 */

582 public static int dateDiff(charflag, Calendar calSrc, Calendar calDes) {583

584 long millisDiff = getMillis(calSrc) -getMillis(calDes);585

586 if (flag == 'y') {587 return (calSrc.get(calSrc.YEAR) -calDes.get(calDes.YEAR));588 }589

590 if (flag == 'd') {591 return (int) (millisDiff /DAY_IN_MILLIS);592 }593

594 if (flag == 'h') {595 return (int) (millisDiff /HOUR_IN_MILLIS);596 }597

598 if (flag == 'm') {599 return (int) (millisDiff /MINUTE_IN_MILLIS);600 }601

602 if (flag == 's') {603 return (int) (millisDiff /SECOND_IN_MILLIS);604 }605

606 return 0;607 }608 /**

609 * String类型 转换为Date,610 * 如果参数长度为10 转换格式”yyyy-MM-dd“611 *如果参数长度为19 转换格式”yyyy-MM-dd HH:mm:ss“612 * *@paramtext613 * String类型的时间值614 */

615 public void setAsText(String text) throwsIllegalArgumentException {616 if(StringUtils.hasText(text)) {617 try{618 if (text.indexOf(":") == -1 && text.length() == 10) {619 setValue(this.date_sdf.parse(text));620 } else if (text.indexOf(":") > 0 && text.length() == 19) {621 setValue(this.datetimeFormat.parse(text));622 } else{623 throw newIllegalArgumentException(624 "Could not parse date, date format is error ");625 }626 } catch(ParseException ex) {627 IllegalArgumentException iae = newIllegalArgumentException(628 "Could not parse date: " +ex.getMessage());629 iae.initCause(ex);630 throwiae;631 }632 } else{633 setValue(null);634 }635 }636 public static intgetYear(){637 GregorianCalendar calendar=newGregorianCalendar();638 calendar.setTime(getDate());639 returncalendar.get(Calendar.YEAR);640 }641

642 public static void main(String[] args) throwsParseException {643 System.out.println(DateUtils.formatTimeyyyyMMddHHmmss(1462666089 * 1000L));644 System.out.println(DateUtils.getMillis(DateUtils.parseDate("2016-07-08 08:08:09", "yyyy-mm-dd hh:mm:ss")));645 }646 }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3