比较所选日期与当前日期,不能超过1天。

huangapple go评论101阅读模式
英文:

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(&quot;dd/MM/uuuu&quot;));
	
	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(&quot;dd/MM/uuuu&quot;));
	LocalDate today = LocalDate.now();
	return (! date.isBefore(today.minusDays(1)) &amp;&amp; ! 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(&quot;dd/MM/uuuu&quot;));
	return (Math.abs(ChronoUnit.DAYS.between(LocalDate.now(), date)) &lt;= 1);
}

huangapple
  • 本文由 发表于 2020年8月14日 10:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63405646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定