1. public static boolean isLeapYear (int year) Returns whether or not the given
ID: 3731744 • Letter: 1
Question
1. public static boolean isLeapYear (int year) Returns whether or not the given year is a leap year. 2. public static int dayofYear (int month, int day, int year) Return the day of the year. Return -1 if the date is not valid 3. public static int daysRemaining(int month, int day, int year) Return the number of days remaining in the year. Return -1 ithe date is not valid. 4. public static boolean isDateValid(int month, int day, int year) Return whether or not the date given is valid.(Valid years are 1800 to 3000, inclusive.) 5. public static short daysInNonth(int month, int year) Given a month number, return the number of days in that month for that year. Return 0 if the month is not valid. 6. public static void dateReport (int month, int day, int year) Print out a date report for the given date as shown in the sample runs. 7. public static short monthNameToNumber (String month) Given a String of the full month "January" or the first three letters "Jan" . return the month's position in the year. For example, " juL" would return 7 ·Invalid months should return -1 . (case insensitive) 8. public static String monthNumberToName (int month) Given a month of the year, return the full name of the month as a string or INVALID otherwise. 9. public static int daysBetween(int mi, int d, int yl. int m2, int d2, int y2) Return the number of days between two dates in the same year Ifeither of the dates are invalid or the years are not the same, return-1 10. public static long millisecLeftInYear (int month, int day, int year) Return the number of milliseconds left in a year. You may assume 24-hour days and work in whole-day increments. 11. public static void printMenu () Print out the choice menu as shown in the sample runs. 12. public static int getPosNum(int max) Prompt the user to "Enter a positive number up to [max]· until a valid number is read in. See the sample runs for an example. If max is not a positive number, returnExplanation / Answer
import java.util.*;
import java.text.*;
import java.util.concurrent.TimeUnit;
public class Calander1 {
public static boolean isLeepYear(int year){
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
public static boolean isValidDate(int d, int m, int y)
{
int MAX_VALID_YR = 3000;
int MIN_VALID_YR = 1800;
if (y > MAX_VALID_YR || y < MIN_VALID_YR)
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
// Handle February month with leap year
if (m == 2) {
if (isLeepYear(y))
return (d <= 29);
else
return (d <= 28);
}
if (m == 4 || m == 6 || m == 9 || m == 11)
return (d <= 30);
return true;
}
public static int dayOfYear(int month,int day,int year){
if(!isValidDate(day,month,year)){
return -1;
}
Calendar ca1 = Calendar.getInstance();
ca1.set(year,month,day);
int DAY_OF_YEAR=ca1.get(Calendar.DAY_OF_YEAR);
return DAY_OF_YEAR;
}
public static long dayRemaining(int month,int day,int year){
if(!isValidDate(day,month,year)){
return -1;
}
final Calendar c = Calendar.getInstance();
c.set(year, month, day);
final Calendar today = Calendar.getInstance();
final long millis = c.getTimeInMillis()- today.getTimeInMillis();
final long days = millis / 86400000;
return days;
}
public static short daysInMonth(int month, int year){
if(month>12 && month<1)
return 0;
return (short) new Date(year, month, 0).getDate();
}
public static void dateReport(int month,int day,int year){
}
public static short monthnameToNumber(String month) throws ParseException{
Date date = new SimpleDateFormat("MMMM").parse(month);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return (short) (cal.get(Calendar.MONTH));
}
public static String monthNumberToName(int month){
String monthString;
switch (month) {
case 1: monthString = "January"; break;
case 2: monthString = "February"; break;
case 3: monthString = "March"; break;
case 4: monthString = "April"; break;
case 5: monthString = "May"; break;
case 6: monthString = "June"; break;
case 7: monthString = "July"; break;
case 8: monthString = "August"; break;
case 9: monthString = "September"; break;
case 10: monthString = "October"; break;
case 11: monthString = "November"; break;
case 12: monthString = "December"; break;
default: monthString = "Invalid month"; break;
}
return monthString;
}
public static int daysBetween(int m1,int d1,int y1,int m2,int d2,int y2){
SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
String inputString1 = "d1 m1 y1";
String inputString2 = "d2 m2 y2";
int Days = 0;
try {
Date date1 = myFormat.parse(inputString1);
Date date2 = myFormat.parse(inputString2);
long diff = date2.getTime() - date1.getTime();
Days = (int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
} catch (ParseException e) {
e.printStackTrace();
}
return Days;
}
public static void printMenu(){
System.out.println("*** Date / Time Menu ***");
System.out.println("=========================");
System.out.println("1. Data Report");
System.out.println("<<OTHER MENU OPTIONS>>");
System.out.println("9. Quit");
}
public static int getPositiveNo(int Max){
System.out.println("Enter a positive number up to 9: ");
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
return no;
}
public static void main(){
Calander1.printMenu();
int no=Calander1.getPositiveNo(0);
int month,day,year;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the month as a Number :");
month=sc.nextInt();
System.out.println("Enter the day as a Number :");
day=sc.nextInt();
System.out.println("Enter the year as a Number :");
year=sc.nextInt();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.