【问题标题】:how to convert string to Datein android(java)如何在android(java)中将字符串转换为日期
【发布时间】:2012-10-07 18:02:18
【问题描述】:

在我的 android 应用程序中,我从 web 服务中获取了一个字符串,如下所示 -

 2012-10-17 22:54:00          or        2012-10-17 08:48:00

我想把这个字符串转换成下面的字符串-

2012-10-17 10:54:00 PM        or        2012-10-17 08:48:00 AM

如何在android中做到这一点。我已经完成了以下操作,但无法得到这样的结果。 像 gethours(),getminutes() 这样的日期类方法也被贬低了。如何做到这一点?

我做了什么-

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
        try {
            convertedDate = dateFormat.parse(publishtime);
            dateFormat.setLenient(true);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        Log.i("", "CREATE Date"+convertedDate);//the resut of log is - CREATE DateMon Jan 16 00:10:00 GMT+05:30 2012


        Log.i("", "HOURS:"+hour);
        if(hour>=12)
            publishtime=publishtime+" PM";
        else
            publishtime=publishtime+" AM";

【问题讨论】:

    标签: android string date calendar


    【解决方案1】:

    试试这个

        String publishTime="2012-10-17 22:54:00";
        SimpleDateFormat inputTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat outputTime= new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
        Date date=inputTime.parse(publishTime);
        publishTime = outputTime.format(d);
        Log.i("", "CREATE Time"+publishTime);
    

    【讨论】:

    • 你能告诉我这个星期三 2012 年 10 月 24 日 10:58:06 转换为 24/11/2012 10:58 的日期格式应该是什么
    【解决方案2】:

    首先,您没有设置正确的日期解析模式,应该是

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    

    要获得转换后的字符串,请使用

    SimpleDateFormat convertedFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
    convertedFormat.format(convertedDate);
    

    【讨论】:

    • 是的,有时人们会回答,而其他人仍然会写下他们的答案,所以毕竟提交后你会得到几个非常相似的答案:) 当然首先值得接受。
    【解决方案3】:
    SimpleDateFormat dfTime_a= new SimpleDateFormat("yyyy-MM-dd h:mm:ss a");
    SimpleDateFormat dfTime= new SimpleDateFormat("yyyy-MM-dd h:mm:ss");
    
    public String formatTime(String str)
    {
        java.util.Date d = null;
    
        try {
            d = dfTime.parse(str);
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        str = dfTime_a.format(d);
        return str;
    }
    

    检查

    formatTime("2012-10-17 22:54:00") 给出 op 为 2012-10-17 10:54:00 pm

    【讨论】:

    • 伟大的答案公羊。非常感谢。
    • 我在这个答案中发现了一个问题。 12 点到 13 点(即 12 点到 1 点)之间的时间显示--> 12:12:00 AM 而不是 12:12:00 PM
    • 只需更改 Simpledateformat 参数即可解决此问题。SimpleDateFormat dfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");SimpleDateFormat dfTime_a = new SimpleDateFormat("yyyy-MM-dd hh: mm:ss a");
    • 你能告诉我这个星期三 2012 年 10 月 24 日 10:58:06 转换为 24/11/2012 10:58 的日期格式应该是什么
    • 尝试使用 EEE, dd MMM yyyy HH:mm:ss 作为您的日期 Wed, 24 Oct 2012 10:58:06
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2012-01-24
    • 2012-04-14
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多