Question

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

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.

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.sql.Timestamp;

public class RandomDate
{
public static void main(String[] args)
{
try{
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);
}catch(Exception e){
//some pice of infromation

}
}
}

Add a comment
Know the answer?
Add Answer to:
How to add exceptions to this code for each month so that it won't exceed the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • Extend this date validation program, so that it checks for valid leap years (in which the...

    Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...

  • I am trying to understand how to manipulate the Gregorian Calendar so that I can convert...

    I am trying to understand how to manipulate the Gregorian Calendar so that I can convert my answer into a string format. What I'm hoping to do is to do is return string to a different file to add to a string in separate file (Object-oriented exercise between files), but once I can convert this into a string I can convert this file on my own. I added the System.out.println() to show what the results should look like. In other...

  • Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

    Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...

  • Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.

    Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.1. Increment a Date to the next day (for example, 1-31-2011 increments to 2-1-2011; 12-31-2010 increments to 1-1-2011).2. Decrement a Date to the previous day (for example, 2-1-1953 decrements to 1-31-1953; 1-1-1954 decrements to 12-31-1953).. Extra Credit (up to 5 points) Re-write the Date member functions Input() and Output() to usedialog boxes to...

  • C++ edit: You start with the code given and then modify it so that it does...

    C++ edit: You start with the code given and then modify it so that it does what the following asks for. Create an EmployeeException class whose constructor receives a String that consists of an employee’s ID and pay rate. Modify the Employee class so that it has two more fields, idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, throw an EmployeeException if the hourlyWage is less than $6.00 or over $50.00. Write a program that...

  • In Java You are to write a program that determines the day of the week for...

    In Java You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program: /** Interface for Date objects to be used by the Year3000 driver program. @author Jon Sorenson */ public interface DateInterface { /** @return the day of the month (1-31) */ public int getDay(); /** @return the day of...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

    Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...

  • Please help me edit my code so it looks a little different but produces the same...

    Please help me edit my code so it looks a little different but produces the same output. Me and classmate did the assignment correctly but are afraid of plagarism. Here is my code. /** * @(#) * @author * @version 1.00 6/25/2017 3:00 PM * Program Purpose: Code a program that computes sales revenue * to-date to determine the status of the company’s * sales revenue and whether a year end bonus is in store for the employees. */ import...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT