Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Joda-Time have a method called isToday

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?

like image 733
learner Avatar asked Feb 10 '14 04:02

learner


People also ask

How do I use Joda Time?

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.

How does Joda-Time handle date and 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.

What is the license for the Joda Time Library?

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.

How to compare to an instant object in Joda?

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:


1 Answers

The date can be compared by single statement so why you need a special function.

when dateTimeis 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 includes date, time and timezone.

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.

like image 122
Dileep Avatar answered Sep 24 '22 19:09

Dileep