如何计算提供两个日期的老化?
How to calculate the aging provided two dates?
我正在尝试在我必须计算老化过程的应用程序中实现此逻辑。
我将动态获取日期和时间作为字符串。
例如,它会像以下
String due_date = "2016-03-27 00:00:00"
要查找两个日期之间的天数,我有以下代码
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
Date dt1 = format1.parse("due_date");
但问题是我无法这样做,因为我遇到了解析异常。
所以,
- 我怎样才能解析字符串 due_date?
- 如何从 due_date 中减去当前日期以获得两个日期之间的天数?
How can i be able to parse a string due_date?
您应该使用这种格式:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dueDate = format1.parse(due_date);
How can I be able to subtract current date from due_date to get the number of days between two days?
你应该这样写方法:
public static int getDaysBetweenDates(Date fromDate, Date dueDate) {
return (int) ((dueDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24L));
}
并像这样使用它:
String from_date = "2016-03-26 00:00:00";
String due_date = "2016-03-27 00:00:00";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date fromDate = format1.parse(from_date);
Date dueDate = format1.parse(due_date);
System.out.println(getDaysBetweenDates(fromDate, dueDate));//prints "1"
Two ways to get total days between two dates
- 使用日期class
public static int getDaysDifference(Date fromDate,Date toDate)
{
if(fromDate==null||toDate==null)
return 0;
return (int)( (toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
}
- 使用日历
public static int getDaysDifference(Calendar calendar1,Calendar calendar2)
{
if(calendar1==null||calendar2==null)
return 0;
return (int)( (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60 * 60 * 24));
}
要格式化日期,请遵循@Cootri 的回答。 :)
我正在尝试在我必须计算老化过程的应用程序中实现此逻辑。
我将动态获取日期和时间作为字符串。
例如,它会像以下
String due_date = "2016-03-27 00:00:00"
要查找两个日期之间的天数,我有以下代码
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
Date dt1 = format1.parse("due_date");
但问题是我无法这样做,因为我遇到了解析异常。
所以,
- 我怎样才能解析字符串 due_date?
- 如何从 due_date 中减去当前日期以获得两个日期之间的天数?
How can i be able to parse a string due_date?
您应该使用这种格式:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dueDate = format1.parse(due_date);
How can I be able to subtract current date from due_date to get the number of days between two days?
你应该这样写方法:
public static int getDaysBetweenDates(Date fromDate, Date dueDate) {
return (int) ((dueDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24L));
}
并像这样使用它:
String from_date = "2016-03-26 00:00:00";
String due_date = "2016-03-27 00:00:00";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date fromDate = format1.parse(from_date);
Date dueDate = format1.parse(due_date);
System.out.println(getDaysBetweenDates(fromDate, dueDate));//prints "1"
Two ways to get total days between two dates
- 使用日期class
public static int getDaysDifference(Date fromDate,Date toDate)
{
if(fromDate==null||toDate==null)
return 0;
return (int)( (toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
}
- 使用日历
public static int getDaysDifference(Calendar calendar1,Calendar calendar2)
{
if(calendar1==null||calendar2==null)
return 0;
return (int)( (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60 * 60 * 24));
}
要格式化日期,请遵循@Cootri 的回答。 :)