1

私のアプリケーションには2つのフィールドがあり、1つは日付で、もう1つはデータベーステーブルのrecc(整数)です。

今、私は私の要件を説明します:

ユーザーが今日の日付 (26-07-2012) を入力し、recc を 10 とします。これは、今日の日付から +10 の日付までという意味です。今日の日付から 10 番目の日付を取得しましたが、今日の日付から 10 日目です。確かに来月にも行くことを意味します (26-07-2012 ---- 5-08-2012) が、この特定の月 (つまり) 26-07- の間の日付の数を知る必要があります。 2012----5-08-2012 今月は何日かかりますか。問題を明確に説明したと思います。そうでない場合は、さらに説明する準備ができています。よろしくお願いします。

日付値:

 EditText date=(EditText)findViewById(R.id.startdateexp);       
             String startdate=date.getText().toString();
4

3 に答える 3

0

1. データベースから日付を取得します。

次の方法で、日付から月の日を取得します。

DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
    date = iso8601Format.parse(dateTime);
    } catch (ParseException e) {
    Log.e(TAG, "Parsing ISO8601 datetime failed", e);
    }
    Calendar cal=Calendar.getInstance();
    cal.setTime(date);
    int currentDay= cal.get(Calendar.DAY_OF_MONTH);

次の方法で月の最終日を計算します。

    int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
于 2012-07-26T06:50:20.330 に答える
0

Date クラスを使用している場合は、ミリ秒に変換された日数でオフセットして、新しい Date を作成できます。

Date datePlusRecc = new Date(oldDate.getTime() + recc*86400000);  // 86400000 = 24*60*60*1000

これは、recc が比較的小さい (< 約 20) 場合にのみ使用できることに注意してください。そうしないと、乗算がオーバーフローするためです。

于 2012-07-26T05:52:07.077 に答える
0

ミリ秒単位の日付と組み合わせて java.util.Date クラスを使用するだけです。

for ループで、ミリ秒単位で 1 つのバージョンに 1 日を追加し、それを Date に戻します。Date オブジェクトから月を取得し、現在の月と比較します。月が新しい月になるとすぐに、現在の月の合計日数が表示されます。

Date currentDate = new Date(currentMillis);
long countingMillis = currentMillis;
int daysInSameMonth = 0;
while(true){
    daysInSameMonth++; // if the actual date schould not count move this line at the button of the while
    countingMillis += 1000*60*60*24;
    Date dt = new Date(countingMillis);
    if(currentDate.getMonth() != dt.getMonth())
         break;
}
于 2012-07-26T05:53:32.963 に答える