如何在android中转换日期格式

use*_*793 12 android date

我正在将格式化为字符串日期YYYY/MM/DD HH:MM:SS.我想将其更改为mm/dd/yyyy HH:mm:ss并且它还将显示AM和PM我该怎么做.请帮助我

谢谢

Sun*_*hoo 34

要将AM PM和12小时日期格式hh:mm:ss a用作字符串格式化器,WHERE hh 用于12小时格式,a并且用于AM PM格式.

注意: HH表示24小时,hh表示12小时日期格式

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

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);
Run Code Online (Sandbox Code Playgroud)


Par*_*ani 10

您可以将SimpleDateFormat用于相同类型的任何日期操作.

SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
SimpleDateFormat DesiredFormat = new SimpleDateFormat("MM/dd/yyyy HH:MM:SS a");   
                                                             // 'a' for AM/PM

Date date = sourceFormat.parse("2012/12/31 03:20:20");
String formattedDate = DesiredFormat.format(date.getTime());  
// Now formattedDate have current date/time  
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();  
Run Code Online (Sandbox Code Playgroud)