2017-12-18 18 views

答えて

1

あなたは

private int mYear; 
private int mMonth; 
private int mDay; 
private int mHour; 
private int mMinute; 
private Calendar mCalendar; 

private void datePicker() { 
    mCalendar = Calendar.getInstance(); 

    mYear = mCalendar.get(Calendar.YEAR); 
    mMonth = mCalendar.get(Calendar.MONTH); 
    mDay = mCalendar.get(Calendar.DAY_OF_MONTH); 

    DatePickerDialog datePickerDialog = new DatePickerDialog(this, 
      new DatePickerDialog.OnDateSetListener() { 

       @Override 
       public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 

        mCalendar.set(Calendar.YEAR, year); 
        mCalendar.set(Calendar.MONTH, monthOfYear); 
        mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
        //here you request timePicker Dialog 
        timePicker(); 

       } 
      }, mYear, mMonth, mDay); 
    datePickerDialog.show(); 

} 


private void timePicker() { 
    mHour = mCalendar.get(Calendar.HOUR_OF_DAY); 
    mMinute = mCalendar.get(Calendar.MINUTE); 

    TimePickerDialog mTimePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { 
     @Override 
     public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 

//  here you go, you got selectedHour and selectedMinute, convert them into String 
      mCalendar.set(Calendar.HOUR_OF_DAY, selectedHour); 
      mCalendar.set(Calendar.MINUTE, selectedMinute); 
//     Now if you want current time in long format 

      long currentTime= mCalendar.getCurrentTimeInMilis(); 
       // now you can format this to dd-MM using SimpleDateFormat.. 

System.out.println(formateDate(curentTime)); 

     } 
    }, mHour, mMinute, false); 
    mTimePicker.setTitle("Select Time"); 
    mTimePicker.show(); 
} 

//適切に変換、それが助けとにかく場合は、ここに投稿する前にビットを探索している必要があります日付形式

public static String formatDate(long dateInMillis) { 
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm a", Locale.getDefault()); 
    Date date = new Date(dateInMillis); 
    return dateFormat.format(date); 
} 

これだけです。

関連する問題