Get value of day month from Date object in Android?

前端 未结 10 1964
Happy的楠姐
Happy的楠姐 2020-12-04 10:48

By using this code :

SimpleDateFormat format = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");
Date date = format.parse(dtStart);
return date;
10条回答
  •  既然无缘
    2020-12-04 11:27

    Consider using the java.util.Calendar class.

    String dateString = "20/12/2018";
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    
    Date readDate = df.parse(dateString);
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(readDate.getTime());
    
    Log.d(TAG, "Year: "+cal.get(Calendar.YEAR));
    Log.d(TAG, "Month: "+cal.get(Calendar.MONTH));
    Log.d(TAG, "Day: "+cal.get(Calendar.DAY_OF_MONTH));
    

提交回复
热议问题