0

日付と時刻に関連する次の条件を確認するのに助けが必要です...

Calendar cal = Calendar.getInstance(); 
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

String CurrentDate= dateFormat.format(cal.getTime());

String ModifiedDate = dateTime は、日付 n 時間ピッカー ウィジェットから取得されます。

私はチェックする必要があります:

現在の ModifiedDate が現在時刻の 5 分以上である

Android / Javaでこの状態を確認する方法.........?

4

3 に答える 3

1

なぜ日付をフォーマットするのですか?

文字列表現よりも「自然な」表現でデータを操作する方がはるかに簡単です。変更された日付を文字列として取得する必要があるかどうかは明確ではありませんが、そうする場合は、最初にそれを解析する必要があります。次に、次を使用して現在の日付と時刻と比較できます。

// Check if the value is later than "now"
if (date.getTime() > System.currentTimeMillis())

また

// Check if the value is later than "now + 5 minutes"
if (date.getTime() > System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5))

「現在の ModifiedDate が現在の時刻の 5 分以上である」という意味は明確ではありません。要件を処理するために上記のコードを変更できます。

日付/時刻の操作を頻繁に行う場合は、 /よりもはるかに優れた日付/時刻 API であるJoda Timeの使用を強くお勧めします。java.util.DateCalendar

于 2013-01-16T06:51:05.893 に答える
0

指定された時間が現在の時間より前か後かを確認するには、Android に Calendar インスタンスがあり、日時の値を比較します。

Calendar current_time = Calendar.getInstance ();

current_time.add(Calendar.DAY_OF_YEAR, 0);

current_time.set(Calendar.HOUR_OF_DAY, hrs);

current_time.set(Calendar.MINUTE, mins );

current_time.set(Calendar.SECOND, 0);


Calendar given_time = Calendar.getInstance ();

given_time.add(Calendar.DAY_OF_YEAR, 0);

given_time.set(Calendar.HOUR_OF_DAY, hrs);

given_time.set(Calendar.MINUTE, mins );

given_time.set(Calendar.SECOND, 0);


current_time.getTime();

given_time.getTime();


boolean v = current_calendar.after(given_calendar);


// it will return true if current time is after given time


if(v){

return true;

}
于 2013-01-16T06:48:35.117 に答える
0
public static boolean getTimeDiff(Date dateOne, Date dateTwo) {
    long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
    int day =   (int) TimeUnit.MILLISECONDS.toHours(timeDiff);
    int min=    (int) ( TimeUnit.MILLISECONDS.toMinutes(timeDiff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeDiff)));
    if(day>1)
    {
        return false;
    }
    else if(min>5)
    {
        return false;
    }
    else
    {
        return true;
    }
}

利用方法:

System.out.println(getTimeDiff(new Date("01/13/2012 12:05:00"),new Date("01/12/2012 13:00:00")));
于 2013-01-16T06:53:22.313 に答える