Does Joda-Time have a method called isToday

I need to check if this timestamp is set. I am using Joda-Time . Is there a way or an easy way to test this? Which Joda-Time class is best suited for this? LocalDate ? DateTime ?

+7
java datetime jodatime
source share
7 answers

A date can be compared using a single operator, so you need a special function.

when dateTime is a DateTime() object

 if((dateTime.toLocalDate()).equals(new LocalDate())) 

when date is a java.util.date object

  if((new DateTime(date).toLocalDate()).equals(new LocalDate())) 

Which Joda-time class is best suited for this? LocalDate? DateTime?

Understanding that you need to know what LocalDate and DateTime are.

LocalDate() is an immutable datetime class representing a date without a time zone. Thus, you do not have part of the time.

DateTime() is a standard implementation of non-modifiable time datetime class. Having all the attributes of a Date, which includes date , time and timezone .

So, if you need to compare date and time better with dateTime , if you just need to check the date, you should use localDate , because datetime will generate false if the .equal operator, if the time including the second part is not the same for both objects .

+26
source share

One possibility is to create an interval that covers the whole day in question, and then check to see if different timestamps are contained in this interval.

The construction of the initial interval may look like this:

  Interval today = new Interval(DateTime.now().withTimeAtStartOfDay(), Days.ONE); 

Then timestamps can be checked like this:

  today.contains(DateTime.now()); // True today.contains(DateTime.now().minusDays(1)); // False today.contains(DateTime.now().plusDays(1)); // False today.contains(someOtherTimeStamp.toDateTime()); // And so on... 
+7
source share

Here are some simple ways to check if DateTime is today, tomorrow, or yesterday:

 public boolean isToday(DateTime time) { return LocalDate.now().compareTo(new LocalDate(time)) == 0; } public boolean isTomorrow(DateTime time) { return LocalDate.now().plusDays(1).compareTo(new LocalDate(time)) == 0; } public boolean isYesterday(DateTime time) { return LocalDate.now().minusDays(1).compareTo(new LocalDate(time)) == 0; } 
+7
source share

Recommended way to do this:

 DateTime midnightToday = DateTime.now().withTimeAtStartOfDay(); DateTime myDateTime = <whatever>; if(myDateTime.isAfter(midnightToday)) { } 

I think you need Joda 2.5, but that should do the trick.

+2
source share

AFAIK there is no direct method with which you can check the date Date Date Date or not.

The easiest approach would be to create two DateTime objects with a timestamp, and the other with today's date, and then compare the day with dayOfYear() and the year with year() , but remember if both dates are in UTC or in the Local Time Zone .
Small sample

 DateTime date = new DateTime(TimeStamp); DateTime todayDate = new DateTime(); if(date.dayOfYear().get() == todayDate.dayOfYear().get() && date.year().get() == todayDate.year().get()) { System.out.println("Date is today date"); } 
+1
source share

Joda time actually has a method for this:

 DateUtils#isToday(ReadablePartial); DateUtils#isToday(ReadableInstant); 
+1
source share

The easiest way I've found:

 public boolean isToday(DateTime dateTime) { return dateTime.withTimeAtStartOfDay().getMillis() == new DateTime().withTimeAtStartOfDay().getMillis(); } 
0
source share

All Articles