如何检查 startTime 和 startDate 是否小于 endTime 和 endDate?
How to check if startTime and startDate is less than endTime and endDate?
我正在开发一个应用程序,我想在其中检查 startTime
和 startDate
是否小于 endTime
和 endDate
,如果是则只有信息应保存在 FirebaseDatabase
.
我正在使用此代码检查 startTime
< endTime
和 startDate
< endDate
:
try {
Date date1 = currentTime.parse(btnChooseStartTime.getText().toString());
Date date2 = currentTime.parse(btnChooseEndTime.getText().toString());
differenceBetweenStartEndTime = date2.getTime() - date1.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
isEndDateGreaterThanStartDate = CheckDates(btnChooseEndDate.getText().toString(), btnChooseStartDate.getText().toString());
if (String.valueOf(differenceBetweenStartEndTime).contains("-")) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Start time is greater than end time. Fix it please.", Snackbar.LENGTH_SHORT);
snackbar.setDuration(3500);
snackbar.show();
progressDialogPostingE.dismiss();
} else if (!isEndDateGreaterThanStartDate) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Start date is greater than end date. Fix it please.", Snackbar.LENGTH_SHORT);
snackbar.setDuration(3500);
snackbar.show();
progressDialogPostingE.dismiss();
} else {
// code for storing information in database
}
这里是 CheckDates()
方法:
public static boolean CheckDates(String d2, String d1) {
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MM-yyyy");
boolean b = false;
try {
if(dfDate.parse(d1).before(dfDate.parse(d2)))
{
b = true;//If start date is before end date
}
else if(dfDate.parse(d1).equals(dfDate.parse(d2)))
{
b = true;//If two dates are equal
}
else if (dfDate.parse(d2).before(dfDate.parse(d1)))
{
b = false; //If start date is after the end date
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
这段代码几乎可以正常工作,但是当我设置 startDate = 20-11-2016
和 startTime = 10 AM
然后设置 endDate = 21-11-2016
和 endTime = 8 AM
时,我在 SnackBar
中显示 "Start time is greater than end time. Fix it please."
,因为 8 AM
小于 10 AM
,但这里的上午 10 点是前一天,上午 8 点是第二天。因此,它应该将数据存储在数据库中而不是显示 SnackBar。
出了什么问题,如何解决?
请告诉我。
添加带有小时、分钟和秒的 SimpleDateFormat
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
boolean b = false;
try {
if(dfDate.parse(d1).before(dfDate.parse(d2)))
{
b = true;//If start date is before end date
}
else if(dfDate.parse(d1).equals(dfDate.parse(d2)))
{
b = true;//If two dates are equal
}
else if (dfDate.parse(d2).before(dfDate.parse(d1)))
{
b = false; //If start date is after the end date
}
我正在开发一个应用程序,我想在其中检查 startTime
和 startDate
是否小于 endTime
和 endDate
,如果是则只有信息应保存在 FirebaseDatabase
.
我正在使用此代码检查 startTime
< endTime
和 startDate
< endDate
:
try {
Date date1 = currentTime.parse(btnChooseStartTime.getText().toString());
Date date2 = currentTime.parse(btnChooseEndTime.getText().toString());
differenceBetweenStartEndTime = date2.getTime() - date1.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
isEndDateGreaterThanStartDate = CheckDates(btnChooseEndDate.getText().toString(), btnChooseStartDate.getText().toString());
if (String.valueOf(differenceBetweenStartEndTime).contains("-")) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Start time is greater than end time. Fix it please.", Snackbar.LENGTH_SHORT);
snackbar.setDuration(3500);
snackbar.show();
progressDialogPostingE.dismiss();
} else if (!isEndDateGreaterThanStartDate) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Start date is greater than end date. Fix it please.", Snackbar.LENGTH_SHORT);
snackbar.setDuration(3500);
snackbar.show();
progressDialogPostingE.dismiss();
} else {
// code for storing information in database
}
这里是 CheckDates()
方法:
public static boolean CheckDates(String d2, String d1) {
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MM-yyyy");
boolean b = false;
try {
if(dfDate.parse(d1).before(dfDate.parse(d2)))
{
b = true;//If start date is before end date
}
else if(dfDate.parse(d1).equals(dfDate.parse(d2)))
{
b = true;//If two dates are equal
}
else if (dfDate.parse(d2).before(dfDate.parse(d1)))
{
b = false; //If start date is after the end date
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
这段代码几乎可以正常工作,但是当我设置 startDate = 20-11-2016
和 startTime = 10 AM
然后设置 endDate = 21-11-2016
和 endTime = 8 AM
时,我在 SnackBar
中显示 "Start time is greater than end time. Fix it please."
,因为 8 AM
小于 10 AM
,但这里的上午 10 点是前一天,上午 8 点是第二天。因此,它应该将数据存储在数据库中而不是显示 SnackBar。
出了什么问题,如何解决?
请告诉我。
添加带有小时、分钟和秒的 SimpleDateFormat
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
boolean b = false;
try {
if(dfDate.parse(d1).before(dfDate.parse(d2)))
{
b = true;//If start date is before end date
}
else if(dfDate.parse(d1).equals(dfDate.parse(d2)))
{
b = true;//If two dates are equal
}
else if (dfDate.parse(d2).before(dfDate.parse(d1)))
{
b = false; //If start date is after the end date
}