【发布时间】:2026-02-03 16:00:01
【问题描述】:
在发布此问题之前,我试图找到类似的答案,但我找不到。我应该计算并显示总天数,然后是月数和天数 从现在(程序运行时)到该人的下一个生日时(无闰年)。这是我的代码:
import java.util.Scanner;
import java.text.*;
import java.lang.System;
import java.util.Date;
public class CSCD210Lab3
{
public static void main (String [] args)
{
Scanner userInput = new Scanner(System.in);
//Declare Variables
String nameFirst;
char firstLetter;
String nameMiddle;
String nameLast;
char lastLetter;
String genders;
String genderName;
String nameReplace;
//Get User Input
System.out.print("Please Enter Your Full Name(Including Middle Name): ") ;
nameFirst = userInput.next();
nameMiddle = userInput.next();
nameLast = userInput.next();
firstLetter = nameFirst.charAt(0);
lastLetter = nameLast.charAt(nameLast.length()-1);
userInput.nextLine() ;
System.out.print("Please Enter Your Birthday in the Following Format: MM/DD/YYYY: ") ;
userInput.nextLine() ;
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
System.out.print("Please Enter Your Gender as M or F: ") ;
genders = userInput.next();
char gender = genders.charAt(0);
genderName = nameFirst + " " + nameMiddle + " " + nameLast;
nameReplace = genderName.replaceAll(genders, "?");
userInput.nextLine();
System.out.println();
System.out.println("The First Letter of Your First Name is: " + firstLetter) ;
System.out.println("The Last Letter of Your Name is: "+ lastLetter);
System.out.println("Your Middle Name is: " + nameMiddle);
System.out.println("Your Name With the Last Name First is: "+ nameLast + ", "+ nameFirst + " " +nameMiddle);
System.out.println("Today's Date is: "+dateFormat.format(date));
System.out.println("Your Name With The Letter of Your Gender Replacing the Same Letters In Your Name is: "+ nameReplace);
}
}
我们还没有被教导使用 Jodatime 之类的东西,所以这不是一个选择。有什么方法可以计算这个?
更新
我找到了一种计算剩余天数的方法,但我遇到了一个相当有趣的错误。以下是提示输入生日后的更新代码,以及更新后的变量列表:
String nameFirst;
char firstLetter;
String nameMiddle;
String nameLast
char lastLetter;
String genders;
String genderName;
String nameReplace;
String birthMonth;
String birthDate;
String birthYear;
long birthMillis;
long systemMillis;
long diffMillis;
int daysFromMillis;
int birthMonthInt;
int birthDayInt;
int birthYearInt;
System.out.print("Please Enter Your Birthday in the Following Format: MM DD YYYY: ") ;
birthMonth = userInput.next();
birthDate = userInput.next();
birthYear = userInput.next();
birthMonthInt = Integer.parseInt(birthMonth);
birthDayInt = Integer.parseInt(birthDate);
birthYearInt = Integer.parseInt(birthYear);
Calendar myCalendar = Calendar.getInstance();
myCalendar.set(birthYearInt, birthDayInt, birthYearInt);
birthMillis = myCalendar.getTimeInMillis();
systemMillis = System.currentTimeMillis();
diffMillis = (systemMillis - birthMillis);
daysFromMillis = (int)(diffMillis / (86400000));
userInput.nextLine() ;
当我在打印语句中调用 daysFromMillis 时,如果我输入 1993 年 2 月 17 日的生日,我将得到“你的生日距现在 5435 天”的答案。我做错了什么?
【问题讨论】:
-
这不是重复的,这是一个原始问题。
-
您可以为此使用新的日期 API。你可以使用 LocalDate 它有一个加号方法
-
@Kjc21793 我并不是说你复制了链接的问题,而是这个问题非常相似。作为一项服务,当冗余较少时,* 会更有帮助。您的问题是公平的,但我认为您可以从链接的答案中获得所需的所有信息。
-
链接的唯一问题是它不能处理几个月或几天,这两者都是我需要的。
-
@BasilBourque 我搜索过,没有类似的东西。请在发表评论前阅读我的问题。