How to get day, month, year and hour, minutes, second from DateTime format?

前端 未结 1 1844
别那么骄傲
别那么骄傲 2021-01-26 06:39

I tried this following code but its a deprecated in android

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

try {

    Date          


        
相关标签:
1条回答
  • 2021-01-26 07:28

    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();
    }
    
    0 讨论(0)
提交回复
热议问题