What's the best way to represent a time period in Java? May I use 2 Date objects or is there a better way?
I.e. all April, the hole 3rd week of July, from January to March, etc.
The Period class uses the units year, month and day to represent a period of time. We can obtain a Period object as the difference between two dates by using the between() method: LocalDate startDate = LocalDate. of(2015, 2, 20); LocalDate endDate = LocalDate.
TIME Type. The time data type. The format is yyyy- MM -dd hh:mm:ss, with both the date and time parts maintained. Mapped to java.
Format dateFormat = new SimpleDateFormat("EEE, dd/MM/yyyy"); Above, the “EEE” is set to display the name of the day i.e. Monday, Tuesday, Wednesday, etc.
I see the answers are quite old.
As of Java 8, we can use the native Java.Time which is partially based on Joda Time and makes the manipulations on dates very easy.
Take a look at Period, Duration and LocalDate on
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With