![]() Most locales have daylight saving, so there is one day a year that is 23 hours long (clocks go forward) and another that is 25 hours long (clocks go back). If you decide to use the old-style Java Date class, be very careful. There is a glaring omission of Julian day in the Date and Calendar classes. The pre-java 8 date/time facilities suck (to coin a technical term). You have the advantage of being on Java 8, which includes an upgraded date/time library modelled on the excellent Joda library that recommends. Remember that you MUST check for the number of days in a year/month when converting. ("Differance is: " + year + " " + month + " " + day) Int day = (durationdifferance % 365) % 30 Int month = (durationdifferance % 365) / 30 convert back (this is again only an approximation) Int durationdifferance = date2 - date1 // note this CAN be negative if date1 is after date2(I'll leave it to you to deside the action needed if that's true) Int date1 = (year1 * 365 + month1 * 30) + day1 // I'll leave the number of days in the year/month checking to you. convert the input to days(note that the conversion is only an approximation, not every year/day has the same amount of days) Year2 = scanner.nextInt() // assuming the input is correct ("Enter the second date: (format -> \"Y M D\")") Year1 = scanner.nextInt() // assuming the input is correct ("Enter the first date: (format -> \"Y M D\")") Int year1, year2, month1, month2, day1, day2 Scanner scanner = new Scanner(System.in) // create a scanner object I'll note that I left all the "checking" out, for you to fill them in. What I would do is read in the dates using Scanner then convert them to days and calculate the difference in days, then convert that difference back to "Y M D" format and print it. Considering that you mentioned that this is an "absolute beginners" task. I'm going to assume you have to write everything your self. ("Your time difference is: " + months + " Months, " + days + " Days, and " + years + " Years.") Period difference = oldestDate.until(newestDate) ("You entered " + newestDate + " for your most recent date.") LocalDate newestDate = LocalDate.parse(newestDateString) String newestDateString = scanner.nextLine() ("Enter the most recent date in YYYY-MM-DD format: ") ![]() ("You entered " + oldestDate + " for your oldest date.") LocalDate oldestDate = LocalDate.parse(oldestDateString) String oldestDateString = scanner.nextLine() Scanner scanner = new Scanner(System.in) ("Enter the oldest date in YYYY-MM-DD format: ") ("two user specified dates in terms of months, days, and years.") ![]() ("About This Program: This program will calcuate the difference in") ("BY: Matt Anderson for Grand Circus Detroit's Java Bootcamp") Again, thank all of you so much for the help! import // Calling in Scanner to get some user input. On a side note, I'm using Eclipse with JDK 8 if that matters.ĮDIT: Here's my end result. Any suggested ways to go about this? I've prowled through many Stack Overflow posts similar to my question, but none that I have came across seem to incorporate the scenario where a user inputs the two dates in question. The user should enter two dates as input, then the program will show the result of the difference in time between these two dates."ĭoes anyone have any examples of this that I can attempt to re-write? Concepts tend to stick better for me and make more sense if I'm able to write it out and see what's happening as I go. "Write a Java console application to calculate the duration between two dates in Years, Months and Days.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |