【问题标题】:24 hour clock not working in dateformat android24小时时钟在日期格式android中不起作用
【发布时间】:2011-04-22 11:45:39
【问题描述】:

我做了一个应用程序,我需要在其中执行 24 小时格式的日期转换。 这是我的代码。

GregorianCalendar c = new GregorianCalendar(Locale.GERMANY);
            c.set(2011, 04, 29,0,0,0);
            String cdate = (String) DateFormat.format("yyyy-MM-dd HH:mm:ss", c.getTime());
            Log.i(tag,cdate);

现在当我检查我的日志时,这里是输出:

04-22 12:44:15.956: INFO/GridCellAdapter(30248): 2011-04-29 HH:00:00

为什么没有设置小时字段。我在制作日历对象时明确传递了 0,但它仍然在 LOG 中显示 HH。 我在传递 hh 时尝试过,它正在设置,但 HH 没有被设置。

可能是什么问题?

提前谢谢你。

【问题讨论】:

    标签: java android date time


    【解决方案1】:
    String cdate = (String) DateFormat.format("yyyy-MM-dd kk:mm:ss", c.getTime());
    

    【讨论】:

    【解决方案2】:

    尝试使用 SimpleDateFormat,它是 DateFormat 的子类。这可能会解决您遇到的问题。

    例如打印当前日期时间

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_H-mm-ss",Locale.US);
    String formattedDateString = dateFormat.format(new java.util.Date());
    // result is something like "2013-02-14_18-44-15"
    

    【讨论】:

    【解决方案3】:

    试试这个

    Long someLongDate = 240000L 
    // I was working with a Long in my own code. 
    // I'm sure you can use something like varDate.getLong()
    TextView textView = (TextView) view;                 // your view of interest
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");     
    textView.setText(sdf.format(new Date(someLongDate)));
    

    如果有帮助 - 不要忘记检查我的帖子作为答案..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 2014-08-25
      相关资源
      最近更新 更多