gpt4 book ai didi

java - 计算日期差异时出现奇怪的值

转载 作者:行者123 更新时间:2023-12-02 11:53:52 27 4
gpt4 key购买 nike

我一直在使用以下代码来计算两个日期之间的差异,但出现了异常错误:

如果我过了以下日期:

d1 = 08/12/2017d2 = 31/07/2022

它返回:24055..

结果不是应该以月数形式返回吗?

 public static int CalcDateDiff( java.util.Date  date1,  java.util.Date  date2) {
if(date1 == null || date2 ==null )
{
if(date2 == null)
{
Calendar d1 = Calendar.getInstance();
d1.setTime(date1);
final Calendar d2 = Calendar.getInstance();
int diff = (d2.get(Calendar.YEAR) - d1.get(Calendar.YEAR)) * 12 + d2.get(Calendar.MONTH) - d1.get(Calendar.MONTH);

return diff;
}
else
return -1;
}
else
{

Calendar d1 = Calendar.getInstance();
d1.setTime(date1);
final Calendar d2 = Calendar.getInstance();
d2.setTime(date2);
int diff = (d2.get(Calendar.YEAR) - d1.get(Calendar.YEAR)) * 12 + d2.get(Calendar.MONTH) - d1.get(Calendar.MONTH);

return diff;
}
}

提前致谢!

最佳答案

最佳解决方案:

如果您已经有 java.sql.Date 实例,那么

final Date d1 = new java.sql.Date(2017 - 1900, 12, 8);
final Date d2 = new java.sql.Date(2022 - 1900, 07, 31);

I know that I am using a deprecated constructor in java.sql.Date, that is just for convenience to get what I need in the least amount of lines of code. Never use this constructor in production code!

最简单、最直接的 self 记录方式来获得你想要的东西:

final long monthsBetween ChronoUnit.MONTHS.between(d1.toLocalDate(),d2.toLocalDate()) + 1;

因为使用此方法,您不必摆弄 TimeZone 信息,因为对于以这种方式创建的 LocalDate 实例来说,它都保证是正确的。

出于某种原因,只有 java.sql.Date 只有 .toLocalDate()这对您有好处,因为这是您从数据库中返回的内容。

public LocalDate toLocalDate() Converts this Date object to a LocalDate The conversion creates a LocalDate that represents the same date value as this Date in local time zone

Returns: a LocalDate object representing the same date value Since: 1.8

<小时/>

对代码更正的评论:

正确的公式是:

(y2 - y1) * 12 + (m2 - m1) + 1

此外,您的代码过于复杂并且使用非常旧的类。

Java 8 解决方案和 Java 7 兼容的更正解决方案:

public class Q47717075
{
/*
https://stackoverflow.com/questions/1086396/java-date-month-difference
*/
public static void main(@Nonnull final String[] args)
{
final Date d1 = new java.sql.Date(2017 - 1900, 12, 8);
final Date d2 = new java.sql.Date(2022 - 1900, 07, 31);
System.out.println(CalcDateDiff(d1, d2));
/* Obtains an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z. */
System.out.println(intervalInMonths(LocalDate.of(2017,12,8), LocalDate.of(2022,7,31)));
System.out.println(ChronoUnit.MONTHS.between(d1.toLocalDate(),d2.toLocalDate()) + 1);
}


/*
Alternate Java 8 version
*/
public static int intervalInMonths(@Nonnull final LocalDate i1, @Nonnull final LocalDate i2)
{
final Period p = Period.between(i1, i2);
return (p.getYears() * 12) + p.getMonths() + 1;
}

/**
* Your versin corrected
*/
public static int CalcDateDiff(@Nonnull final Date date1, @Nonnull final Date date2)
{
final Calendar d1 = Calendar.getInstance();
d1.setTime(date1);
final Calendar d2 = Calendar.getInstance();
d2.setTime(date2);

return ((d2.get(YEAR) - d1.get(YEAR)) * 12 + (d2.get(MONTH) - d1.get(MONTH)) + 1);
}
}

正确的输出是:

56
56
56

关于java - 计算日期差异时出现奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47717075/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com