Skip to content Skip to sidebar Skip to footer

How To Get Day, Month, Year And Hour, Minutes, Second From Datetime Format?

I tried this following code but its a deprecated in android SimpleDateFormat simpleDateFormat = new SimpleDateFormat('MM/dd/yyyy HH:mm:ss'); try { Date date1 = simpleDateFor

Solution 1:

Set calendar time using simpleDateFormat.parse() and get field value from calendar :

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();
}

Post a Comment for "How To Get Day, Month, Year And Hour, Minutes, Second From Datetime Format?"