i need help with java code to match an output. the output sample that i am tryin
ID: 3665174 • Letter: I
Question
i need help with java code to match an output. the output sample that i am trying to get looks like that
Please enter today’s date (month day): 1 29
Today is 1/29/2016, day #29 of the year.
Please enter person #1’s birthday (month day): 10 17
10/17/2016 falls on day #291 of 366.
Your next birthday is in 262 day(s).
Please enter person #2’s birthday (month day): 11 23
11/23/2016 falls on day #328 of 366.
Your next birthday is in 299 day(s).
Person #1’s birthday is sooner.
Did ye know? 9/19 be International Talk like a Pirate day.
Arr, me mateys, arr!
so far i managed to get this output
Please enter todays's date (month day) : 2 2
today is # 337
Please enter person #1's (month day) : 10 19
Your next birhtday is in 257
Please enter person #2's (month day) : 5 7
Your next birhtday is in 95
Person #2's birthday is sooner.
please help! thanks in advance
here is my code.
import java.util.*;
public class Birthdays {
public static void main(String[] args) {
int days_of_bday1;
int days_of_bday2;
int day;
Scanner console = new Scanner (System.in);
System.out.print("Please enter todays's date (month day) : ");
int present_month = console.nextInt();
int today_date = console.nextInt();
day = (366 -((30 - (today_date - 1))));
System.out.println("today is # " + day);
//---------------------------------------------------------------------------------------------------
System.out.print("Please enter person #1's (month day) : ");
int person1_bday_month = console.nextInt();
int person1_bday_date = console.nextInt();
if (present_month > person1_bday_date)
{
days_of_bday1 = (366 -((30 - (person1_bday_date - 1)) +( today_date-1)));
System.out.println("Your next birhtday is in " + days_of_bday1);
}
else
{
int days_between_months = (30 * ((person1_bday_month - present_month) - 1));
days_of_bday1 = ((30 - today_date) + (days_between_months) + (person1_bday_date));
System.out.println("Your next birhtday is in " + days_of_bday1);
}
//---------------------------------------------------------------------------------------------------
System.out.print("Please enter person #2's (month day) : ");
int person2_bday_month = console.nextInt();
int person2_bday_date = console.nextInt();
if(present_month > person2_bday_month)
{
days_of_bday2 = (366 - ((30 - (person2_bday_date - 1)) + (today_date - 1)));
System.out.println("Your next birhtday is in " + days_of_bday2);
}
else
{
int days_between_months = (30 * ((person2_bday_month - present_month) - 1));
days_of_bday2 = ((30 - today_date) + (days_between_months) + (person2_bday_date));
System.out.println("Your next birhtday is in " + days_of_bday2);
}
//---------------------------------------------------------------------------------------------------
if (days_of_bday1 < days_of_bday2)
{
System.out.println("Person #1's birthday is sooner.");
}
else
System.out.println("Person #2's birthday is sooner.");
}
}
Explanation / Answer
package com.he.capillary;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Birthdays {
private static SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
public static void main(String[] args) {
int days_of_bday1;
int days_of_bday2;
int day;
int year = LocalDate.now().getYear();
Scanner console = new Scanner(System.in);
System.out.print("Please enter todays's date (month day) : ");
int present_month = console.nextInt();
int today_date = console.nextInt();
Calendar firstDate = Calendar.getInstance();
firstDate.set(year, present_month-1, today_date);
System.out.println("today is # " + format.format(firstDate.getTimeInMillis()) + ",day #"
+ firstDate.get(Calendar.DAY_OF_YEAR) + " of the year.");
// ---------------------------------------------------------------------------------------------------
System.out.print("Please enter person #1's (month day) : ");
int person1_bday_month = console.nextInt();
int person1_bday_date = console.nextInt();
Calendar birthDate = Calendar.getInstance();
birthDate.set(year, person1_bday_month-1, person1_bday_date);
System.out.println(format.format(birthDate.getTimeInMillis()) + " falls on day #"
+ birthDate.get(Calendar.DAY_OF_YEAR) + " of " + getTotalDays(year));
int days = 0;
if(birthDate.compareTo(firstDate) > 0){
days = Math.abs(firstDate.get(Calendar.DAY_OF_YEAR) - birthDate.get(Calendar.DAY_OF_YEAR));
System.out.println("Your next birthday is in "+days +" day(s).");
}else {
days = getTotalDays(year) - firstDate.get(Calendar.DAY_OF_YEAR) + birthDate.get(Calendar.DAY_OF_YEAR);
System.out.println("Your next birthday is in "+days+" day(s).");
}
firstDate.set(Calendar.YEAR, year+1);
// ---------------------------------------------------------------------------------------------------
System.out.print("Please enter person #2’s birthday (month day):");
int present_month1 = console.nextInt();
int today_date1 = console.nextInt();
Calendar secondDate = Calendar.getInstance();
secondDate.set(year, present_month1-1, today_date1);
System.out.println("today is # " + format.format(secondDate.getTimeInMillis()) + ",day #"
+ secondDate.get(Calendar.DAY_OF_YEAR) + " of the year.");
// ---------------------------------------------------------------------------------------------------
System.out.print("Please enter person #2's (month day) : ");
int person2_bday_month = console.nextInt();
int person2_bday_date = console.nextInt();
Calendar birthDate2 = Calendar.getInstance();
birthDate2.set(year, person2_bday_month-1, person2_bday_date);
System.out.println(format.format(birthDate2.getTimeInMillis()) + " falls on day #"
+ birthDate2.get(Calendar.DAY_OF_YEAR) + " of " + getTotalDays(year));
int days1 = 0;
if(birthDate2.compareTo(secondDate) > 0){
days1 = Math.abs(secondDate.get(Calendar.DAY_OF_YEAR) - birthDate2.get(Calendar.DAY_OF_YEAR));
System.out.println("Your next birthday is in "+days1 +" day(s).");
}else {
days1 = getTotalDays(year) - secondDate.get(Calendar.DAY_OF_YEAR) + birthDate2.get(Calendar.DAY_OF_YEAR);
System.out.println("Your next birthday is in "+days1 +" day(s).");
}
// ---------------------------------------------------------------------------------------------------
if(days < days1){
System.out.println("Person #1’s birthday is sooner.");
}else if(days > days1){
System.out.println("Person #2’s birthday is sooner.");
}else{
System.out.println("Same Days. :)");
}
}
private static int compareDates(Date date1,Date date2){
int result = date1.compareTo(date2);
if(result == 0){
return 0;
}else{
return result;
}
}
private static int getTotalDays(int year) {
if (year % 100 == 0) {
if (year / 400 == 0) {
return 366;
} else {
return 365;
}
} else {
if (year % 4 == 0) {
return 366;
} else {
return 365;
}
}
}
}
==== Use Java 8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.