2014-11-20 3 views
0

나는 안드로이드DateTime 형식으로 일, 월, 년,시, 분, 초를 얻는 방법은 무엇입니까? 년, 월, 일 달력을 사용하여 얻는 방법되지 않습니다

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 

try { 

    Date date1 = simpleDateFormat.parse("11/20/2014 8:10:00 AM"); 

    Log.e("date", "" + date1.getYear()); 

    Log.e("month", "" + date1.getmonth()); 

    Log.e("year", "" + date1.getYear()); 

    Log.e("hour", "" + date1.getHours()); 

    Log.e("minutes", "" + date1.getMinutes()); 

    Log.e("seconds", "" + date1.getSeconds()); 

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

그것에서 사용되지 않는이 다음 코드 있지만 그것 시도?

+0

를 사용하여 달력 클래스 –

+0

있는 샘플 코드를? – Deva

+0

http://developer.android.com/reference/java/util/Calendar.html –

답변

6
simpleDateFormat.parse을 (사용

설정 달력 시간) 및 달력에서 필드 값을 얻을 :

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
Calendar calendar = Calendar.getInstance(); 
try { 
    calendar.setTime(simpleDateFormat.parse("11/20/2014 8:10:00 AM")); 

    Log.e("date", "" + calendar.get(Calendar.DAY_OF_MONTH)); 

    Log.e("month", ""+calendar.get(Calendar.MONTH)); 

    Log.e("year", ""+calendar.get(Calendar.YEAR)); 

    Log.e("hour", ""+calendar.get(Calendar.HOUR)); 

    Log.e("minutes", ""+calendar.get(Calendar.MINUTE)); 

    Log.e("seconds", ""+calendar.get(Calendar.SECOND)); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 
+1

11 월 대신에 10이 반환됩니다. – Deva

+1

월별로 월별로 0부터 11까지 시작되므로 월별 값에 +1을 추가하십시오. –

관련 문제