Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to add exceptions to this code for each month so that it won\'t exceed the a

ID: 639537 • Letter: H

Question

How to add exceptions to this code for each month so that it won't exceed the amount of days that in each one for them.

Example there are only 28 days in feb and 31 days in march.

Please include the exception for each month like maximum 28 days in Feb

import java.sql.Timestamp;

public class RandomDate
{
public static void main(String[] args)
{
long begin=Timestamp.valueOf("1900-01-01 00:00:00").getTime();
long end=Timestamp.valueOf("2014-12-31 00:00:00").getTime();
long diff=end-begin+1;
Timestamp rand=new Timestamp(begin+(long)(Math.random()*diff));
  
int month=rand.getMonth()+1,day=rand.getDate(),year=rand.getYear()+1900;
  
String[] months={"Janunary","February","March","April","May","June","July","August","September","October","November","December"};
String suffix="th";
if(day==2)suffix="nd";
else if(day==3)suffix="rd";
else if(day==1)suffix="st";
  
System.out.println("Random Date is: "+month+"/"+day+"/"+year);
System.out.println("Formatted Date is: "+months[month-1]+" "+day+""+suffix+" ,"+year);
}
}

Explanation / Answer

import java.sql.Timestamp;

public class RandomDate
{
public static void main(String[] args)
{
long begin=Timestamp.valueOf("1900-01-01 00:00:00").getTime();
long end=Timestamp.valueOf("2014-12-31 00:00:00").getTime();
long diff=end-begin+1;
Timestamp rand=new Timestamp(begin+(long)(Math.random()*diff));

try

{  
int month=rand.getMonth()+1,day=rand.getDate(),year=rand.getYear()+1900;

}

Catch(Exception e)

{

If(month==

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote