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

You will create a class called SimpleCalendar that represents a simple calendar

ID: 3637562 • Letter: Y

Question

You will create a class called SimpleCalendar that represents a simple calendar with properties for year, month, day, hour, minute and second. Look at the code snippet:

As you can see above, the class has a way for setting its properties and returning a string representation via toString method. For this pass of the assignment, you need to implement the following:
1. A constructor that takes its year,month,day as parameters and another version that takes all of the properties via parameters; namely, year,month,day,hour, minute and second.
2. A constructor that takes a string in the form “2/12/2011”, or “2-12-2011” or “2-12-2011 5:55:32” and it should parse the string and get its values from the string.
3. The calendar class should allow usage of AM/PM vs 24 Hour formats for its time representation.
4. It should use exceptions for handling error and exception cases. For example, cal.setDay(45) should cause an error/exception.
5. Comment your code and document your fields/methods so people can understand what they do without having to read your code.
6. It should give methods to advance its fields, such as advancing the year or months with things like this:
7. cal.add(2,”year”) and the result should be 11/12/2013 13:25:55. Adding negative values should change the date to an earlier date, for example cal.add(-2,”year”) should rewind the date/time to two years before.
8. It should have a method that counts the number of year, month…etc. between two dates. For example,
9. If c1 has value of 5/12/2011 and c2 has value of 26/12/2011, c1.count(c2,”day”) should return 11 since there are 11 days from c1 to c2.

Explanation / Answer

import java.util.Date; import java.util.Calendar; import java.text.SimpleDateFormat; import java.util.*; /** * ----------------------------------------------------------------------------- * Used to provide an example that exercises most of the functionality of the * java.util.Calendar class. * * @version 1.0 * @author Jeffrey M. Hunter (jhunter@idevelopment.info) * @author http://www.idevelopment.info * ----------------------------------------------------------------------------- */ public class CalendarExample { /** * Helper utility used to print a String to STDOUT. * @param s String that will be printed to STDOUT. */ private static void prt(String s) { System.out.println(s); } private static void prt() { System.out.println(); } /** * Calendar's getTime() method returns a Date object. This can then be * passed to println() to print today's date (and time) in the traditional * (but non-localized) format. */ private static void doCalendarTimeExample() { prt("CURRENT DATE/TIME"); prt("================================================================="); Date now = Calendar.getInstance().getTime(); prt(" Calendar.getInstance().getTime() : " + now); prt(); } /** * Simple Date Format from java.text package. */ private static void doSimpleDateFormat() { prt("SIMPLE DATE FORMAT"); prt("================================================================="); // Get today's date Calendar now = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); prt(" It is now : " + formatter.format(now.getTime())); prt(); } /** * Date Arithmetic function. Adds the specified (signed) amount of time to * the given time field, based on the calendar's rules. * The following examle: * - Subtracts 2 years from the current time of the calendar * - Adds 5 days from the current time of the calendar */ private static void doAdd() { prt("ADD / SUBTRACT CALENDAR / DATEs"); prt("================================================================="); // Get today's date Calendar now = Calendar.getInstance(); Calendar working; SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); working = (Calendar) now.clone(); working.add(Calendar.DAY_OF_YEAR, - (365 * 2)); prt(" Two years ago it was: " + formatter.format(working.getTime())); working = (Calendar) now.clone(); working.add(Calendar.DAY_OF_YEAR, + 5); prt(" In five days it will be: " + formatter.format(working.getTime())); prt(); } private static void doDateDifference() { prt("DIFFERENCE BETWEEN TWO DATES"); prt("================================================================="); Date startDate1 = new GregorianCalendar(1994, 02, 14, 14, 00).getTime(); Date endDate1 = new Date();; long diff = endDate1.getTime() - startDate1.getTime(); prt(" Difference between " + endDate1); prt(" and " + startDate1 + " is " + (diff / (1000L*60L*60L*24L)) + " days."); prt(); } private static void doGetMethods() { prt("CALENDAR GET METHODS"); prt("================================================================="); Calendar c = Calendar.getInstance(); prt(" YEAR : " + c.get(Calendar.YEAR)); prt(" MONTH : " + c.get(Calendar.MONTH)); prt(" DAY_OF_MONTH : " + c.get(Calendar.DAY_OF_MONTH)); prt(" DAY_OF_WEEK : " + c.get(Calendar.DAY_OF_WEEK)); prt(" DAY_OF_YEAR : " + c.get(Calendar.DAY_OF_YEAR)); prt(" WEEK_OF_YEAR : " + c.get(Calendar.WEEK_OF_YEAR)); prt(" WEEK_OF_MONTH : " + c.get(Calendar.WEEK_OF_MONTH)); prt(" DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH)); prt(" HOUR : " + c.get(Calendar.HOUR)); prt(" AM_PM : " + c.get(Calendar.AM_PM)); prt(" HOUR_OF_DAY (24-hour): " + c.get(Calendar.HOUR_OF_DAY)); prt(" MINUTE : " + c.get(Calendar.MINUTE)); prt(" SECOND : " + c.get(Calendar.SECOND)); prt(); } public static void main(String[] args) { prt(); doCalendarTimeExample(); doSimpleDateFormat(); doAdd(); doDateDifference(); doGetMethods(); } }

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