Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中转换日期格式_Android_Date - Fatal编程技术网

如何在android中转换日期格式

如何在android中转换日期格式,android,date,Android,Date,我正在以YYYY/MM/DD HH:MM:SS格式将日期转换为字符串。我想将其更改为MM/DD/YYYY HH:MM:SS,并且它将显示am和PM如何执行此操作。请帮助我 谢谢使用android.text.format.Time进行转换。您可以将时间作为文本传递,它将使用timeInstance.format(“”)以所需格式返回时间 您需要提供格式化程序 请参阅以下内容: 时间: 格式化程序: 您可以使用格式化程序字符串的任意组合来使用它:)只需使用Time类即可。尝试类似的方法 Time t

我正在以
YYYY/MM/DD HH:MM:SS
格式将日期转换为字符串。我想将其更改为
MM/DD/YYYY HH:MM:SS
,并且它将显示am和PM如何执行此操作。请帮助我


谢谢

使用
android.text.format.Time
进行转换。您可以将时间作为文本传递,它将使用
timeInstance.format(“”)以所需格式返回时间

您需要提供格式化程序

请参阅以下内容: 时间: 格式化程序:


您可以使用格式化程序字符串的任意组合来使用它:)

只需使用Time类即可。尝试类似的方法

Time time = new Time();
time.set(Long.valueOf(yourTimeString));
如果你真的需要一个日期对象,试试这个

Date date = new Date(Long.parse(yourTimeString));

您可以使用SimpleDataFormat或DateFormat来实现此目的,以下是示例

SimpleDateFormat gsdf = new SimpleDateFormat("YYYY/MM/DD HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
try{
    Date d = gsdf.parse("2011/12/13 16:17:00");
    System.out.println("new date format " + sdf.format(d));
}catch(Exception){
     // handle exception if parse time or another by cause
}
可以将用于相同类型的任何日期操作

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();  

要获取AM PM和12小时日期格式,请使用
hh:mm:ss a
作为字符串格式化程序,其中
hh
表示12小时格式,
a
表示AM PM格式

注:HH表示
24
hour,HH表示
12
hour日期格式

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);
和使用一样

getFormatedDate(strDate,
            "yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy") 

很好的回答,帮了我很大的忙..犯了一个错误
HH:mm:ss a
没有使用HH也给你
0
12
?我对这个答案投了赞成票。我创建了一个简单的方法来实现这一点。提到
getFormatedDate(strDate,
            "yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy")