0

私は2つの日付の時間の差を取得しようとしています..配列リストがあり、各オブジェクトにはDate型のデータが含まれています..

私の質問は次のとおりです: 1) Calendar.getInstance().get(Calendar.MINUTE) ... などを使用して、現在の日付と時刻を取得する最良の方法 2) 次のように、日付の変数にデータを手動で入力する必要があります。

Date currentDate = new Date();
currentDate.setMinutes(Calendar.getInstance().get(Calendar.MINUTE));
currentDate.setHours(Calendar.getInstance().get(Calendar.HOUR));
currentDate.setDate(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
currentDate.setMonth(Calendar.getInstance().get(Calendar.MONTH));
currentDate.setYear(Calendar.getInstance().get(Calendar.YEAR));

3) currentDate と私が持っている古い日付の違いを取得する方法とcurrentDate - oldDate、「AM_PM」の問題はどうですか? この関数を手動で行う必要がありますか?

4

3 に答える 3

0

これを試してください。以下の変数nowは現在の日付です。

String givenDate = "03/11/2015";

          SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
          try {
                 Date date = (Date)dateFormat.parseObject(givenDate);
                 Date now = new Date();
                 System.out.println(date);
                 System.out.println(now);
                 int diffInDays = (int)( (now.getTime() - date.getTime()) 
                    / (1000 * 60 * 60 * 24) );

                 System.out.println(diffInDays);

          } catch (ParseException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
          }

任意の形式を選択し、時刻または AM/PM を追加できます。 SimpleDateFormatの詳細を参照してください。文字列の日付がない場合は、date上記の変数を直接使用できます。

乾杯 !!

于 2015-06-24T20:58:00.573 に答える
0

1) Calendar.getInstance().get(Calendar.MINUTE) ... などを使用して、現在の日付を取得する最良の方法

java.util.Date の空のコンストラクターからの JavaDoc:

Date オブジェクトを割り当てて初期化し、割り当てられた時刻をミリ秒単位で表すようにします。

3) currentDate と私が持っている古い日付の違いを取得する方法は、currentDate - oldDate のようなもので、「AM_PM」の問題はどうですか? この関数を手動で行う必要がありますか?

Date oldDate = ...
Date currentDate = new Date();
long dt = currentDate.getTime() - oldDate.getTime();
于 2015-06-24T20:39:10.997 に答える
0

1) 現在の日付を取得するには:

Date = new Date();

2) 日付を手動で設定するには、カレンダーを使用することをお勧めします。

Calendar c = new GregorianCalendar();
c.set(Calendar.MONTH, 1);
c.set(Calendar.YEAR, 2015);
// ... and so on
Date date = c.getTime();

3) 2 つの日付間のミリ秒単位の距離を計算します。

Date d1 = ....;
Date d2 = ....;
long distance = d1.getTime() - d2.getTime();
于 2015-06-24T20:55:15.430 に答える