【问题标题】:How to convert HH:mm:ss to hh:mm AM/PM in android如何在 android 中将 HH:mm:ss 转换为 hh:mm AM/PM
【发布时间】:2017-08-08 19:29:43
【问题描述】:

来自网络服务,接收时间为 23:30:00。我想将其转换为 12 小时的格式。我想输出类似晚上 11:30 的内容(转换为 23:30:00 之后)。

【问题讨论】:

  • //am if (hours >12) { hours = hours - 12; //下午 }

标签: android datetime


【解决方案1】:

使用 SimpleDateFormat 从一种时间/日期格式转换为另一种。

Log.v("12HRS Time", getFormatedDateTime("23:30:00", "HH:mm:ss", "hh:mm a"))

public static String getFormatedDateTime(String dateStr, String strReadFormat, String strWriteFormat) {

    String formattedDate = dateStr;

    DateFormat readFormat = new SimpleDateFormat(strReadFormat, Locale.getDefault());
    DateFormat writeFormat = new SimpleDateFormat(strWriteFormat, Locale.getDefault());

    Date date = null;

    try {
        date = readFormat.parse(dateStr);
    } catch (ParseException e) {
    }

    if (date != null) {
        formattedDate = writeFormat.format(date);
    }

    return formattedDate;
}

对于SimpleDateFormat参考:-https://developer.android.com/reference/java/text/SimpleDateFormat.html

【讨论】:

    【解决方案2】:

    使用日期模式获取它的最简单方法 - h:mm a, where

    h - Hour in am/pm (1-12)
    m - Minute in hour
    a - Am/pm marker
    

    代码 sn-p:

    DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
    

    更多信息请看link

    【讨论】:

      【解决方案3】:

      试试SimpleDateFormat

      例子

      Date dateToFormat = new Date(someDate);
      SimpleDateFormat dateFormatExpression = new SimpleDateFormat("hh:mm a");
      String formattedDate = dateFormatExpression.format(dateToFormat);
      

      【讨论】:

        【解决方案4】:

        只需使用SimpleDateFormat

        像这样.....

        public String GetTimeWithAMPMFromTime(String dt) {
                try {
                    SimpleDateFormat inFormat = new SimpleDateFormat("HH:mm:ss");
                    Date date = inFormat.parse(dt);
                    SimpleDateFormat outFormat = new SimpleDateFormat("hh:mm a");
                    String goal = outFormat.format(date);
                    return goal;
                } catch (ParseException e) {
                    e.printStackTrace();
                    return "";
                }
            }
        

        调用方法...

        String YOUR_TIME = GetTimeWithAMPMFromTime(WEB_SERVICE_TIME);
        

        【讨论】:

          【解决方案5】:

          我就这样得到了答案,我已经测试过了(y)

           startTime = "2013-02-27 21:06:30";
                              StringTokenizer tk = new StringTokenizer(startTime);
                              String date = tk.nextToken();  
                              String time = tk.nextToken();
          
                              SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
                              SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
                              Date dt;
                              try {    
                                  dt = sdf.parse(time);
                                  System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here
                              } catch (ParseException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }
          

          【讨论】:

            【解决方案6】:

            试试这个。

            DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
            

            【讨论】:

              【解决方案7】:
              android.text.format.DateFormat myDate = new android.text.format.DateFormat();
              myDate.format("hh:mm a", new java.util.Date());
              

              DateFormat

              【讨论】:

                【解决方案8】:
                public static void convert(String time) throws Exception {
                       try {       
                           SimpleDateFormat t24 = new SimpleDateFormat("HH:mm");
                           SimpleDateFormat t12 = new SimpleDateFormat("hh:mm a");
                           Date date = t24.parse(time);
                           System.out.println(t12.format(date));
                       } catch (Exception e) {
                           e.printStackTrace();
                       }
                   }
                

                【讨论】:

                  【解决方案9】:

                  一行代码

                  String time = null;
                  try{
                    time = new SimpleDateFormat("HH:mm").format(new SimpleDateFormat("HH:mm:ss").parse("your_time");
                  }catch{
                  }
                  

                  your_time 中,您必须提供要转换的输入。

                  【讨论】:

                  • 感谢您的贡献。它似乎没有给出要求的输出,也似乎没有添加其他答案中没有的任何内容?
                  • 此外,我认为教年轻人毫无保留地使用早已过时且臭名昭著的SimpleDateFormat 类作为第一选择是不明智的建议。今天我们在java.time, the modern Java date and time API 和它的DateTimeFormatter 中做得更好。是的,您可以在 Android 上使用它。对于较旧的 Android,请参阅 How to use ThreeTenABP in Android Project
                  • 感谢您的建议@OleV.V。但我认为对于熟悉此类的人来说是更好的解决方案。
                  猜你喜欢
                  • 2017-04-08
                  • 2014-02-06
                  • 1970-01-01
                  • 2021-07-26
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多