java速度_java里面如何提升编写速度

1 importjava.text.SimpleDateFormat;2 importjava.util.Calendar;3 importjava.util.Date;4 importjava.util.Locale;5 importjava.util.TimeZone;6 importorg.slf4j.Logger;7 importorg.slf4j.LoggerFactory;8

9 /**

10 *@author: ice11 * @create on:2018-04-12 23:2512 */

13 public classDateUtil {14

15 private static final Logger logger = LoggerFactory.getLogger(ODateu.class);16

17 public static final long SECOND = 1 * 1000;18 public static final long MINUTE = 60 *SECOND;19 public static final long HOUR = 60 *MINUTE;20 public static final long DAY = 24 *HOUR;21 public static final long WEEK = 7 *DAY;22 public static final TimeZone tz =TimeZone.getDefault();23

24 /**

25 * 入参格式: Sat Nov 01 14:01:55 CST 2014.26 */

27 public static finalDate parseLocale(String date) {28 if (date == null) {29 return null;30 }31 try{32 return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(date);33 } catch(Exception e) {34 if(logger.isDebugEnabled()) {35 logger.debug("{}, date: {}", Misc.trace(e), date);36 }37 return null;38 }39 }40

41 /**

42 * 入参格式: yyyy.43 */

44 public static finalDate parseDateyyyy(String date) {45 if (date == null) {46 return null;47 }48 try{49 return new SimpleDateFormat("yyyy").parse(date);50 } catch(Exception e) {51 if(logger.isDebugEnabled()) {52 logger.debug("{}, date: {}", Misc.trace(e), date);53 }54 return null;55 }56 }57

58 /**

59 * 入参格式: yyyy-MM.60 */

61 public static finalDate parseDateyyyy_MM(String date) {62 if (date == null) {63 return null;64 }65 try{66 return new SimpleDateFormat("yyyy-MM").parse(date);67 } catch(Exception e) {68 if(logger.isDebugEnabled()) {69 logger.debug("{}, date: {}", Misc.trace(e), date);70 }71 return null;72 }73 }74

75 /**

76 * 入参格式: yyyyMMdd.77 */

78 public static finalDate parseDateyyyyMMdd(String date) {79 if (date == null) {80 return null;81 }82 try{83 return new SimpleDateFormat("yyyyMMdd").parse(date);84 } catch(Exception e) {85 if(logger.isDebugEnabled()) {86 logger.debug("{}, date: {}", Misc.trace(e), date);87 }88 return null;89 }90 }91

92 /**

93 * 入参格式: yyyy-MM-dd.94 */

95 public static finalDate parseDateyyyy_MM_dd(String date) {96 if (date == null) {97 return null;98 }99 try{100 return new SimpleDateFormat("yyyy-MM-dd").parse(date);101 } catch(Exception e) {102 if(logger.isDebugEnabled()) {103 logger.debug("{}, date: {}", Misc.trace(e), date);104 }105 return null;106 }107 }108

109 /**

110 * 解析yyyyMMddHH格式的日期.111 */

112 public static finalDate parseDateyyyyMMddHH(String date) {113 if (date == null) {114 return null;115 }116 try{117 return new SimpleDateFormat("yyyyMMddHH").parse(date);118 } catch(Exception e) {119 if(logger.isDebugEnabled()) {120 logger.debug("{}, date: {}", Misc.trace(e), date);121 }122 return null;123 }124 }125

126 /**

127 * 解析yyyyMMddHHmm格式的日期.128 */

129 public static finalDate parseDateyyyyMMddHHmm(String date) {130 if (date == null) {131 return null;132 }133 try{134 return new SimpleDateFormat("yyyyMMddHHmm").parse(date);135 } catch(Exception e) {136 if(logger.isDebugEnabled()) {137 logger.debug("{}, date: {}", Misc.trace(e), date);138 }139 return null;140 }141 }142

143 /**

144 * 解析yyyyMMddHHmmss格式的日期.145 */

146 public static finalDate parseDateyyyyMMddHHmmss(String date) {147 if (date == null) {148 return null;149 }150 try{151 return new SimpleDateFormat("yyyyMMddHHmmss").parse(date);152 } catch(Exception e) {153 if(logger.isDebugEnabled()) {154 logger.debug("{}, date: {}", Misc.trace(e), date);155 }156 return null;157 }158 }159

160 /**

161 * 解析yyyy-MM-dd HH:mm:ss格式的日期.162 */

163 public static finalDate parseDateyyyy_MM_dd_HH_mm_ss(String date) {164 if (date == null) {165 return null;166 }167 try{168 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);169 } catch(Exception e) {170 if(logger.isDebugEnabled()) {171 logger.debug("{}, date: {}", Misc.trace(e), date);172 }173 return null;174 }175 }176

177 /**

178 * 返回格式: yyyy-MM-dd.179 */

180 public static finalString parseDateyyyy_MM_dd(Date date) {181 return ODateu.parse(new SimpleDateFormat("yyyy-MM-dd"), date);182 }183

184 /**

185 * 返回格式: yyyy-MM.186 */

187 public static finalString parseDateyyyy_MM(Date date) {188 return ODateu.parse(new SimpleDateFormat("yyyy-MM"), date);189 }190

191 /**

192 * 返回格式:yyyy-MM-dd HH:mm:ss.193 */

194 public static finalString parseDateyyyyMMddHHmmss(Date date) {195 return ODateu.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), date);196 }197

198 /**

199 * 返回格式:yyyy/MM/dd HH:mm.200 */

201 public static finalString parseDateyyyyMMddHHmm2(Date date) {202 return ODateu.parse(new SimpleDateFormat("yyyy/MM/dd HH:mm"), date);203 }204

205 /**

206 * 返回格式:yyyyMMdd.207 */

208 public static finalString parseDateyyyyMMdd(Date date) {209 return ODateu.parse(new SimpleDateFormat("yyyyMMdd"), date);210 }211

212 /**

213 * 返回格式:yyyyMMddHH.214 */

215 public static finalString parseDateyyyyMMddHH(Date date) {216 return ODateu.parse(new SimpleDateFormat("yyyyMMddHH"), date);217 }218

219 /**

220 * 返回格式:yyyyMMddHHmmss.221 */

222 public static finalString parseDateyyyyMMddHHmmss2(Date date) {223 return ODateu.parse(new SimpleDateFormat("yyyyMMddHHmmss"), date);224 }225

226 /**

227 * 返回格式:yyyyMMddHHmm.228 */

229 public static finalString parseDateyyyyMMddHHmm(Date date) {230 return ODateu.parse(new SimpleDateFormat("yyyyMMddHHmm"), date);231 }232

233 /**

234 * 返回格式:MMddHHmmss.235 */

236 public static finalString parseDateMMddHHmmss(Date date) {237 return ODateu.parse(new SimpleDateFormat("MMddHHmmss"), date);238 }239

240 /**

241 * 返回格式:HH:mm:ss.242 */

243 public static finalString parseDateHHmmss(Date date) {244 return ODateu.parse(new SimpleDateFormat("HH:mm:ss"), date);245 }246

247 /**

248 * 返回格式: HH:mm:ss.ms.249 */

250 public static finalString parseDateHHmmssms(Date date) {251 long ms = date.getTime() % 1000;252 return ODateu.parse(new SimpleDateFormat("HH:mm:ss"), date) + "."

253 + (ms > 99 ? ms : (ms > 9 ? ("0" + ms) : ("00" +ms)));254 }255

256 /**

257 * 返回格式:yyyy-MM-dd HH:mm:ss.ms.258 */

259 public static finalString parseDateyyyyMMddHHmmssms(Date date) {260 long ms = date.getTime() % 1000;261 return ODateu.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), date) + "."

262 + (ms > 99 ? ms : (ms > 9 ? ("0" + ms) : ("00" +ms)));263 }264

265 /**

266 * 返回格式:yyyyMMddHHmmssms.267 */

268 public static finalString parseDateyyyyMMddHHmmssms2(Date date) {269 long ms = date.getTime() % 1000;270 return ODateu.parseDateyyyyMMddHHmmss2(date) + (ms > 99 ?ms271 : (ms > 9 ? ("0" + ms) : ("00" +ms)));272 }273

274 /**

275 * 置为凌晨00:00:00 000,Calendar提供的set函数.276 */

277 public static finalDate set000000(Date date) {278 Calendar cal =Calendar.getInstance();279 cal.setTimeInMillis(date.getTime() - (date.getTime() % 1000));280 cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0,281 0);282 returncal.getTime();283 }284

285 /**

286 * 当前时间的hour, 小于10时前面补零.287 */

288 public static finalString hour(Date date) {289 Calendar cal =Calendar.getInstance();290 cal.setTime(date);291 int hour =cal.get(Calendar.HOUR_OF_DAY);292 return hour > 9 ? hour + "" : "0" +hour;293 }294

295 /**

296 * 返回秒(0 ~ 59).297 */

298 public static final intsecondInt(Date date) {299 Calendar cal =Calendar.getInstance();300 cal.setTime(date);301 returncal.get(Calendar.SECOND);302 }303

304 /**

305 * 返回分钟(0 ~ 59).306 */

307 public static final intminuteInt(Date date) {308 Calendar cal =Calendar.getInstance();309 cal.setTime(date);310 returncal.get(Calendar.MINUTE);311 }312

313 /**

314 * 返回小时(0 ~ 23).315 */

316 public static final inthourInt(Date date) {317 Calendar cal =Calendar.getInstance();318 cal.setTime(date);319 returncal.get(Calendar.HOUR_OF_DAY);320 }321

322 /**

323 * 返回天.324 */

325 public static final intdayInt(Date date) {326 Calendar cal =Calendar.getInstance();327 cal.setTime(date);328 returncal.get(Calendar.DAY_OF_MONTH);329 }330

331 /**

332 * 返回星期.333 */

334 public static final intweekInt(Date date) {335 Calendar cal =Calendar.getInstance();336 cal.setTime(date);337 returncal.get(Calendar.WEEK_OF_YEAR);338 }339

340 /**

341 * 星期几? (周期一返回1, 星期天返回7).342 */

343 public static final intweek(Date date) {344 Calendar cal =Calendar.getInstance();345 cal.setTime(date);346 int week =cal.get(Calendar.DAY_OF_WEEK);347 week -= 1;348 return week < 1 ? 7: week;349 }350

351 /**

352 * 返回月份.353 */

354 public static final intmonthInt(Date date) {355 Calendar cal =Calendar.getInstance();356 cal.setTime(date);357 return cal.get(Calendar.MONTH) + 1;358 }359

360 /**

361 * 返回年份.362 */

363 public static final intyearInt(Date date) {364 Calendar cal =Calendar.getInstance();365 cal.setTime(date);366 returncal.get(Calendar.YEAR);367 }368

369 /**

370 * yyyymm整数形式.371 */

372 public static final intyyyymm(Date date) {373 Calendar cal =Calendar.getInstance();374 cal.setTime(date);375 return cal.get(Calendar.YEAR) * 100 + (cal.get(Calendar.MONTH) + 1);376 }377

378 /**

379 * yyyymmdd整数形式.380 */

381 public static final intyyyymmdd(Date date) {382 Calendar cal =Calendar.getInstance();383 cal.setTime(date);384 return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 +cal385 .get(Calendar.DAY_OF_MONTH);386 }387

388 /**

389 * 返回这个月的总天数.390 */

391 public static final intmonthDays(Date date) {392 Calendar cal =Calendar.getInstance();393 cal.setTime(date);394 returncal.getActualMaximum(Calendar.DAY_OF_MONTH);395 }396

397 /**

398 * 返回这个月的最后一天(时分秒跟随).399 */

400 public static finalDate montheLastDay(Date date) {401 Calendar cal =Calendar.getInstance();402 cal.setTime(date);403 Calendar ret =Calendar.getInstance();404 ret.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),405 cal.getActualMaximum(Calendar.DAY_OF_MONTH),406 cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));407 returnret.getTime();408 }409

410 /**

411 * 返回这个月的第一天(00:00:00).412 */

413 public static finalDate monthFirstDay000000(Date date) {414 Calendar cal =Calendar.getInstance();415 cal.setTime(date);416 Calendar ret =Calendar.getInstance();417 ret.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),418 cal.getActualMinimum(Calendar.DAY_OF_MONTH), 0, 0, 0);419 returnret.getTime();420 }421

422 /**

423 * 返回这个月的最后一天(23:59:59).424 */

425 public static finalDate monthLastDay235959(Date date) {426 Calendar cal =Calendar.getInstance();427 cal.setTime(date);428 Calendar ret =Calendar.getInstance();429 ret.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),430 cal.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59,431 59);432 returnret.getTime();433 }434

435 /**

436 * 返回这一年的第一天(00:00:00).437 */

438 public static finalDate yearFirstDay000000(Date date) {439 Calendar cal =Calendar.getInstance();440 cal.setTime(date);441 Calendar ret =Calendar.getInstance();442 ret.set(cal.get(Calendar.YEAR), cal.getActualMinimum(Calendar.MONTH),443 cal.getActualMinimum(Calendar.DAY_OF_MONTH), 0, 0, 0);444 returnret.getTime();445 }446

447 /**

448 * 返回这一年的最后一天(23:59:59).449 */

450 public static finalDate yearLastDay235959(Date date) {451 Calendar cal =Calendar.getInstance();452 cal.setTime(date);453 Calendar ret =Calendar.getInstance();454 ret.set(cal.get(Calendar.YEAR), cal.getActualMaximum(Calendar.MONTH),455 cal.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);456 returnret.getTime();457 }458

459 /**

460 * 获取当前系统时区.461 */

462 public static final intgetTimezone() {463 return (int) (ODateu.tz.getRawOffset() /ODateu.HOUR);464 }465

466 /**

467 * 将本地时间转换成指定时区的时间.468 */

469 public static final long changeLocalTimeZone(long ts /*本地时间, 毫秒.*/,470 int gmt /*指定时区偏移, 小时 .*/) {471 return (ts - ODateu.tz.getRawOffset() /*回归零时区.*/) + (gmt *ODateu.HOUR);472 }473

474 /**

475 * 将本地时间转换成指定时区的时间.476 */

477 public static final Date changeLocalTimeZone2date(long ts /*本地时间, 毫秒.*/,478 int gmt /*指定时区偏移, 小时 .*/) {479 return newDate(ODateu.changeLocalTimeZone(ts, gmt));480 }481

482 /**

483 * 返回当前时间在零时区的绝对时间.484 */

485 public static finalDate nowGmt0() {486 return new Date(System.currentTimeMillis() - ODateu.tz.getRawOffset() /*回归零时区.*/);487 }488

489 /**

490 * 将指定GM+0时间回归到GMT+x.491 */

492 public static final Date gotoGmtxOld(Date date /*具有gmt0时区的绝对时间.*/, int gmt /*要返回的时区.*/) {493 return new Date(date.getTime() + gmt *ODateu.HOUR);494 }495

496 /**

497 * 将指定时间回归到GMT+0.498 */

499 public static final Date gotoGmt0Old(Date date /*具有gmt时区的绝对时间.*/, int gmt /*date的时区.*/) {500 return new Date((date.getTime() - gmt *ODateu.HOUR));501 }502

503 /**

504 * 将本地时区绝对时间转换成目标时区的绝对时间.505 */

506 public static final Date gotoGmtx(long ts /*本时绝对时间.*/, int gmtSec /*要返回的时区(秒)*/) {507 return newDate(508 (ts - ODateu.tz.getRawOffset() /*去零时区.*/) + (gmtSec * ODateu.SECOND /*去目标时区.*/));509 }510

511 /**

512 * 将指定GMT+x时间回归到GMT+0.513 */

514 public static final Date gmtxGoto0(Date date /*具有gmtSec时区的绝对时间.*/, int gmtSec /*date的时区.*/) {515 return new Date((date.getTime() - gmtSec *ODateu.SECOND));516 }517

518 /**

519 * 将指定GM+0时间回归到GMT+x.520 */

521 public static final Date gmt0Gotox(Date date /*具有gmt0时区的绝对时间.*/, int gmtSec /*要返回的时区(秒).*/) {522 return new Date(date.getTime() + gmtSec *ODateu.SECOND);523 }524

525 /**

526 * 本地时间去零时区.527 */

528 public static final Date gotoGmt0(Date date /*具有本地时区的时间*/) {529 return new Date(date.getTime() -ODateu.tz.getRawOffset());530 }531

532 /**

533 * 零时区时间去本地时区.534 */

535 public static final Date gotoLocal(Date date/*具有0时区的时间.*/) {536 return new Date(date.getTime() +ODateu.tz.getRawOffset());537 }538

539 /**

540 * 判断两个日期是否在同一天.541 */

542 public static final booleanisSameDay(Date arg0, Date arg1) {543 return (ODateu.yearInt(arg0) == ODateu.yearInt(arg1)) && //544 (ODateu.monthInt(arg0) == ODateu.monthInt(arg1)) && //545 (ODateu.dayInt(arg0) ==ODateu.dayInt(arg1));546 }547

548 /**

549 * 构造一个给定的时间.550 */

551 public static final Date createDate(int year, int month, int day, int hourOfDay, intminute,552 intsecond) {553 Calendar cal =Calendar.getInstance();554 cal.set(year, month, day, hourOfDay, minute, second);555 returncal.getTime();556 }557

558 /**

559 * 时间滚动, 按秒钟, up == true向今后滚动, 否则向以前滚动.560 */

561 public static final Date dateRollOfSecond(Date date, int amount, booleanup) {562 return up ? new Date(date.getTime() + ((long) amount) *ODateu.SECOND)563 : new Date(date.getTime() - ((long) amount) *ODateu.SECOND);564 }565

566 /**

567 * 时间滚动, 按分钟, up == true向今后滚动, 否则向以前滚动.568 */

569 public static final Date dateRollOfMinute(Date date, int amount, booleanup) {570 return up ? new Date(date.getTime() + ((long) amount) *ODateu.MINUTE)571 : new Date(date.getTime() - ((long) amount) *ODateu.MINUTE);572 }573

574 /**

575 * 时间滚动, 按小时, up == true向今后滚动, 否则向以前滚动.576 */

577 public static final Date dateRollOfHour(Date date, int amount, booleanup) {578 return up ? new Date(date.getTime() + ((long) amount) *ODateu.HOUR)579 : new Date(date.getTime() - ((long) amount) *ODateu.HOUR);580 }581

582 /**

583 * 时间滚动, 按天, up == true向今后滚动, 否则向以前滚动.584 */

585 public static final Date dateRollOfDay(Date date, int amount, booleanup) {586 return up ? new Date(date.getTime() + ((long) amount) *ODateu.DAY)587 : new Date(date.getTime() - ((long) amount) *ODateu.DAY);588 }589

590 /**

591 * 时间滚动, 按月, up == true向今后滚动, 否则向以前滚动.592 */

593 public static final Date dateRollOfMonth(Date date, booleanup) {594 Calendar ca =Calendar.getInstance();595 ca.setTime(date);596 ca.roll(Calendar.MONTH, up);597 int m =ODateu.monthInt(date);598 if (m == 1 && !up) {599 ca.roll(Calendar.YEAR, false);600 }601 if (m == 12 &&up) {602 ca.roll(Calendar.YEAR, true);603 }604 returnca.getTime();605 }606

607 /**

608 * 时间滚动, 按年, up == true向今后滚动, 否则向以前滚动.609 */

610 public static final Date dateRollOfYear(Date date, booleanup) {611 Calendar ca =Calendar.getInstance();612 ca.setTime(date);613 ca.roll(Calendar.YEAR, up);614 returnca.getTime();615 }616

617 /**

618 * 清除分钟.619 */

620 public static finalDate clearMinute(Date date) {621 return new Date(date.getTime() - (date.getTime() %ODateu.HOUR));622 }623

624 /**

625 * 清除小时.626 */

627 public static finalDate clearHour(Date date) {628 returnODateu.set000000(date);629 }630

631 /**

632 * 秒转换为毫秒, 出现这个函数的原因时, 当前时间的秒数 * 1000后总是整数(4字节)溢出, 此函数则可避免出错.633 */

634 public static final long sec2msec(longsec) {635 return sec * 1000L;636 }637

638 /**

639 * 按格式解析日期.640 */

641 private static finalString parse(SimpleDateFormat format, Date date) {642 try{643 return date == null ? null: format.format(date);644 } catch(Exception e) {645 if(logger.isDebugEnabled()) {646 logger.debug("{}", Misc.trace(e));647 }648 return null;649 }650 }651 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值