我需要找到两个日期之间的天数:一个来自报告,一个是当前日期.我的片段:
int age=calculateDifference(agingDate, today);
Run Code Online (Sandbox Code Playgroud)
这calculateDifference是一个私有方法,agingDate并且today是Date对象,仅供您澄清.我已经关注了Java论坛中的两篇文章,即Thread 1/Thread 2.
它在独立程序中运行良好,但是当我将其包含在我的逻辑中以从报告中读取时,我会得到一个不寻常的值差异.
为什么会发生这种情况?我该如何解决?
编辑:
与实际天数相比,我获得的天数更多.
public static int calculateDifference(Date a, Date b)
{
int tempDifference = 0;
int difference = 0;
Calendar earlier = Calendar.getInstance();
Calendar later = Calendar.getInstance();
if (a.compareTo(b) < 0)
{
earlier.setTime(a);
later.setTime(b);
}
else
{
earlier.setTime(b);
later.setTime(a);
}
while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR))
{
tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
difference += tempDifference;
earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
} …Run Code Online (Sandbox Code Playgroud) 我在http://www.rgagnon.com/javadetails/java-0506.html上看过"解决方案" ,但它无法正常工作.例如,昨天(6月8日)应该是159,但它说它是245.
那么,有人在Java中有一个解决方案来获取当前日期的三位数朱利安日(不是朱利安日期 - 我需要今年的这一天)吗?
谢谢!标记
我正在做一个与根据给定的生日日期输入获得一个人的年龄相关的应用程序.因为我从下面的代码获得从该日期到当前日期的总天数.
String strThatDay = "1991/05/10";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date d = null;
try {
try {
d = formatter.parse(strThatDay);
Log.i(TAG, "" +d);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
} catch (ParseException e) {
e.printStackTrace();
}
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(d); //rest is the same....
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - thatDay.getTimeInMillis();
long days = diff / (24 * 60 * 60 * 1000);
Run Code Online (Sandbox Code Playgroud)
从这段代码我得到总天数.所以我的要求是将总天数转换为年,月和日......请帮助....