-1

I have this code that is meant to find the day of the week of a specific date. (Example: 09/21/2016 = Wednesday).

Currently the user enters the month, day, and year one by one. However, I want to change it so that the user may enter it in the format MM/DD/YYYY.

I'm having trouble splitting the month, day, and year from the format MM/DD/YYYY.

Scanner keyboard = new Scanner(System.in);
double MM;
double DD;
double YYYY;
double pOne;
double pTwo;
double pThree;
int pFour;

System.out.print("Enter the month (MM): ");
MM = keyboard.nextInt();

System.out.print("Enter the day (DD): ");
DD = keyboard.nextInt();

System.out.print("Enter the year (YYYY): ");
YYYY = keyboard.nextInt();  

pOne = (YYYY - ((14 - MM) / 12));
pTwo = (pOne + ((pOne / 4) - (pOne / 100) + (pOne / 400)));
pThree = ((MM + 12) * ((14 - MM) / 12) - 2);
pFour = (int) ((DD + pTwo + ((31 * pThree) / 12)) % 7); 
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Derrin
  • 29
  • 5

3 Answers3

1

Prompt the user to enter date in the format you want, i.e. MM/DD/YYYY. Then use split() like so:

String date = keyboard.nextLine();
String[] parts = date.split("/");
double MM = Double.valueOf(parts[0]);
double DD = Double.valueOf(parts[1]);
double YYYY = Double.valueOf(parts[2]);

Then use the double variables in your calculation.

Read this SO post to learn about the differences between Double.parseDouble() and Double.valueOf().

Community
  • 1
  • 1
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
0

you can split the string and then parse each result into a double,

String myDate = "09/21/2016";
double MM = Double.parseDouble(myDate.split("/")[0]));
double DD = Double.parseDouble(myDate.split("/")[1]));
double YYYY = Double.parseDouble(myDate.split("/")[2]));
Afsun Khammadli
  • 2,048
  • 4
  • 18
  • 28
Daren Delima
  • 131
  • 8
0

I'm a regex guy. It's also helpful here because you can validate that you got what you expected before actually doing anything with it.

String myDate = "01/02/03";
Matcher matcher = Pattern.compile("(\\d+)/(\\d+)/(\\d+)").matcher(myDate);
if (matcher.find()) {
  double month = Double.parseDouble(matcher.group(1));
  double day = Double.parseDouble(matcher.group(2));
  double year = Double.parseDouble(matcher.group(3));

  System.err.println("M: " + month + " D: " + day + " Y: " + year);
} else {
  // error handling
}

HOWEVER!!!

You can use something like Joda Time or Java's internal date/time support and actually parse it and using formatting. This would be much more robust and also allow you to validate that the date is actually real and not just a set of numbers.

Depends on your needs.

try {
  DateTime parsed = DateTimeFormat.forPattern("MM/dd/yy").parseDateTime(myDate);
  double month = parsed.getMonthOfYear();
  double day = parsed.getDayOfMonth();
  double year = parsed.getYear();
  
  System.err.println("M: " + month + " D: " + day + " Y: " + year);
} catch (DateTimeParseException e) {
  // error handling
}

The benefit here is also that you get two different outputs:

M: 1.0 D: 2.0 Y: 3.0

M: 1.0 D: 2.0 Y: 2003.0

Notice that the 2nd one is not assuming just a basic number and tries to give you a real year to work with. If this is a production application, I would definitely recommend using the actual date/time parsing methodologies instead of just numeric splits.

Community
  • 1
  • 1
el n00b
  • 1,957
  • 7
  • 37
  • 64