Java 将用户输入日期与当前日期进行比较
嗨,我正在尝试将用户输入的日期(作为字符串)与当前日期进行比较,以确定该日期是更早还是更早 我现在的代码是Java 将用户输入日期与当前日期进行比较,java,date,calendar,simpledateformat,Java,Date,Calendar,Simpledateformat,嗨,我正在尝试将用户输入的日期(作为字符串)与当前日期进行比较,以确定该日期是更早还是更早 我现在的代码是 String date; Date newDate; Date todayDate, myDate; SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy"); while(true) { Scanner s = new Scanner (System.in); date = s.ne
String date;
Date newDate;
Date todayDate, myDate;
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
while(true)
{
Scanner s = new Scanner (System.in);
date = s.nextLine();
Calendar cal = Calendar.getInstance();
try {
// trying to parse current date here
// newDate = dateFormatter.parse(cal.getTime().toString()); //throws exception
// trying to parse inputted date here
myDate = dateFormatter.parse(date); //no exception
} catch (ParseException e) {
e.printStackTrace(System.out);
}
}
我试图将用户输入日期和当前日期都放入两个日期对象中,这样我就可以使用date.compareTo()简化日期比较
我能够将用户输入字符串解析为日期对象。但是,由于当前日期cal.getTime().toString()是无效字符串,因此无法解析为日期对象
怎么做呢?提前感谢您可以通过以下方式获取当前的
日期
:
todayDate = new Date();
编辑:由于您需要比较日期而不考虑时间成分,因此我建议您查看以下内容:
尽管一个答案的形式很糟糕,但我实际上非常喜欢它:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.format(date1).equals(sdf.format(date2));
在您的情况下,您已经:
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
所以我会考虑(为了简单而不是表现):
Fomat没有像您预期的那样遵守
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime().toString());
输出:Fri Nov 01 13:46:52 EET 2013您可以这样做
// Make a Calendar whose DATE part is some time yesterday.
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.DATE, -1);
if (myDate.before(cal.getTime())) {
// myDate must be yesterday or earlier
} else {
// myDate must be today or later
}
cal
有时间成分并不重要,因为myDate
没有时间成分。因此,当您比较它们时,如果cal
和myDate
是相同的日期,则时间组件将使cal
晚于myDate
,而不管时间组件是什么。创建一个新的日期()将为您提供一个具有当前日期的日期对象
因此:
将执行此任务以下是检查给定日期时间是否大于当前日期时间的代码。下面的方法将特定日期时间字符串作为参数,返回true如果提供日期时间大于当前日期时间#刺耳的
private boolean isExpire(字符串日期){
如果(date.isEmpty()| | date.trim()等于(“”){
返回false;
}否则{
SimpleDateFormat sdf=新SimpleDateFormat(“MMM dd yyyy hh:mm:ss a”);//2015年1月20日下午1:30:55
日期d=空;
日期d1=空;
String today=getToday(“MMM-dd-yyyy hh:mm:ss a”);
试一试{
//System.out.println(“expdate>>”+日期);
//System.out.println(“今天>>”+今天+“\n\n”);
d=sdf.parse(日期);
d1=sdf.parse(今天);
如果(d1.与(d)比较public void onDataChange(DataSnapshot DataSnapshot){
//使用初始值调用此方法一次,然后再次调用
//每当更新此位置的数据时。
对于(DataSnapshot dataSnapshot1:DataSnapshot.getChildren()){
SimpleDataFormat sdf1234=新的SimpleDataFormat(“dd-MM-yyyy-hh:MM-a”);
字符串abs12=value.getExpiryData();
Date todayDate=新日期();
试一试{
datetestdate1=sdf1234.parse(abs12);
if(testDate1.compareTo(todayDate)java.time
我建议您使用java.time(现代java日期和时间API)进行日期工作
ZoneId zone = ZoneId.of("America/Santarem");
LocalDate today = LocalDate.now(zone);
String userInput = "14-04-2021";
LocalDate myDate = LocalDate.parse(userInput, DATE_PARSER);
if (myDate.isBefore(today)) {
System.out.println(userInput + " is in the past.");
} else if (myDate.isAfter(today)) {
System.out.println(userInput + " is in the future.");
} else {
System.out.println(userInput + " is today.");
}
输出为:
2021年4月14日已经过去
我使用此格式化程序进行解析:
private static final DateTimeFormatter DATE_PARSER
= DateTimeFormatter.ofPattern("dd-MM-uuuu");
你的代码出了什么问题?
我在你得到异常的那行上加了注释:
// trying to parse current date here
newDate = dateFormatter.parse(cal.getTime().toString()); //throws exception
果然,我得到了一个例外:
// trying to parse current date here
newDate = dateFormatter.parse(cal.getTime().toString()); //throws exception
java.text.ParseException:不可解析日期:“星期五4月16日07:37:00 CEST
2021“
您的SimpleDataFormat
尝试解析格式为dd-MM-yyyy
的字符串cal.getTime()
返回一个Date
,以及toString()中的值
在日期上
看起来像异常所说的那样,Fri Apr 16 07:37:00 CEST 2021
。这不是dd-MM-yyyy
格式(甚至没有关闭)。这就是解析失败并出现异常的原因
使用java.time解析今天的日期没有任何意义,因为您没有将其作为字符串。使用java 1.0和1.1中旧的、麻烦的日期类,它可以作为一种破解方法,以摆脱日期的时间部分,但在当时也不是推荐的方法
链接
解释如何使用java.time.谢谢。这也会将当前时间信息存储到日期中吗?是的,如果需要,可以删除它。我必须将其放入日历中才能正确修改时间?既然date.setHour已被折旧,为什么要修改时间?因为我只想比较用户输入日期到当前日期仅基于日期(而非时间)。compareTo()还比较时间,因此我想将时间设置为00:00:00以删除时间比较问题只是为了澄清-如果输入的日期是昨天,您希望返回true
,但是如果输入的日期是今天,则返回false
。对吗?是的,这就是我试图做的。假设我今天运行程序,则返回新的Date=new Date();给我(2013年11月1日晚上8点),如果用户输入2013年11月1日,它应该返回false,因为输入的日期是今天(无论何时)。对于这个问题的新读者,我建议您不要使用SimpleDateFormat
和Date
。这些类的设计很差,而且很早就过时了,前者特别麻烦。相反,请使用LocalDate
和DateTimeFormatter
,这两个类都来自。
ZoneId zone = ZoneId.of("America/Santarem");
LocalDate today = LocalDate.now(zone);
String userInput = "14-04-2021";
LocalDate myDate = LocalDate.parse(userInput, DATE_PARSER);
if (myDate.isBefore(today)) {
System.out.println(userInput + " is in the past.");
} else if (myDate.isAfter(today)) {
System.out.println(userInput + " is in the future.");
} else {
System.out.println(userInput + " is today.");
}
private static final DateTimeFormatter DATE_PARSER
= DateTimeFormatter.ofPattern("dd-MM-uuuu");
// trying to parse current date here
newDate = dateFormatter.parse(cal.getTime().toString()); //throws exception