如何转换日期格式的android日期、格式、android

2023-09-12 08:04:09 作者:别吵醒、悲伤

我收到日期为字符串 YYYY / MM / DD HH:MM:SS format.I想把它变成了毫米/ DD / YYYY HH:MM:SS ,也将显示AM和PM我该怎么办this.please帮助我

I am getting date into string in YYYY/MM/DD HH:MM:SS format.I want to change it into the mm/dd/yyyy HH:mm:ss and also it will show AM and PM how can I do this.please help me

感谢您

推荐答案

要获得AM PM和12小时日期格式使用 HH:MM:SS 作为字符串格式化WHERE HH 是12小时制和 A 是AM PM格式。

To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.

注意:的HH是 24 小时,HH是 12 小时日期格式

Note: HH is for 24 hour and hh is for 12 hour date format

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
            String newFormat = formatter.format(testDate);

示例

String date = "2011/11/12 16:05:06";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
        Date testDate = null;
        try {
            testDate = sdf.parse(date);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
        String newFormat = formatter.format(testDate);
        System.out.println(".....Date..."+newFormat);