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
Does Joda-Time have a method called isToday
提问by learner
回答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 DateTime
one 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 UTC
or 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 dateTime
is an object of DateTime()
什么时候dateTime
是一个对象DateTime()
if((dateTime.toLocalDate()).equals(new LocalDate()))
when date
is 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 includesdate
,time
andtimezone
.
LocalDate()
是一个不可变的日期时间类,表示没有时区的日期。所以没有时间部分。
DateTime()
是不可修改的日期时间类的标准实现。它具有 Date 的所有属性,包括date
、time
和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.
因此,如果您需要更好地比较日期和时间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));
}