Get day, month and year separately using SimpleDateFormat

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

    The accepted answer here suggests to use more than one SimpleDateFormat, but it's possible to do this using one SimpleDateFormat instance and calling applyPattern.

    Note: I believe this post would also be helpful for those who were searching for setPattern() just like me.

    Date date=new Date();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    simpleDateFormat.applyPattern("dd");
    System.out.println("Day   : " + simpleDateFormat.format(date));
    simpleDateFormat.applyPattern("MMM");
    System.out.println("Month : " + simpleDateFormat.format(date));
    simpleDateFormat.applyPattern("yyyy");
    System.out.println("Year  : " + simpleDateFormat.format(date));
    

提交回复
热议问题