Get day, month and year separately using SimpleDateFormat

前端 未结 8 863
無奈伤痛
無奈伤痛 2021-02-08 19:45

I have a SimleDateFormat like this

SimpleDateFormat format = new SimpleDateFormat(\"MMM dd,yyyy  hh:mm\");
String date = format.format(Date.parse(p         


        
8条回答
  •  北荒
    北荒 (楼主)
    2021-02-08 20:00

        SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy  hh:mm", Locale.ENGLISH);
    
        Date theDate = format.parse("JAN 13,2014  09:15");
    
        Calendar myCal = new GregorianCalendar();
        myCal.setTime(theDate);
    
        System.out.println("Day: " + myCal.get(Calendar.DAY_OF_MONTH));
        System.out.println("Month: " + myCal.get(Calendar.MONTH) + 1);
        System.out.println("Year: " + myCal.get(Calendar.YEAR));
    

提交回复
热议问题