剩余天数计算 java
Remaining days calculation in java
我已经实现了以下代码来计算剩余天数。我使用 SimpleDateFormat 将字符串转换为日期。如果我输入 10/02/1993(今天的日期)作为日期,输出显示还剩 0 天。但是如果我选择 11/02/1993,它会显示与剩余 0 天相同的输出。但是,如果我更改月份,即 22/07/1993,它会显示剩余 160 天,而不是原始结果的 162 天。我在这里做错了什么?任何帮助是极大的赞赏。
private static void calculateNextBirthday() {
Scanner in = new Scanner(System.in);
System.out.println("Enter your birthday(dd/MM/yyyy)");
user_input = in.nextLine();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar user_selected_day = new GregorianCalendar();
Calendar today = new GregorianCalendar();
Date selected_date = null;
try {
selected_date = formatter.parse(user_input);
} catch (ParseException e) {
e.printStackTrace();
}
user_selected_day.setTime(selected_date);
int user_selected_month = user_selected_day.get(Calendar.MONTH);
int CMonth = today.get(Calendar.MONTH);
int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
int CDate = today.get(Calendar.DAY_OF_MONTH);
user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));
user_selected_day.set(Calendar.DAY_OF_WEEK,today.get(Calendar.DAY_OF_WEEK));
if (user_selected_month < CMonth) {
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
else if (user_selected_month == CMonth){
if(user_selected_date < CDate){
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
}
long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
long days;
days = (millis / (24 * 60 * 60 * 1000));
System.out.println(String.valueOf(days) + " days left!!");
}
UPDATE : I have updated the Code below and it works fine. Hope it helps someone in the future.
` SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar user_selected_day = new GregorianCalendar();
Calendar today = new GregorianCalendar();
Date selected_date = null;
try {
selected_date = formatter.parse(plainDate);
} catch (ParseException e) {
e.printStackTrace();
}
user_selected_day.setTime(selected_date);
int user_selected_month = user_selected_day.get(Calendar.MONTH);
int CMonth = today.get(Calendar.MONTH);
int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
int CDate = today.get(Calendar.DAY_OF_MONTH);
user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));
user_selected_day.set(Calendar.HOUR_OF_DAY,today.get(Calendar.HOUR_OF_DAY));
user_selected_day.set(Calendar.MINUTE,today.get(Calendar.MINUTE));
user_selected_day.set(Calendar.SECOND,today.get(Calendar.SECOND));
user_selected_day.set(Calendar.MILLISECOND,today.get(Calendar.MILLISECOND));
if (user_selected_month < CMonth) {
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
else if (user_selected_month == CMonth){
if(user_selected_date < CDate){
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
}
long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
long days;
if(user_selected_day.getTime()==today.getTime()){
days = 0;
}
else{
days = (millis / (24 * 60 * 60 * 1000));
}`
不要自己计算。拥有 Calendar 对象后,使用 "add" 方法增加所需的值。
问题出在行
user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));
这会将今天日期的年份设置为 user_selected
日,并且由于月份相等,它将进入 elseif 并在不进行任何更改的情况下退出.
还要检查您提到的 if else 条件。
您还可以在 Java 8 中使用 LocalDate
class,add/subtract 约会。
java.time
offers a Period
class to represent a length of time, such as
a number of days, or hours and minutes. LocalDate
and friends offer
plus()
and minus()
methods to add or subtract a Period or other
time-related object. Period offers factory methods such as ofDays()
.
阅读此示例以供参考。它计算从现在起 700
天后的日期:
/**
* DateAdd -- compute the difference between two dates (e.g., today and 700 days
* from now).
*/
public class Main {
public static void main(String[] av) {
/** Today's date */
LocalDate now = LocalDate.now();
Period p = Period.ofDays(700);
LocalDate then = now.plus(p);
System.out.printf("Seven hundred days from %s is %s%n", now, then);
}
}
运行 该程序报告当前日期和时间,以及七百天后的日期和时间。例如:
Seven hundred days from 2013-11-09 is 2015-10-10
我已经实现了以下代码来计算剩余天数。我使用 SimpleDateFormat 将字符串转换为日期。如果我输入 10/02/1993(今天的日期)作为日期,输出显示还剩 0 天。但是如果我选择 11/02/1993,它会显示与剩余 0 天相同的输出。但是,如果我更改月份,即 22/07/1993,它会显示剩余 160 天,而不是原始结果的 162 天。我在这里做错了什么?任何帮助是极大的赞赏。
private static void calculateNextBirthday() {
Scanner in = new Scanner(System.in);
System.out.println("Enter your birthday(dd/MM/yyyy)");
user_input = in.nextLine();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar user_selected_day = new GregorianCalendar();
Calendar today = new GregorianCalendar();
Date selected_date = null;
try {
selected_date = formatter.parse(user_input);
} catch (ParseException e) {
e.printStackTrace();
}
user_selected_day.setTime(selected_date);
int user_selected_month = user_selected_day.get(Calendar.MONTH);
int CMonth = today.get(Calendar.MONTH);
int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
int CDate = today.get(Calendar.DAY_OF_MONTH);
user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));
user_selected_day.set(Calendar.DAY_OF_WEEK,today.get(Calendar.DAY_OF_WEEK));
if (user_selected_month < CMonth) {
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
else if (user_selected_month == CMonth){
if(user_selected_date < CDate){
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
}
long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
long days;
days = (millis / (24 * 60 * 60 * 1000));
System.out.println(String.valueOf(days) + " days left!!");
}
UPDATE : I have updated the Code below and it works fine. Hope it helps someone in the future.
` SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar user_selected_day = new GregorianCalendar();
Calendar today = new GregorianCalendar();
Date selected_date = null;
try {
selected_date = formatter.parse(plainDate);
} catch (ParseException e) {
e.printStackTrace();
}
user_selected_day.setTime(selected_date);
int user_selected_month = user_selected_day.get(Calendar.MONTH);
int CMonth = today.get(Calendar.MONTH);
int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
int CDate = today.get(Calendar.DAY_OF_MONTH);
user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));
user_selected_day.set(Calendar.HOUR_OF_DAY,today.get(Calendar.HOUR_OF_DAY));
user_selected_day.set(Calendar.MINUTE,today.get(Calendar.MINUTE));
user_selected_day.set(Calendar.SECOND,today.get(Calendar.SECOND));
user_selected_day.set(Calendar.MILLISECOND,today.get(Calendar.MILLISECOND));
if (user_selected_month < CMonth) {
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
else if (user_selected_month == CMonth){
if(user_selected_date < CDate){
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
}
long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
long days;
if(user_selected_day.getTime()==today.getTime()){
days = 0;
}
else{
days = (millis / (24 * 60 * 60 * 1000));
}`
不要自己计算。拥有 Calendar 对象后,使用 "add" 方法增加所需的值。
问题出在行
user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));
这会将今天日期的年份设置为 user_selected
日,并且由于月份相等,它将进入 elseif 并在不进行任何更改的情况下退出.
还要检查您提到的 if else 条件。
您还可以在 Java 8 中使用 LocalDate
class,add/subtract 约会。
java.time
offers aPeriod
class to represent a length of time, such as a number of days, or hours and minutes.LocalDate
and friends offerplus()
andminus()
methods to add or subtract a Period or other time-related object. Period offers factory methods such asofDays()
.
阅读此示例以供参考。它计算从现在起 700
天后的日期:
/**
* DateAdd -- compute the difference between two dates (e.g., today and 700 days
* from now).
*/
public class Main {
public static void main(String[] av) {
/** Today's date */
LocalDate now = LocalDate.now();
Period p = Period.ofDays(700);
LocalDate then = now.plus(p);
System.out.printf("Seven hundred days from %s is %s%n", now, then);
}
}
运行 该程序报告当前日期和时间,以及七百天后的日期和时间。例如:
Seven hundred days from 2013-11-09 is 2015-10-10