gpt4 book ai didi

java - 使用日期和类,比较正在读取的日期

转载 作者:行者123 更新时间:2023-12-01 17:20:45 24 4
gpt4 key购买 nike

我有一个任务要读取一个文件,其中包含格式为

的内容
  June 17, 1997
July 23, 1997
September 28, 1980
September 31, 1980
Mar. 2, 1980
Apr. 2, 1980
May 3, 1980
Nov 25, 1989
Dec 25, 1989
Jan 3, 1973

我必须将每个日期存储到 Date 对象中。如果下一个日期晚于上一个日期,则应根据这两个日期创建一个 DateRange 对象。最终输出应该类似于

    Date: June 17, 1997
Date: July 23, 1997
DateRange: Date: June 17, 1997 - Date: July 23, 1997
Date: September 28, 1980
Invalid Date
Date: March 2, 1980
Date: April 2, 1980
DateRange: Date: March 2, 1980 - Date: April 2, 1980
Date: May 3, 1980
DateRange: Date: April 2, 1980 - Date: May 3, 1980
Date: November 25, 1989
DateRange: Date: May 3, 1980 - Date: November 25, 1989
Date: December 25, 1989
DateRange: Date: November 25, 1989 - Date: December 25, 1989
Date: January 3, 1973

首先,我很抱歉写了这么长的文章,读了这么多内容,但我已经研究这个问题有一段时间了,但我不知道如何继续。我陷入困境,因为我不完全确定如何比较两个日期并将它们放入 DateRange 类中。我的主程序的一个片段包括:

Scanner in = null;
try {
in = new Scanner(new File("dates.txt"));
} catch (FileNotFoundException exception) {
System.err.println("failed to open dates.txt");
System.exit(1);
}
while (in.hasNextLine()) {

String line = in.nextLine();
Date date = new Date(line);
testDates(date);
if (testDateRange(date) < 0){
System.out.println("There's a range");
}
}
}
public static int testDateRange(Date dates){
return dates.compareTo(dates);
}

public static void testDates(Date dates){
System.out.printf("%s\n", dates.getDate());
//System.out.printf("Date: %s\n", dates.getDate());
}
}

我的日期类包括:

public class Date implements Comparable<Date> {

private String dates;
private int year;
private int day;
private int month;

public Date(String line) {
dates = line; // initialize date
} // end one-argument Grades constructor

public String getDate() {
dates = dates.replace(".", "");
dates = dates.replace(",", "");
StringTokenizer st = new StringTokenizer(dates);
String initMonth = st.nextToken();
String initDay = st.nextToken();
String initYear = st.nextToken();
Integer day = Integer.parseInt(initDay);
Integer year = Integer.parseInt(initYear);
String month = initMonth.substring(0,3);
int numMonth = 0;
int maxDays = 0;
String Month = null;
switch (month){
case "Jan":
numMonth = 1;
maxDays = 31;
Month = "January";
break;
case "Feb":
numMonth = 2;
maxDays = 29;
Month = "February";
break;
case "Mar":
numMonth = 3;
maxDays = 31;
Month = "March";
break;
case "Apr":
numMonth = 4;
maxDays = 30;
Month = "April";
break;
case "May":
numMonth = 5;
maxDays = 31;
Month = "May";
break;
case "Jun":
numMonth = 6;
maxDays = 30;
Month = "June";
break;
case "Jul":
numMonth = 7;
maxDays = 31;
Month = "July";
break;
case "Aug":
numMonth = 8;
maxDays = 31;
Month = "August";
break;
case "Sep":
numMonth = 9;
maxDays = 30;
Month = "September";
break;
case "Oct":
numMonth = 10;
maxDays = 31;
Month = "October";
break;
case "Nov":
numMonth = 11;
maxDays = 30;
Month = "November";
break;
case "Dec":
numMonth = 12;
maxDays = 31;
Month = "Decemeber";
break;
default:
break;
}
if(maxDays < day){
dates = ("Invalid Date");
}else{
dates = ("Date: " + Month + " " + day + ", " + year);
}

return dates;
}

public int compareTo (Date other) {

int result = year - other.year;
if (result == 0)
{
result = month - other.month;
if (result == 0)
{
result = day - other.day;
}
}
if (result > 0) return 1;
else if (result < 0) return -1;
else return 0;
}
}

我关心的是 Date 类,我需要比较两个日期。在我的compareTo()方法中,我知道它总是会返回0,因为我没有正确实现它。我只是对如何实现此compareTo() 方法以及如何实际比较第一个日期与第二个日期、第二个日期与第三个日期等进行比较感到困惑。我目前没有 DateRange 类,因为我无法测试两个日期以查看它们是否有范围。

最佳答案

您可以尝试使用SimpleDateFormat类(class)。它会简化您的问题。

例如,您可以先将字符串格式的日期转换为日期格式,如下所示。

String str = "June 17, 1997";
SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
Date date = sdf.parse(str);

然后为了比较日期,您可以使用 before(date)after(date)方法。

date.before(date2);
date.after(date2);

关于java - 使用日期和类,比较正在读取的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18971989/

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