在Java中如何计算特定两个日期之间的星期日
我正努力数一数这个月的第一个星期天的数量。 像输入1(测试用例)1900 1 1902 1 1(年、月、日)一样,输出应该是4。包括开始日期 说明:在Java中如何计算特定两个日期之间的星期日,java,date,date-format,java-time,Java,Date,Date Format,Java Time,我正努力数一数这个月的第一个星期天的数量。 像输入1(测试用例)1900 1 1902 1 1(年、月、日)一样,输出应该是4。包括开始日期 说明: 1 April 1900 1 July 1900 1 September 1901 1 December 1901 然而,当我尝试这一点时: 6 4699 12 12 4710 1 1 1988 3 25 1989 7 13 1924 6 6 1925 6 16 1000000000000 2 2 1000000001000 3 2 1925 6
1 April 1900
1 July 1900
1 September 1901
1 December 1901
然而,当我尝试这一点时:
6 4699 12 12 4710 1 1 1988 3 25 1989 7 13 1924 6 6 1925 6 16 1000000000000 2 2 1000000001000 3 2 1925 6 16 1924 6 6 1905 1 1 1905 1 1
输出应为:18 2 1720 0 1
我的代码输出是:182**1714**01
在测试输入中,6表示6个测试用例。4699年12月12日,4699年12月12日;4710结束日期等等
你能帮我解决这个问题吗
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Calendar;
public class nineteen {
public static void main(String[] args) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Scanner sc = new Scanner(System.in);
int loop = sc.nextInt();
for (int i = 0; i < loop; i++) {
long year = sc.nextLong(), month = sc.nextLong(), day = sc.nextLong(), yearto = sc.nextLong(),
monthto = sc.nextLong(), dayto = sc.nextLong();
String day1 = String.valueOf(day);
String month1 = String.valueOf(month);
String year1 = String.valueOf(year);
String dayt = String.valueOf(dayto);
String montht = String.valueOf(monthto);
String yeart = String.valueOf(yearto);
String input_date = month1 + "/" + day1 + "/" + year1; // month day year
String out_date = montht + "/" + dayt + "/" + yeart; // month day year
long count = 0;
Date d1 = formatter.parse(input_date);
Date d2 = formatter.parse(out_date);
count = saturdayscount(d1, d2);
// TODO Auto-generated method stub
}
sc.close();
}
public static long saturdayscount(Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
long sundays = 0;
while (!c1.after(c2)) {
if (c1.get(Calendar.DAY_OF_MONTH) != 1) {
c1.add(Calendar.MONTH, 1);
c1.set(Calendar.DAY_OF_MONTH, 1);
}
if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
sundays++;
}
c1.add(Calendar.MONTH, 1);
c1.set(Calendar.DAY_OF_MONTH, 1);
}
System.out.println(sundays);
return sundays;
}
}
import java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.*;
导入java.util.Calendar;
公共十九班{
公共静态void main(字符串[]args)引发异常{
SimpleDataFormat格式化程序=新的SimpleDataFormat(“MM/dd/yyyy”);
扫描仪sc=新的扫描仪(System.in);
int loop=sc.nextInt();
for(int i=0;i
我建议您使用现代日期时间API来完成此操作*
输出:
Processing start date: 1900 1 1 and end date: 1902 1 1
1 April 1900
1 July 1900
1 September 1901
1 December 1901
Count: 4
Processing start date: 1000000000000 2 2 and end date: 1000000001000 3 2
The start date 1000000000000 2 2 can not be processed
Count: 0
从了解有关现代日期时间API的更多信息
注意:我曾经保持演示的整洁,并将重点放在主要问题上,即如何计算两个给定日期之间的星期日。如果需要使用年、月和月日作为输入来创建LocalDate
的实例,您将使用。而且,正如您从文档中了解到的,在这种情况下,您需要将DateTimeParseException
替换为DateTimeException
。解决方案的其余部分将保持不变
*
java.util
date-time API及其格式化APISimpleDataFormat
已经过时,并且容易出错。建议完全停止使用,并切换到。无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。
我建议您使用现代日期时间API来完成此操作* 输出:
Processing start date: 1900 1 1 and end date: 1902 1 1
1 April 1900
1 July 1900
1 September 1901
1 December 1901
Count: 4
Processing start date: 1000000000000 2 2 and end date: 1000000001000 3 2
The start date 1000000000000 2 2 can not be processed
Count: 0
从了解有关现代日期时间API的更多信息
注意:我曾经保持演示的整洁,并将重点放在主要问题上,即如何计算两个给定日期之间的星期日。如果需要使用年、月和月日作为输入来创建LocalDate
的实例,您将使用。而且,正如您从文档中了解到的,在这种情况下,您需要将DateTimeParseException
替换为DateTimeException
。解决方案的其余部分将保持不变
*
java.util
date-time API及其格式化APISimpleDataFormat
已经过时,并且容易出错。建议完全停止使用,并切换到。无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。
您的程序失败,因为输入
10000000000
超过了Java API中的最大有效日期。在使用时:
- 不推荐使用的
API是年java.util.Date
292278994
- 新的
API是年java.time
99999999
因此,您需要使用简单的
if
-else
语句自己验证输入。或者让新的api来处理它,如所示,您的程序失败是因为输入10000000000
超过了Java api中的最大有效日期。在使用时:
- 不推荐使用的
API是年java.util.Date
292278994
- 新的
API是年java.time
99999999
因此,您需要使用简单的
if
-else
语句自己验证输入。或者让新的api来处理它,如所示,我建议使用新的java.time api解决方案。你可以计算你看到的那一天是否是一个月的第一天,如果是星期天,然后递增
class nineteen {
// 6 4699 12 12 4710 1 1 1988 3 25 1989 7 13 1924 6 6 1925 6 16 2020 2 2 3020 3 2 1925 6 16 1924 6 6 1905 1 1 1905 1 1
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
int loop = sc.nextInt();
for (int i = 0; i < loop; i++) {
long year = sc.nextLong();
int month = sc.nextInt();
int day = sc.nextInt();
long yearto = sc.nextLong();
int monthto = sc.nextInt();
int dayto = sc.nextInt();
LocalDate startDate = LocalDate.of(0, month, day).plusYears(year);
LocalDate endDate = LocalDate.of(0, monthto, dayto).plusYears(yearto);
LocalDate currentDay = startDate;
int sundaysOnFirstDayOfMonth = 0;
while (currentDay.isBefore(endDate) || currentDay.isEqual(endDate)) {
if (currentDay.getDayOfMonth() == 1 && currentDay.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
sundaysOnFirstDayOfMonth++;
}
currentDay = currentDay.plusDays(1L);
}
System.out.println(sundaysOnFirstDayOfMonth);
}
}
}