我在发布这个问题之前试图找到一个类似的答案,但我找不到一个。我应该计算并显示总天数,然后是月数和天数 从现在开始(当程序运行时)到人的下一个生日(没有闰年)。这是我的代码:
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时,如果我在02/17/1993的生日那天进入,我将得到#34;你的生日是5435天的回答。"我做错了什么?
答案 0 :(得分:0)
大多数人会同意Date类几乎是一场灾难......也许使用其他现有的标准类Calendar可以帮到你?并不是说它不是灾难,而是因为你没有在Joda-Time中获知java.time package或新的Java 8(由JSR 310定义,受到Joda-Time的启发)标准的8-Java之前没有太多明智的选择。
请查看另一个问题的Soumyarup Dasgupta’s answer,了解一个不太快但很简单的方法,即只使用其add method和一个简单循环查找两个Calendar实例之间的天数。同样的逻辑也将持续数月。日历有一个set method to manipulate individual fields - 以及相应的get method,使用这些功能并从链接代码中获取灵感应该可以帮助您入门。