2021.1.16 LeetCode 从零单刷个人笔记整理(持续更新)
github:https://github.com/ChopinXBP/LeetCode-Babel
如果能依靠库函数Date和SimpleDateFormat可以很快解决问题。在不能依赖库函数的情况下,直接让两个日期做差需要考虑的因素太多。
需要换个思路,分别求出两个日期距离1970年1月1日的天数,再将两个天数做差即可。
Write a program to count the number of days between two dates.
The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.
请你编写一个程序来计算两个日期之间隔了多少天。
日期以字符串形式给出,格式为 YYYY-MM-DD,如示例所示。
示例 1:
输入:date1 = "2019-06-29", date2 = "2019-06-30"
输出:1
示例 2:
输入:date1 = "2020-01-15", date2 = "2019-12-31"
输出:15
提示:给定的日期是 1971 年到 2100 年之间的有效日期。
package Problems;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* Write a program to count the number of days between two dates.
* The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.
* 请你编写一个程序来计算两个日期之间隔了多少天。
* 日期以字符串形式给出,格式为 YYYY-MM-DD,如示例所示。
*
*/
public class NumberOfDaysBetweenTwoDates {
public int daysBetweenDates(String date1, String date2) {
int days1 = count(date1.split("-"));