【问题标题】:Time format change according to time settings of phone时间格式根据手机时间设置变化
【发布时间】:2012-10-29 15:44:12
【问题描述】:

我有一个字符串,它从数据库中返回日期和时间为2012-11-09 13:20:17。现在我需要从数据库字符串中获取时间,如果电话时间设置为24 hour,并且如果电话时间设置为12 hour,我应该将其显示为01:20:17 pm。通过这行代码可以得到时间格式,显示是24小时还是12小时

String timefrmt = Settings.System.getString(context.getContentResolver(),Settings.System.TIME_12_24);
SimpleDateFormat timesettingsFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
date="2012-11-09 13:20:17";
Date myDate=null;
try {
     myDate=timesettingsFormat.parse(date);
} catch (ParseException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
} 
if(timefrmt==null){
     timefrmt="hh:mm aa";
}
SimpleDateFormat timeFormat=new SimpleDateFormat(timefrmt);
String time=timeFormat.format(myDate);

我尝试过的示例代码在上面,但它崩溃了。谁能帮我找到解决办法。 非常感谢

【问题讨论】:

  • @Rosalie..你还没有发布你得到的错误......也许 logcat 可以帮助你

标签: android time time-format


【解决方案1】:

试试这个

public static String dateFormateNote(String date) 
{   

    try {
          DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
          DateFormat df2 = new SimpleDateFormat("hh:mm:ss");
          return df2.format(df1.parse(date));
        }
        catch (ParseException e) {
          return date;
        }
}

【讨论】:

    猜你喜欢
    • 2021-01-29
    • 2011-08-12
    • 2011-08-04
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多