gpt4 book ai didi

java - 一种找出两个日期之间的差异的方法

转载 作者:行者123 更新时间:2023-11-30 06:50:11 25 4
gpt4 key购买 nike

我的方法有效,因为我只想知道年份的差异,我的问题是我这样做效率很低。我觉得有一种更简单、更美观的方法来编写该方法。

编辑:我必须编写自己的方法。我也不想特意使用高级东西,一些在第一年程序员范围内的东西。

public int differenceInYears(MyDate comparedDate) {
int difference = 0;
if (this.year > comparedDate.year) {
if (this.month > comparedDate.month) {
difference = this.year - comparedDate.year;
}
else if (this.month == comparedDate.month) {
if (this.day >= comparedDate.day) {
difference = this.year - comparedDate.year;
}
else {
difference = this.year - comparedDate.year - 1;
}
}
else {
difference = this.year - comparedDate.year - 1;
}
}
if (comparedDate.year > this.year) {
if (comparedDate.month > this.month) {
difference = comparedDate.year - this.year;
}
else if (comparedDate.month == this.month) {
if (comparedDate.day >= this.day) {
difference = comparedDate.year - this.year;
}
else {
difference = comparedDate.year - this.year - 1;
}
}
else {
difference = comparedDate.year - this.year - 1;
}
}
return difference;
}

我将在下面为上下文添加 MyDate 类:

public class MyDate {

private int day;
private int month;
private int year;

public MyDate(int day, int montd, int year) {
this.day = day;
this.month = montd;
this.year = year;
}

public String toString() {
return this.day + "." + this.month + "." + this.year;
}

public boolean earlier(MyDate compared) {
if (this.year < compared.year) {
return true;
}

if (this.year == compared.year && this.month < compared.month) {
return true;
}

if (this.year == compared.year && this.month == compared.month
&& this.day < compared.day) {
return true;
}

return false;
}

最佳答案

我会假定您的方法工作正常。如何改进?您可以做的第一件事是消除所有重复的 this.year - comparedDate.yearcomparedDate.year - this.year 计算。无论如何你都要做它们,所以让我们把它们放在各自的 if block 的顶部。

public int differenceInYears(MyDate comparedDate) {
int difference;

if (this.year > comparedDate.year) {
difference = this.year - comparedDate.year;

if (this.month > comparedDate.month) {
}
else if (this.month == comparedDate.month) {
if (this.day >= comparedDate.day) {
}
else {
difference -= 1;
}
}
else {
difference -= 1;
}
}
if (comparedDate.year > this.year) {
difference = comparedDate.year - this.year;

if (comparedDate.month > this.month) {
}
else if (comparedDate.month == this.month) {
if (comparedDate.day >= this.day) {
}
else {
difference -= 1;
}
}
else {
difference -= 1;
}
}

return difference;
}

接下来,让我们去掉那些空的分支。

public int differenceInYears(MyDate comparedDate) {
int difference;

if (this.year > comparedDate.year) {
difference = this.year - comparedDate.year;

if (this.month == comparedDate.month) {
if (this.day < comparedDate.day) {
difference -= 1;
}
}
else if (this.month < comparedDate.month) {
difference -= 1;
}
}
if (comparedDate.year > this.year) {
difference = comparedDate.year - this.year;

if (comparedDate.month == this.month) {
if (comparedDate.day < this.day) {
difference -= 1;
}
}
else if (comparedDate.month < this.month) {
difference -= 1;
}
}

return difference;
}

现在让我们看看我们是否不能用 &&|| 将一些条件压缩在一起。

public int differenceInYears(MyDate comparedDate) {
int difference;

if (this.year > comparedDate.year) {
difference = this.year - comparedDate.year;

if (this.month == comparedDate.month && this.day < comparedDate.day ||
this.month < comparedDate.month)
{
difference -= 1;
}
}
if (comparedDate.year > this.year) {
difference = comparedDate.year - this.year;

if (comparedDate.month == this.month && comparedDate.day < this.day ||
comparedDate.month < this.month)
{
difference -= 1;
}
}

return difference;
}

这两个街区看起来非常相似,不是吗?我们可以通过有条件地交换 thiscomparedDate 来组合它们。设 ab 分别是较早和较晚的日期。

public int differenceInYears(MyDate comparedDate) {
MyDate a = (this.year < comparedDate.year) ? this : comparedDate;
MyDate b = (this.year < comparedDate.year) ? comparedDate : this;

int difference = b.year - a.year;

if (a.year < b.year) {
if (a.month == b.month && a.day < b.day ||
a.month < b.month)
{
difference -= 1;
}
}

return difference;
}

还有最后的挤压。

public int differenceInYears(MyDate comparedDate) {
MyDate a = (this.year < comparedDate.year) ? this : comparedDate;
MyDate b = (this.year < comparedDate.year) ? comparedDate : this;

int difference = b.year - a.year;

if (a.year < b.year &&
(a.month == b.month && a.day < b.day ||
a.month < b.month))
{
difference -= 1;
}

return difference;
}

关于java - 一种找出两个日期之间的差异的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621542/

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