Java Joda-Time 有一个叫做 isToday 的方法吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21669148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:59:21  来源:igfitidea点击:

Does Joda-Time have a method called isToday

javadatetimejodatime

提问by learner

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?

我需要检查给定的时间戳是否是今天。我正在使用Joda-Time。有没有一种方法或简单的方法来检查这个?哪个 Joda-Time 课程更适合于此?本地日期日期时间

回答by dbw

AFAIK there is no direct method available by which you can check the Date is Today Date or not.

AFAIK 没有可用的直接方法可以检查日期是否为今天日期。

The simplest approach will be constructing two DateTimeone with the Timestamp, and another with today Date and then comparing day from dayOfYear()and year from year()but do remember whether both Date are in UTCor in Local Time Zone.
A small sample,

最简单的方法是DateTime用时间戳构造两个,另一个用今天日期构造,然后比较日期 fromdayOfYear()和 year from,year()但要记住两个 Date 是 inUTC还是 in Local Time Zone
一个小样本,

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");
}

回答by Dileep

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

日期可以通过单个语句进行比较,所以为什么需要一个特殊的函数。

when dateTimeis an object of DateTime()

什么时候dateTime是一个对象DateTime()

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

when dateis an object of java.util.date

什么时候date是一个对象java.util.date

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

What Joda-time class is better suited for this? LocalDate? DateTime?

什么 Joda-time 课程更适合这个?本地日期?约会时间?

The understanding that you need to know what is LocalDate and DateTime.

了解您需要知道什么是 LocalDate 和 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, timeand timezone.

LocalDate()是一个不可变的日期时间类,表示没有时区的日期。所以没有时间部分。

DateTime()是不可修改的日期时间类的标准实现。它具有 Date 的所有属性,包括 datetimetimezone

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 localDatebecause the datetime will produce a falseif an .equaloperator is used, unless the time including the seconds part are same for both the objects.

因此,如果您需要更好地比较日期和时间datetime,如果您只需要检查必须使用localDate的日期,因为false如果使用.equal运算符,日期时间将产生一个,除非包括秒部分的时间对于两个对象。

回答by clstrfsck

One possibility is to create an interval covering the whole day in question, and then check if the various timestamps are contained in this interval.

一种可能性是创建一个涵盖所讨论的一整天的间隔,然后检查该间隔中是否包含各种时间戳。

Constructing the initial interval could look like:

构建初始间隔可能如下所示:

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

Then the timestamps could be checked like so:

然后可以像这样检查时间戳:

  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...

回答by colin

The recommended way to do this would be:

推荐的方法是:

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

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

我认为你需要 Joda 2.5 来做到这一点,但这应该可以解决问题。

回答by Nicklas A.

Joda time actually have a method for this:

Joda 时间实际上有一个方法:

DateUtils#isToday(ReadablePartial);
DateUtils#isToday(ReadableInstant);

回答by Gonzalo Aune

Simplest way I've found:

我发现的最简单的方法:

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

回答by JustinMorris

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

这里有一些简单的方法来检查 DateTime 是今天、明天还是昨天:

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;
}

回答by Rik van Velzen

I like @JustinMorris's answer. But I found this even better:

我喜欢@JustinMorris 的回答。但我发现这更好:

public static boolean isToday(DateTime time) {
    return LocalDate.now().equals(new LocalDate(time));
}

public static boolean isTomorrow(DateTime time) {
    return LocalDate.now().plusDays(1).equals(new LocalDate(time));
}

public static boolean isYesterday(DateTime time) {
    return LocalDate.now().minusDays(1).equals(new LocalDate(time));
}