Skip to content
Advertisement

Manipulating and comparing dates with GregorianCalendar and can’t get the code to work properly

I’m struggling to find the error in my code here. paytotal is coming out 0 when it should have a number.

firstDayOfPaycheck is the date Oct. 23rd 2020.

lastDayOfPaycheck is the date Nov. 6 2020.

My Simple date format sdf is “MMM dd, yyyy”.

string dateInQuestion passed into runPayroll is “Oct. 31, 2020” which came originally from the same sdf as above.

I’m new to java and haven’t dealt with manipulating the calendar like this. It feels like the code below should work.

JavaScript

Advertisement

Answer

I’m struggling to find the error in my code here.

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes. Never use Date, Calendar, GregorianCalendar, or their relatives.

firstDayOfPaycheck is the date Oct. 23rd 2020.

Use LocalDate to represent a date without time-of-day and without time zone.

JavaScript

lastDayOfPaycheck is the date Nov. 6 2020.

You’ll find date-time handling much easier if you define your spans-of-time using the Half-Open approach. The beginning is inclusive while the ending is exclusive. So instead of focusing on the last day of the pay period, focus on the first day of the following period.

JavaScript

Tip: You can represent the date range of the pay period as a LocalDateRange object if you add the ThreeTen-Extra library to your Java project.

My Simple date format sdf is “MMM dd, yyyy”.

You should not be mixing business logic with localization code. Custom formatting of date-times should only be done for presentation to the user.

When exchanging date-time values textually as data, use the standard ISO 8601 formats. For a date-only value, the standard format is YYYY-MM-DD. The java.time use ISO 8601 formats by default, so no need to specify any formatting pattern.

string dateInQuestion passed into runPayroll is “Oct. 31, 2020” which came originally from the same sdf as above.

JavaScript

If you must accommodate an input of formatted date string rather than standard ISO 8601 format, use DateTimeFormatter. This has been covered many many times already on Stack Overflow, so search for more info.

And rather than check for valid data later, check your inputs early in your code. “Fail fast” is the saying.

JavaScript

I’m new to java and haven’t dealt with manipulating the calendar like this. It feels like the code below should work.

Your code will be much simpler when using java.time. For one thing, the java.time classes offer convenient isBefore, isAfter, and isEqual methods, so no need for clumsy compareTo calls.

JavaScript

See this code run live on IdeOne.com.

Partial pay earned from firstDayOfPayPeriod 2020-10-23 to dateInQuestion 2020-10-31 is 800

With more experience in programming Java, you may want to do this kind of work using streams. See LocalDate::datesUntil.

By the way, if you want to skip weekends, add something like this:

JavaScript

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement