NutzCN Logo
问答 时间格式化问题,不晓得如何处理了
发布于 2291天前 作者 Rekoe 2099 次浏览 复制 上一个帖子 下一个帖子
标签:
System.out.println(Times.format("MMMM dd, yyyy 'at' h:ma", Times.D("2017-11-30 14:41")));
		
String timeStr = "November 30, 2017 at 2:41pm";
System.out.println(Times.parseq("MMMM dd, yyyy 'at' h:ma",timeStr));

上面打印的数据是

November 30, 2017 at 2:41PM

为啥还原的时候会

Exception in thread "main" java.lang.RuntimeException: java.text.ParseException: Unparseable date: "November 30, 2017 at 2:41pm"
	at org.nutz.lang.Lang.wrapThrow(Lang.java:185)
	at org.nutz.lang.Times.parseq(Times.java:979)
	at com.rekoe.TimeTest.main(TimeTest.java:13)
Caused by: java.text.ParseException: Unparseable date: "November 30, 2017 at 2:41pm"
	at java.text.DateFormat.parse(DateFormat.java:366)
	at org.nutz.lang.Times.parse(Times.java:1006)
	at org.nutz.lang.Times.parseq(Times.java:976)
	... 1 more

路过的看看 谢谢

4 回复

原以为是时区问题,结果还是报错

    public static void main(String[] args) throws Exception {
        DateFormat df = new SimpleDateFormat("MMMM dd, yyyy 'at' h:ma", Locale.ENGLISH);
        Date date = Times.D("2017-11-30 14:41");
        System.out.println(date);
        System.out.println(df.format(date));
        System.out.println(df.parse(df.format(date)));
    }

https://zhidao.baidu.com/question/559031066.html
这个有类似的写法 是没问题的
在按照这个测试

@wendal
试出来了

String timeStr = "November 30, 2017 at 2:41 pm";
		Times.parse("MMMM d, yyyy 'at' h:m a", timeStr);

表示上午下午的 a 和 h:m 中间需要加空格区分

现在笨的办法是 2:41pm替换成 2:41 pm

添加回复
请先登陆
回到顶部