java - 解析日期在 webapp 中出现错误
问题描述
我在我的网络应用程序中使用以下函数来比较两个日期是否相等或之后或之前。
public class DateComareClass{
public static void main(String args[]) throws ParseException, IOException {
DateTimeComparator dateTimeComparator=DateTimeComparator.getDateOnlyInstance();
Calendar cal = Calendar.getInstance();
DateFormat dftreg = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = new Date(cal.getTime().getTime());
Date SignUpDateCheck = (Date) dftreg.parse("2018-07-05");
System.out.println(SignUpDateCheck + " is the SignUpDateCheck ");
System.out.println(currentDate + " is the currentDate ");
int retval=dateTimeComparator.compare(currentDate,SignUpDateCheck);
System.out.println(retval);
SignUpDateCheck = (Date) dftreg.parse("2018-07-13");
System.out.println(SignUpDateCheck + " is the SignUpDateCheck ");
System.out.println(currentDate + " is the currentDate ");
retval=dateTimeComparator.compare(currentDate,SignUpDateCheck);
System.out.println(retval);
}
}
它打印
Thu Jul 05 00:00:00 2018 is the SignUpDateCheck
Thu Jul 12 12:03:39 2018 is the currentDate
1
Fri Jul 13 00:00:00 2018 is the SignUpDateCheck
Thu Jul 12 12:03:39 2018 is the currentDate
-1
但是,如果我在我的 webapp 中使用相同的逻辑,它似乎总是将 retval 打印为 -1,无论它是在日期之后、之前还是等于日期。
我在 webapp 中的代码片段
String GenerateSignupDate=year+"-"+month+"-"+day;
SignUpDateCheck= (Date) dftreg.parse(GenerateSignupDate);
System.out.println(SignUpDateCheck + " is the SignUpDateCheck ");
System.out.println(currentDateReg + " is the currentDateReg ");
//System.out.println(SignUpDateCheck.before(currentDateReg) + " is the answer ");
retval=dateTimeComparator.compare(currentDate,SignUpDateCheck);
System.out.println(retval + " is the retval ");
但是注册,当前日期打印正确。它只会错误地打印 retval。
任何帮助表示赞赏。
解决方案
推荐阅读
- javascript - 如何使按钮成为 React with Material-UI 中隐藏输入的标签?
- node.js - 找不到信封的 pem:{"alg":"RS256","kid":"5a66482db3800c83c63","typ":"JWT"}
- azure-devops - Azure DevOps 中来自不同项目的团队
- datatables - jquery datatables:从blob url渲染图像
- javascript - 控制 Twilio Video 渲染的本地视频大小,同时保持服务器端录制的高清质量
- android - 如何避免 google-services.json 的“找不到与包名称匹配的客户端”?
- echarts - Apache ECharts:旋转标签被裁剪
- java - 如何在一个小正方形内填充一个圆圈?
- python - 如何从用 np.poly1d 定义的多项式中获取值?
- ionic-framework - TypeError:无法读取 IONIC 5 中 null 的属性“canDeactivate”