【问题标题】:Extract only time in 12 hr format ( AM or PM ) from date time String android从日期时间字符串 android 中仅提取 12 小时格式(上午或下午)的时间
【发布时间】:2014-12-15 11:08:57
【问题描述】:

我有一个字符串,其中包含从 Web 服务检索到的日期时间,格式为 "2014-11-12 16:19:00" 我只需要以 12 小时格式 AM 或 PM 提取时间。 我试过了,但没有得到想要的结果。

public Date validateBreakingNewsDateDate() throws Exception{                            
        Date date = null;
        String dtStart = "2014-11-12 16:19:00";  
        SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        try {  
             date= format.parse(dtStart);  
            Log.e("date", "kardate "+date); 
        } catch (ParseException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }   
        return date;
}

【问题讨论】:

  • 你可以做的是提取时间,然后检查它的上午或下午
  • this的可能重复
  • 你想得到16:19:00OR 4:19:00 PM吗??

标签: android date time calendar


【解决方案1】:

试试下面的代码sn-p

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy     hh:mm:ss a");

【讨论】:

  • 非常感谢。完美而简单的答案..+1
【解决方案2】:

试试这个...

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(dtStart);
String newstring = new SimpleDateFormat("hh:mm aa").format(date);

【讨论】:

    【解决方案3】:

    将您的日期传递给此函数,它将返回时间

       public String setTime(String date) {
        final String OLD_FORMAT = "yyyy-MM-dd HH:mm:ss";
        final String NEW_FORMAT = "h:mm a";
    
        String newDate = "";
        try {
    SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
            Date d = sdf.parse(date);
            sdf.applyPattern(NEW_FORMAT);
            newDate = sdf.format(d);
        } catch (ParseException e) {
            // TODO: handle exception
        }
        return newDate;
    }
    

    例子

     public String setTime(String date) {
        final String OLD_FORMAT = "yyyy-MM-dd HH:mm:ss";
        final String NEW_FORMAT = "h:mm a";
    
        String newDate = "";
        try {
            sdf = new SimpleDateFormat(OLD_FORMAT);
            Date d = sdf.parse("2014-11-12 16:19:00");
            sdf.applyPattern(NEW_FORMAT);
            newDate = sdf.format(d);
        } catch (ParseException e) {
            // TODO: handle exception
        }
        return newDate;
    }
    

    输出=下午 4:19

    【讨论】:

    • 可能是你传递了错误的日期格式然后它会导致解析异常尝试调试你的代码'
    【解决方案4】:

    您可以使用SimpleDateFormat 获取 12 小时时间,如下所示

    Date date = null;
    String dtStart = "2014-11-12 16:19:00";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss");
    try {
        date = format.parse(dtStart);
        Log.e("date", "kardate " + date);
        // here hh means hours in 12 hr format and a stands for AM/PM
        format = new SimpleDateFormat("hh:mm:ss a");
        String desiredTime= format.format(date);
        // You will get newDate 04:19:00 pm
        Log.e("date", "newDate " + desiredTime);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    希望对你有所帮助ツ

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 2012-12-03
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2011-11-16
      • 2015-06-02
      • 1970-01-01
      相关资源
      最近更新 更多