猿问

检查此日期是否超过新日期的一分钟

问题我该如何做一个条件来检查 dateReservation 是否超过新日期的一分钟!


代码


    Adresse entity = adresseDao.findOne(id);

    entity.setStatut("RESERVER");

    entity.setDateReservation(new Date()); 

    Date date1 = entity.getDateReservation();

    Date date2 = new Date();


    entity.setReserverPar(secUtilisateurService.getCurrentUser());

    adresseDao.save(entity);


12345678_0001
浏览 116回答 3
3回答

喵喵时光机

尝试这个try {&nbsp; &nbsp; &nbsp; &nbsp; SimpleDateFormat fmt = new SimpleDateFormat(DATE_YYYY_MM_ddTHH_MM_SS_Z);&nbsp; &nbsp; &nbsp; &nbsp; Date date1 = fmt.parse(entity.getDateReservation());&nbsp; &nbsp; &nbsp; &nbsp; Date date2&nbsp; = new Date();&nbsp; &nbsp; &nbsp; &nbsp; // Calculates the difference in milliseconds.&nbsp; &nbsp; &nbsp; &nbsp; long millisDiff = nowDate.getTime() - date1.getTime();&nbsp; &nbsp; &nbsp; &nbsp; int minutes = (int) (millisDiff / 60000 % 60);&nbsp; &nbsp; &nbsp; &nbsp; if( minutes<1 && minutes>=0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //what you want if false;&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; // log exception&nbsp; &nbsp; }...将可能的异常写入日志也可能很方便。你好!

江户川乱折腾

&nbsp; &nbsp;// thank you so much That's what I didDate date1 = adresse.getDateReservation();&nbsp; &nbsp; &nbsp; &nbsp; Date date2&nbsp; = new Date();&nbsp; &nbsp; &nbsp; &nbsp; long millisDiff = (date2.getTime() - date1.getTime());&nbsp; &nbsp; &nbsp; &nbsp; long minutes = (long) (millisDiff / (60000));&nbsp; &nbsp; &nbsp; &nbsp; if(minutes > 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adresse.setStatut(EnumStatutAdresse.LIBRE.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adresse.setReserverPar(null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; adresseDao.save(adresse);

慕哥6287543

if(date1.getTime()&nbsp;>=&nbsp;(date2.getTime()&nbsp;+&nbsp;TimeUnit.MINUTES.toMillis(1))){ &nbsp;.... }可能是一个简单的方法。这主要检查预订时间是否等于或大于当前时间后 1 分钟。比较以毫秒为单位进行。可能有更多方法可以做到这一点,具体取决于您拥有的库和 java 版本。
随时随地看视频慕课网APP

相关分类

Java
我要回答