【问题标题】:how to get date from 12 hour format to 24 hour format in android如何在android中将日期从12小时格式转换为24小时格式
【发布时间】:2012-09-13 06:12:35
【问题描述】:

你好,我有约会之类的..

String date="09:00 AM"

但我需要09:00:00

所以我使用以下。

 String date="09:00 AM"  
 SimpleDateFormat f1 = new SimpleDateFormat("hh:mm:ss");
 Date d1= f1.parse(date);

但它给了我解析异常。

请帮我找到这个。

提前致谢。

【问题讨论】:

    标签: android android-date


    【解决方案1】:

    这不是解析那个日期的正确格式,你需要这样的东西:

    "hh:mm a"
    

    一旦你有了一个日期对象,你就可以使用不同的格式来输出它。以下代码段显示了如何:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    String date = "09:27 PM";
    
    SimpleDateFormat h_mm_a   = new SimpleDateFormat("h:mm a");
    SimpleDateFormat hh_mm_ss = new SimpleDateFormat("HH:mm:ss");
    
    try {
        Date d1 = h_mm_a.parse(date);
        System.out.println (hh_mm_ss.format(d1));
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    这个输出:

    21:27:00
    

    如你所料。

    【讨论】:

      【解决方案2】:
      Date c = Calendar.getInstance().getTime();
      
      //day of the month
      
      //EEEE---full day name
      //EEE- 3 chars of the day
      SimpleDateFormat outFormat = new SimpleDateFormat("EEE");
      String dayof_month = outFormat.format(c);
      
      //date
      //MMM--3 chars of month name
      //MM--number of the month
      SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
      String formattedDate = df.format(c);
      
      //time
      //hh---12 hrs format
      //HH---24 hrs format
      // a stands for AM/PM
      DateFormat date = new SimpleDateFormat("hh:mm:ss a");
      String localTime = date.format(c);
      
      login_date_text.setText(dayof_month +" , "+formattedDate);
      login_time_text.setText(localTime);
      
      Log.e("testing","date ==" +dayof_month +" , "+formattedDate);
      Log.e("testing","time ==" +localTime);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        相关资源
        最近更新 更多