将字符串转换为日期格式不正确 [英] incorrect conversion of String into date format

查看:189
本文介绍了将字符串转换为日期格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串格式化为特定的日期格式,但转换中存在一些问题。在转换的字符串月份总是来1月。以下是代码段:

I am trying to format a string into specific date format but there is some problem in conversion. In converted string month is always coming "January". Following is the code snippet:

    String date = "30/10/2012-11:11:11";
    DateFormat dateForm = new SimpleDateFormat("dd/mm/yyyy-hh:mm:ss");

    try {
       System.out.println(dateForm.parse(date));
    } 
    catch (ParseException e) {
       e.printStackTrace();
    }

我无法得到这样的理由。
请建议。

I am not able to get the reason behind this. Please suggest.

推荐答案

正如其他人所说,你应该使用 MM 几个月 - 我强烈怀疑你永远想使用 HH 几个小时,而不是 hh HH 是24小时钟; hh 是12小时钟,通常与AM / PM指示符一起使用。

As others have said, you should use MM for months - and I strongly suspect you always want to use HH for hours, instead of hh. HH is the 24 hour clock; hh is the 12 hour clock, usually used in conjunction with an AM/PM designator.

最后,你应该考虑您希望使用哪些时区和区域设置您的 SimpleDateFormat 。即使在没有任何月份或日期名称的情况下,我仍然会明确指定区域设置,以明确表示您不想尝试使用本地化的数据。

Finally, you should consider what time zone and locale you want your SimpleDateFormat to use. Even in this case where there aren't any month or day names, I'd still specify the locale explicitly, to make it clear that you're not trying to use any localized data.

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy-HH:mm:ss", Locale.US);
// TODO: Set the time zone appropriately

这篇关于将字符串转换为日期格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆