In: Computer Science
Java Program using Netbeans IDE
Create class Date with the following capabilities:
a. Output the date in multiple formats, such as
MM/DD/YYYY
June 14, 1992
DDD YYYY
b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Objects;
public class Date {
public Date(int month, int day, int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, day);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.YEAR, year);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(format.format(calendar.getTime()));
}
public Date(String monthName, int day, int year) {
if (Objects.nonNull(monthName) && !monthName.equals("")) {
try {
java.util.Date date = new SimpleDateFormat("MMMM").parse(monthName);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, day);
calendar.set(Calendar.YEAR, year);
SimpleDateFormat format = new SimpleDateFormat("MMMM dd, yyyy");
System.out.println(format.format(calendar.getTime()));
} catch (ParseException e) {
System.out.println("Invalid month name.!");
}
} else {
System.out.println("Please provide a month name.!");
}
}
public Date(int day, int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, day);
calendar.set(Calendar.YEAR, year);
SimpleDateFormat format = new SimpleDateFormat("ddd yyyy");
System.out.println(format.format(calendar.getTime()));
}
public static void main(String[] args) {
new Date(6, 20, 2020);
new Date("may", 14, 1992);
new Date(20, 1992);
}
}
I have created 3 constructors for the Date class.
1st constructor has 3 parameters - month, day, and year respectively. We created a calendar class instance and set date, month, and year with the parameters.
calendar.set(Calendar.MONTH, month - 1);
We subtract the month by 1 because the month value starts from 0 in
the Calendar class in Java, Then we created the
SimpleDateFormat class instance with the date format given in the
question. Finally, we print the date in the corresponding format
using the format method of SimpleDateFormat class.
calendar.getTime() returns the date object with the date values we
set.
In the 2nd constructor, we pass the month name, day, and year respectively. First, we check if the month's name is null or empty. if yes, we print ''Please provide a month name.!". else we create a date object with the month name. and create the Calendar instance and set the date using the "setTime" method. Then we set the day and year values just like we did in the first constructor. Finally, we create the SimpleDateFormat with the second date format in the question. And we format the date object and print the date.
In the 3rd constructor, we create the Calendar instance and set the date and year values only. Then we create the SimpleDateFormat object with the 3rd date format given in the question. And finally, print the formatted date just like we did in the first and second constructor.
In the main method, we create the instances of our Date class using the 3 overloaded constructors and pass the respective values.
That's all we did in this program.