【问题标题】:how to convert date format in android如何在android中转换日期格式
【发布时间】:2012-01-19 05:39:51
【问题描述】:

我正在将日期转换为YYYY/MM/DD HH:MM:SS 格式的字符串。我想将其更改为mm/dd/yyyy HH:mm:ss,它还会显示上午和下午我该怎么做。请帮助我

谢谢

【问题讨论】:

标签: android date


【解决方案1】:
public static String getFormatedDate(String strDate, String sourceFormate,
        String destinyFormate) {
    SimpleDateFormat df;
    df = new SimpleDateFormat(sourceFormate);
    Date date = null;
    try {
        date = df.parse(strDate);

    } catch (ParseException e) {
        e.printStackTrace();
    }

    df = new SimpleDateFormat(destinyFormate);
    return df.format(date);

}

并像使用一样

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

【讨论】:

    【解决方案2】:

    要获取 AM PM 和 12 小时日期格式,请使用 hh:mm:ss a 作为字符串格式化程序,其中 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);
    

    示例

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

    【讨论】:

    • 不错的答案,帮了我很多..犯了错误HH:mm:ss a
    • 不使用 hh 还给你012?我赞成这个答案。
    【解决方案3】:

    您可以将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();  
    

    【讨论】:

      【解决方案4】:

      您可以使用 SimpleDateFormat 或 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
      }
      

      【讨论】:

        【解决方案5】:

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

        您需要提供格式化程序。

        请参阅以下内容: 时间:http://developer.android.com/reference/android/text/format/Time.html 格式化程序:http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html

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

        【讨论】:

          【解决方案6】:

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

          Time time = new Time();
          time.set(Long.valueOf(yourTimeString));
          

          如果你真的需要一个 Date 对象,试试这个。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-12-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-08
            相关资源
            最近更新 更多