2016-11-08 3 views
0

私のAndroid Studioプロジェクトでは、私はDatePickerDialogを持っています。 onDateSetには、3つの引数があります。ローカル日付形式を使用してDatePickerDialogの後のsetText

  • int selectedYear;
  • int selectedMonth;
  • int selectedDay;

私はデバイスのローカルフォーマットを使用してsetTextを作りたいと思います。 YYYY/MM/DDが、フランスではDD-MM-YYYYのようなものだ

私はそれをどのように操作を行うことができます。

はexempleについては、米国では日付が似ていますか?

@Override 
public void onClick(View view) { 
    int year = cal.get(Calendar.YEAR); 
    int month = cal.get(Calendar.MONTH); 
    int day = cal.get(Calendar.DAY_OF_MONTH); 

    DatePickerDialog datePicker; 
    datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener(){ 
      @Override 
      public void onDateSet(DatePicker datePicker, int selectYear, int selectedMonth, int selectedDay) { 
       textViewDate.setText("HERE I HAVE TO PUT THE DATE WITH THE LOCAL FORMAT); 
      } 
     }, year, month, day); 
     datePicker.show(); 
    } 
}); 

おかげ

答えて

0

あなたは、このメソッドを使用することができます:この方法によって

public String formatDateStyles(Date date, Locale currentLocale) { 

    String result = ""; 
    DateFormat formatter; 

    int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL }; 

    for (int k = 0; k < styles.length; k++) { 
     formatter = DateFormat.getDateInstance(styles[k], currentLocale); 
     result = formatter.format(date); 
    } 

    return result; 
    } 

public Date getDate(int year, int month, int day) { 
     Calendar cal = Calendar.getInstance(); 
     cal.set(Calendar.YEAR, year); 
     cal.set(Calendar.MONTH, month); 
     cal.set(Calendar.DAY_OF_MONTH, day); 
     cal.set(Calendar.HOUR_OF_DAY, 0); 
     cal.set(Calendar.MINUTE, 0); 
     cal.set(Calendar.SECOND, 0); 
     cal.set(Calendar.MILLISECOND, 0); 
     return cal.getTime(); 
    } 

textViewDate.setText(formatDateStyles(getDate(selectYear, selectedMonth, selectedDay), new Locale("en", "US"))); 

詳細here

この

は私のコードです。

0

これを試してみてください:

public void onDateSet(DatePicker datePicker, int selectYear, int selectedMonth, int selectedDay) { 
      DataFormat dateFormat= android.text.format.DateFormat.getDateFormat(getApplicationContext()); 
      cal.set(selectYear, selectedMonth, selectedDay) 
      textViewDate.setText(dateFormat.format(cal.getTime())); 
     } 
関連する問題