0

I tried to find a similar answer before posting this question, but I couldn't find one. I'm supposed to calculate and display the number of total days, and then the number of months, and days from now (when the program is run) to when the person’s next birthday is (no leap year). Here is my code:

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);

   }
}

We have not been taught to use anything like Jodatime yet, so that's not an option. What ways are there to calculate this?

UPDATE

I have figured out a way to calculate the days remaining, but I have run into a rather interesting bug. Here is the updated code after the prompt to enter the birthday, as well as the updated list of variables:

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() ;

When I call daysFromMillis in the print statement, if I enter in a birthday of 02/17/1993, I will get an answer of "your birthday is 5435 days from now." What am I doing wrong?

Kjc21793
  • 79
  • 1
  • 2
  • 13
  • It's not a duplicate, it's an original question. – Kjc21793 Oct 01 '14 at 21:53
  • You can use the new date API for this. You can use the LocalDate it has a plus method – BilalDja Oct 01 '14 at 21:57
  • 1
    @Kjc21793 I didn't mean that you copied the question linked, but that the question is extremely similar. As a service, StackOverflow is more helpful when there is less redundancy. Your question is fair, but I think you can get all of the information you need from the answers to the one linked. – deyur Oct 01 '14 at 22:01
  • The only problem with the one linked is that it doesn't deal with months or days, both of which I need. – Kjc21793 Oct 01 '14 at 22:05
  • @BasilBourque I did search, there wasn't anything similar. Please read my question before commenting. – Kjc21793 Oct 02 '14 at 16:53

1 Answers1

0

Most people will agree that the Date class is pretty much a disaster... Maybe using that other existing standard class, Calendar could help you? Not that it's much less of a catastrophe, but as you haven't learned about Joda-Time or the new java.time package in Java 8 (defined by JSR 310, inspired by Joda-Time) there aren't many sensible options left in standard, pre-8 Java.

Have a look at Soumyarup Dasgupta’s answer to another question for a not overly fast but easy way to find the number of days between two Calendar instances just using its add method and a simple loop. The same logic will also work for months. Calendar has a set method to manipulate individual fields - and the corresponding get method as well, using these features and getting some inspiration from the linked code should get you started.

Community
  • 1
  • 1
fvu
  • 32,488
  • 6
  • 61
  • 79