I need to check if a given timestamp is today. I am using Joda-Time. Is there a method or a simple way to check this? What Joda-Time class is better suited for this? LocalDate? DateTime?
To use Joda Time, download the jar from SourceForge and add it to your project the same way you would any other jar (or with maven, use the group joda-time and the artifact joda-time ). The primary class used to represent an instant in time is the org.joda.time.DateTime. A DateTime, as it name implies, encodes both the date and the time.
To address this issue, Joda-Time uses immutable classes for handling date and time. The Date class doesn't represent an actual date, but instead, it specifies an instant in time, with millisecond precision. The year in a Date starts from 1900, while most of the date operations usually use Epoch time which starts from January 1st, 1970.
Joda-Time is licensed under the business-friendly Apache 2.0 licence. The list of FAQ s. Why Joda Time? The standard date and time classes prior to Java SE 8 are poor. By tackling this problem head-on, Joda-Time became the de facto standard date and time library for Java prior to Java SE 8.
To compare to Instant objects we can use compareTo () because it implements the Comparable interface, but also we can use the Joda-Time API methods provided in the ReadableInstant interface which Instant also implements: Another helpful feature is that Instant can be converted to a DateTime object or event a Java Date:
The date can be compared by single statement so why you need a special function.
when dateTime
is an object of DateTime()
if((dateTime.toLocalDate()).equals(new LocalDate()))
when date
is an object of java.util.date
if((new DateTime(date).toLocalDate()).equals(new LocalDate()))
What Joda-time class is better suited for this? LocalDate? DateTime?
The understanding that you need to know what is LocalDate and DateTime.
LocalDate()
is an immutable datetime class representing a date without a time zone. So is not having a time part.
DateTime()
is the standard implementation of an unmodifiable datetime class. Its having all the attributes of the Date, which includesdate
,time
andtimezone
.
So if you need to compare both the date and time better go with datetime
, if you just need to check the date you must use localDate
because the datetime will produce a false
if an .equal
operator is used, unless the time including the seconds part are same for both the objects.
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