Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从用户输入的日期计算剩余天数?_Java_Date - Fatal编程技术网

Java 从用户输入的日期计算剩余天数?

Java 从用户输入的日期计算剩余天数?,java,date,Java,Date,在发布这个问题之前,我试图找到一个类似的答案,但我找不到。我应该计算并显示总天数,然后是月数和天数 从现在程序运行到下一个生日不是闰年。这是我的密码: import java.util.Scanner; import java.text.*; import java.lang.System; import java.util.Date; public class CSCD210Lab3 { public static void main (String [] args) {

在发布这个问题之前,我试图找到一个类似的答案,但我找不到。我应该计算并显示总天数,然后是月数和天数 从现在程序运行到下一个生日不是闰年。这是我的密码:

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天。我做错了什么?

大多数人都会同意Date类几乎是一场灾难。。。也许使用其他现有的标准类可以帮助您?并不是说这不是一场灾难,而是因为您还没有了解Joda Time的新定义,也没有从Joda Time的启发中了解到,在标准的8之前的Java中,已经没有多少合理的选择了


看看另一个问题,寻找一种不太快但简单的方法,只需使用its和一个简单的循环就可以找到两个日历实例之间的天数。同样的逻辑也将持续数月。日历有一个-和相应的,以及,使用这些功能和获得一些灵感从链接的代码应该让你开始

这不是一个重复的问题,这是一个原始问题。你可以使用新的日期API来解决这个问题。您可以使用LocalDate,它有一个加号method@Kjc21793我不是说你复制了这个问题,而是说这个问题非常相似。作为一种服务,当冗余较少时,StackOverflow更有帮助。你的问题是公平的,但我认为你可以从链接的答案中获得你需要的所有信息。链接的唯一问题是它不能处理我需要的几个月或几天。@BasilBourque我搜索过,没有类似的内容。请在评论之前阅读我的问题。