英文:
Compare selected date with current date and cannot more than 1 day
问题
I want to get the time that user key in and compare with current time and if the different more than 1 day, it will pop out an error message. What can i do?
我想获取用户输入的时间,并与当前时间进行比较,如果相差超过1天,就会弹出一个错误消息。我该怎么做?
Iterator i= rows.iterator(); // Iterating grid rows
while (i.hasNext()) {
FormRow row = (FormRow) i.next();
String menudt = row.get("menudate");
}
String CurrentDate;
Date date1 = new Date();
Date date2;
SimpleDateFormat dates = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
//Setting dates
date1.setTime(System.currentTimeMillis());
CurrentDate = dates.format(date1);
date2 = dates.parse(menudt + " " + "18:00:00"); //There are an error here it cannot change the string to date
date1 = dates.parse(CurrentDate);
long diff = Math.abs(date2.getTime() - date1.getTime());
long day = 86400000;
if (diff > day){
System.out.println("HAHAHAH");
}
英文:
Can I have the solution?
I want to get the time that user key in and compare with current time and if the different more than 1 day, it will pop out an error message. What can i do?
Iterator i= rows.iterator(); // Iterating grid rows
while (i.hasNext()) {
FormRow row = (FormRow) i.next();
String menudt = row.get("menudate");
}
String CurrentDate;
Date date1 = new Date();
Date date2;
SimpleDateFormat dates = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
//Setting dates
date1.setTime(System.currentTimeMillis());
CurrentDate = dates.format(date1);
date2 = dates.parse(menudt + " " + "18:00:00"); //There are an error here it cannot change the string to date
date1 = dates.parse(CurrentDate);
long diff = Math.abs(date2.getTime() - date1.getTime());
long day = 86400000;
if (diff > day){
System.out.println("HAHAHAH")
}
答案1
得分: 1
以下是翻译好的部分:
可以使用以下方法检查日期:
```lang-java
static boolean isValid(String dateString) {
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/MM/uuuu"));
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minusDays(1);
LocalDate tomorrow = today.plusDays(1);
if (date.isBefore(yesterday) || date.isAfter(tomorrow))
return false;
return true;
}
也可以缩写为:
static boolean isValid(String dateString) {
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/MM/uuuu"));
LocalDate today = LocalDate.now();
return (! date.isBefore(today.minusDays(1)) && ! date.isAfter(today.plusDays(1)));
}
或者,您可以使用以下方法,这更接近您尝试的方式:
static boolean isValid(String dateString) {
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/MM/uuuu"));
return (Math.abs(ChronoUnit.DAYS.between(LocalDate.now(), date)) <= 1);
}
<details>
<summary>英文:</summary>
You can check the date with this method:
```lang-java
static boolean isValid(String dateString) {
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/MM/uuuu"));
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minusDays(1);
LocalDate tomorrow = today.plusDays(1);
if (date.isBefore(yesterday) || date.isAfter(tomorrow))
return false;
return true;
}
Which can be abbreviated to:
static boolean isValid(String dateString) {
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/MM/uuuu"));
LocalDate today = LocalDate.now();
return (! date.isBefore(today.minusDays(1)) && ! date.isAfter(today.plusDays(1)));
}
Alternatively, you can do this, which is closer to what you were trying:
static boolean isValid(String dateString) {
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/MM/uuuu"));
return (Math.abs(ChronoUnit.DAYS.between(LocalDate.now(), date)) <= 1);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论