JodaTime IllegalArgumentException Invalid format “yyyy-MM-dd hh:mm:ss”

问题: Before to post, i search this format in previous questions but i dont found it Here is the simple code that i want to try : String YYYYMMDD_HHMMSS = "yyyy/MM/dd HH:mm:...

问题:

Before to post, i search this format in previous questions but i dont found it

Here is the simple code that i want to try :

String YYYYMMDD_HHMMSS = "yyyy/MM/dd HH:mm:ss";
DateTimeFormatter yyyyMMddHHmmssFormatter = DateTimeFormat.forPattern(YYYYMMDD_HHMMSS); 
DateTime fromDate = DateTime.parse("2019-01-17 11:01:15", yyyyMMddHHmmssFormatter);

I got this error :

java.lang.IllegalArgumentException: Invalid format: "2019-01-17 11:01:15" is malformed at "-01-17 11:01:15"

I think, i must change something in the string format ?

Thank's for your help


回答1:

You shoud use format yyyy-MM-dd HH:mm:ss for input 2019-01-17 11:01:15, yyyy/MM/dd HH:mm:ss works when the input like 2019/01/17 11:01:15.

Update

You need two format to convet:

DateTimeFormatter inputFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); 
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss"); 
DateTime fromDate = DateTime.parse("2019-01-17 11:01:15", inputFormat);
System.out.println(outputFormat.print(fromDate));

回答2:

Try changing

String YYYYMMDD_HHMMSS = "yyyy/MM/dd HH:mm:ss";

to

String YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
  • 发表于 2019-01-19 04:15
  • 阅读 ( 699 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除