Get day, month and year separately using SimpleDateFormat

前端 未结 8 865
無奈伤痛
無奈伤痛 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:01

    Are you accepting this ?

    int day = 25 ; //25
    int month =12; //12
    int year = 1988; // 1988
    Calendar c = Calendar.getInstance();
    c.set(year, month-1, day, 0, 0);    
    SimpleDateFormat format =   new SimpleDateFormat("MMM dd,yyyy  hh:mm");
    System.out.println(format.format(c.getTime()));
    

    Display as Dec 25,1988 12:00

    UPDATE : based on Comment

    DateFormat format =   new SimpleDateFormat("MMM");
    System.out.println(format.format(format.parse("Jan,23,2014")));
    

    NOTE: Date.parse() is @deprecated and as per API it is recommend to use DateFormat.parse

提交回复
热议问题