How do I change date time format in Android?

前端 未结 10 1893
故里飘歌
故里飘歌 2020-11-29 07:19

I am displaying the date and time in Android with this format:
2013-06-18 12:41:24

How can I change it to the following format?
18-j

10条回答
  •  悲&欢浪女
    2020-11-29 08:01

    Here is working code

    public String parseDateToddMMyyyy(String time) {
        String inputPattern = "yyyy-MM-dd HH:mm:ss";
        String outputPattern = "dd-MMM-yyyy h:mm a";
        SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
        SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
    
        Date date = null;
        String str = null;
    
        try {
            date = inputFormat.parse(time);
            str = outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }
    

    Documentation: SimpleDateFormat | Android Developers

提交回复
热议问题