0

LocalDateTimeaと a DateTime( Joda 1.3を使用) を取り、それらが互いに 30 分以内にあるかどうかを判断するメソッドを作成しようとしています。これが私が思いつく最善の方法ですが、より良い/よりクリーンで効率的な方法が必要であることはわかっています:

public boolean isWithin30MinsOfEachOther(LocalDateTime localDateTime, DateTime dateTime) {
    return (
        localDateTime.getYear() == dateTime.getYear() &&
        localDateTime.getMonthOfYear() == dateTime.getMonthOfYear() &&
        localDateTime.getDayOfMonth() == dateTime.getDayOfMonth() &&
        localDateTime.getHourOfDay() == dateTime.getHourOfDay() &&
        Math.abs((localDateTime.getMinuteOfHour() - dateTime.getMinuteOfHour())) <= 30
    );
)

localDateTime言うまでもなく、たとえば、2012 年 12 月 31 日 23:58:00 でありdateTime、2013 年 1 月 1 日 00:01:00 である場合、これはうまくいかないと思います。2 つの異なる月と日の開始/終了についても同じことが言えます。助言がありますか?前もって感謝します。

4

2 に答える 2

1

Durationクラスを使用してみましたか?

例として:

  Duration myDuration=new Duration(localDateTime.toDateTime(), dateTime);
  return Math.abs(myDuration.getMillis())<=30*60*1000;
于 2012-12-17T17:31:12.510 に答える
0

あなたは試すことができます

return Math.abs(localDateTime.getLocalMillis() 
                - dateTime.toLocalDateTime().getLocalMillis()) < 30 * 60 * 1000;
于 2012-12-17T17:20:47.770 に答える