字符串到日历的转换,同时保持时间

发布于 2024-11-15 19:07:53 字数 790 浏览 1 评论 0原文

我正在尝试输出一个也包含时间的日历对象。我已经使用 SimpleDateFormat 编写了一个方法来执行此操作。

    public static Calendar stringToCalendar(String string)
{
    Calendar cal = Calendar.getInstance();
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd - HHmmss");
    Date date = new Date();
    try {
      date = formatter.parse(string);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    cal.setTime(date);
    return cal;
}

我的输入字符串是“20110614-15:05:00”

但是它不喜欢这个值并给了我以下错误:

java.text.ParseException: Unparseable date: "20110614-15:05:00"
at java.text.DateFormat.parse(DateFormat.java:337)
at transformer.Converter.stringToCalendar(Converter.java:22)

为什么它不能检测到 15:05:00 作为时间?

I'm trying to output a calendar object which also contains the time. I've written a method to do this using the SimpleDateFormat.

    public static Calendar stringToCalendar(String string)
{
    Calendar cal = Calendar.getInstance();
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd - HHmmss");
    Date date = new Date();
    try {
      date = formatter.parse(string);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    cal.setTime(date);
    return cal;
}

my input string is "20110614-15:05:00"

However it doesn't like this value and gave me the following error:

java.text.ParseException: Unparseable date: "20110614-15:05:00"
at java.text.DateFormat.parse(DateFormat.java:337)
at transformer.Converter.stringToCalendar(Converter.java:22)

Why can it not detect the 15:05:00 as a time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乱了心跳 2024-11-22 19:07:54

您需要使指定的日期格式与数据的实际格式相匹配。在这种情况下,您缺少冒号,并且您期望不存在的空格。您的格式似乎应为 "yyyyMMdd-HH:mm:ss"

此外,捕获并仅记录错误,然后继续当前日期,就好像什么也没发生一样,几乎肯定是错误的做法。为什么不直接声明 stringToCalendar 抛出 ParseException 呢?

(最后,我个人发现 Joda Time 是一个更好用于日期和时间工作的 API。如果你正在做这样的重要工作,你至少应该看看它并考虑切换。)

You need to make the date format you specify match the actual format of the data. In this case you're missing the colons, and you're expecting spaces which don't exist. It looks like your format should be "yyyyMMdd-HH:mm:ss".

Additionally, catching and just logging an error but then continuing with the current date as if nothing had happened is almost certainly the wrong thing to do. Why not just declare that stringToCalendar throws ParseException?

(Finally, I personally find that Joda Time is a much nicer API for date and time work. If you're doing significant work like this, you should at least look at it and consider switching.)

没︽人懂的悲伤 2024-11-22 19:07:54

您是否缺少冒号,例如 yyyyMMdd - HH:mm:ss?您还可以查看额外的空间。

Are you not missing the colons, like yyyyMMdd - HH:mm:ss? Also you may watch the additional spaces.

蝶舞 2024-11-22 19:07:54
public static Calendar string2Calendar(String string) {
    SimpleDateFormat formatter;
    Date date = null;
    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    try {
        date = (Date) formatter.parse(string);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    Logger.printMessage("tag", String.format("%tc", cal),
            Logger.DEBUG);
    return cal;
}
public static Calendar string2Calendar(String string) {
    SimpleDateFormat formatter;
    Date date = null;
    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    try {
        date = (Date) formatter.parse(string);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    Logger.printMessage("tag", String.format("%tc", cal),
            Logger.DEBUG);
    return cal;
}
醉生梦死 2024-11-22 19:07:54

“yyyyMMdd - HHmmss”-> “yyyyMMdd-hh:mm:ss

"yyyyMMdd - HHmmss" -> "yyyyMMdd-hh:mm:ss"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文