2017-02-12 4 views
0

私はAndroidプログラミングが初めてで、現在はアプリを開発中です。誰かが範囲7日以内にユーザーが入力している日付を検証する方法を教えてもらえますか? ので、私は2つの文字列「startDay」及び2017年6月2日=アンドロイドの範囲を検証する方法は?

ユーザ入力 startDay = 2017年1月2日 endDayは、それが次のステップに行った「endDay」を有します。 iは

@Override 
public void onClick(View v) { 

    if (v==btnSearch){ 

     String startDaystr = startDay.getText().toString(); 
     String startMonthtstr = startMonth.getText().toString(); 
     String endDaystr = endDay.getText().toString(); 
     String endMonthstr = endMonth.getText().toString(); 
     String endYearstr = endYear.getText().toString(); 



     if(Integer.valueOf(startDaystr) >1 && Integer.valueOf(endDaystr) < 8){ 
      sharedPreferenceCustom = SharedPreferenceCustom.getInstance(getContext()); 
      sharedPreferenceCustom.putSharedPref("startDay", startDaystr); 
      sharedPreferenceCustom.putSharedPref("startMonth",startMonthtstr); 
      sharedPreferenceCustom.putSharedPref("endDay",endDaystr); 
      sharedPreferenceCustom.putSharedPref("endMonth",endMonthstr); 
      sharedPreferenceCustom.putSharedPref("endYear",endYearstr); 
      startActivity(new Intent(getActivity(), activity_history.class)); 
     }else{ 
      Toast.makeText(getActivity(), "Maximum 7 days!", Toast.LENGTH_LONG).show(); 
     } 



    } 
} 

が、イムしたコードがある

「最大7日間」

ユーザ入力 startDay = 2017年1月2日 endDay = 2017年9月2日、それはメッセージを返す場合入力 startDay = 22/2/2017 endDay = 25/2/2017

結果が「最大7日間」で、次のステップ

に復帰する必要がありますme..imを検索してみました助けてくださいまたではなく、そのような解決策を得る。..

答えて

0

startYearStrを追加しましたので、ご注意ください。コードの下 用途:

String startDaystr = startDay.getText().toString(); 
String startMonthtstr = startMonth.getText().toString(); 
String startYearstr = startYear.getText().toString(); 

String endDaystr = endDay.getText().toString(); 
String endMonthstr = endMonth.getText().toString(); 
String endYearstr = endYear.getText().toString(); 


Calendar startDate = Calendar.getInstance(); 
startDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(startDaystr)); 
startDate.set(Calendar.MONTH, Integer.parseInt(startMonthtstr)); 
startDate.set(Calendar.YEAR, Integer.parseInt(startYearstr)); 
startDate.set(Calendar.HOUR_OF_DAY, 0); 
startDate.set(Calendar.MINUTE, 0); 
startDate.set(Calendar.SECOND, 0); 

Calendar endDate = Calendar.getInstance(); 
endDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(endDaystr)); 
endDate.set(Calendar.MONTH, Integer.parseInt(endMonthstr)); 
endDate.set(Calendar.YEAR, Integer.parseInt(endYearstr)); 
endDate.set(Calendar.HOUR_OF_DAY, 0); 
endDate.set(Calendar.MINUTE, 0); 
endDate.set(Calendar.SECOND, 0); 

long diff = endDate.getTimeInMillis() - startDate.getTimeInMillis(); 
long dayCount = diff/(24 * 60 * 60 * 1000); 

dayCountはあなたに2つの日付の間の日差を与えます。

+0

ありがとうございますAsif、私はそれを試して、それは完全に動作します。 :) diff /(24 * 60 * 60 * 1000)について質問したい。私は1000からどこに説明することができますか? – Jane

+0

1000はミリ秒です。 –

0

は、Java DateまたはCalendarオブジェクトを使用するのではなく、あなたがやっているようなのEditTextからの最初の日付で読み込みが安全ではないでしょうたい日付を比較すること。入力を検証していない場合、ユーザーは「2014年2月2日」を期待しているときに「2016年2月2日」のように入力することができます。自由形式のEditTextから日付ピッカーに変更することができますユーザーが最初に日付を入力する方法を制御します。

サポートされているJavaオブジェクトに2つの日付が設定されると、それらを互いに比較できます。 This answer has an example how to do the comparison.

+0

あなたのアイデアと解決策に感謝します。私はそれを試してみましょう:) – Jane

関連する問題