I have created a class with two fields that need to be dates, start_date
and date_passed
. I have been researching the best way in java to have dates in a YYYY MM DD
format that allows for easy date subtraction, and the ability to "make-up" a date, say in the future for example.
Example of what I'd like it to do...
library.circulate_book("Chemistry", **start date here**) //actual date or random date
library.pass_book("Chemistry", **Date Passed here**) //random date such as 5 days after start date
int days_had = Date_Passed - start_date
So far, I've found plenty of ways to format dates using Calendars and Date classes, but have yet to find one that looks like it would work considering most dates end up as Strings. Any suggestions/small examples are greatly appreciated! Also, any links to examples would be awesome!
To move from one date to another by adding/subtracting a number of days.
LocalDate.now(
ZoneId.of( "Pacific/Auckland" )
)
.minusDays( 5 )
To calculate the number of days, months, and years elapsed between two dates.
Period.between( start , stop )
First you must parse your string inputs into date-time objects. Then you work on preforming your business logic with those objects.
Stop thinking of date-time values as strings, that will drive you nuts. We work with date-time objects in our code; we exchange data with users or other apps using a String representation of that date-time object.
In Java 8 and later, use the java.time framework. See Tutorial.
You want only a date, without time-of-day, so we can use the LocalDate
class.
That funky double-colon syntax is a method reference, a way to say what method should be called by other code.
String input = "2015 01 02";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyy MM dd" );
LocalDate localDate = formatter.parse ( input , LocalDate :: from );
Determining today’s date requires a time zone. For any given moment, the date varies around the globe by zone.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate todayTunis = LocalDate.now( z ) ;
If you want the JVM’s current default time zone, call ZoneId.systemDefault
.
This has been addressed many times before on StackOveflow.com. For example, How to subtract X days from a date using Java calendar?. For details, see other Answers such as this one by me and this one by me for more details. Tip: "elapsed" is a key search word.
Briefly, use a Period
to define a number of standard days, months, and years.
LocalDate weekLater = localDate.plusDays ( 7 );
Period period = Period.between ( localDate , weekLater );
Integer daysElapsed = period.getDays ();
Dump to console.
System.out.println ( "localDate: " + localDate + " to " + weekLater + " in days: " + daysElapsed );
localDate: 2015-01-02 to 2015-01-09 in days: 7
Note the limitation that Period
works only with whole days, that is, date-only values without time-of-day. That is what we need here for this Question, so job done. If you had date-time values (ZonedDateTime
objects), use the ThreeTen-Extra project’s Days
class as noted in linked Question/Answer above.