了解JodaTime DateTime.parse(string,formatter) [英] Understanding JodaTime DateTime.parse(string, formatter)

查看:2595
本文介绍了了解JodaTime DateTime.parse(string,formatter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

格式化程序在解析

Does the style of the formatter in the parse method of the DateTime class have to match the exact style of the string? For instance, I'm getting a TimeStamp object from the database (Oracle) and converting it to a string. In the database the TimeStamp is stored like this


08-AUG-12 12.00.00.000000000 AM

08-AUG-12 12.00.00.000000000 AM

我将格式化器设置为此样式

I set my formatter to this style

String pattern = "dd-MMM-yy";

我得到这个例外

java.lang.IllegalArgumentException: Invalid format: "08-AUG-12 12.00.00 AM" is malformed at " 12.00.00 AM"

org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)

org.joda.time.DateTime.parse(DateTime.java:144)

这是什么意思,我该如何解决?当我将格式化程序设置为yy-MMM-dd hh.mm.ss aa我没有得到例外,但它在浏览器中打印如下: 2008-08-12T00:00:00.000-04:00 ,但是我需要它作为打印出来dd-MMM-yy hh:mm: ss aa

What exactly does this mean and how would I go about fixing it? When I set my formatter to "yy-MMM-dd hh.mm.ss aa" I don't get an exception but it prints in the browser like this: 2008-08-12T00:00:00.000-04:00, but I need for it to print out as "dd-MMM-yy hh:mm:ss aa"

推荐答案

使用LocalDateTime:

Use LocalDateTime instead:

String input = "08-AUG-12 12.00.00 AM";
String pattern = "dd-MMM-yy hh.mm.ss aa";

LocalDateTime localDateTime = LocalDateTime.parse(input, DateTimeFormat.forPattern(pattern));

编辑

事实上你可以使用DateTime来执行它:

As a matter of fact you can do it with DateTime also:

private static String parseDateTime(String input){
     String pattern = "dd-MMM-yy hh.mm.ss aa";
     DateTime dateTime  = DateTime.parse(input, DateTimeFormat.forPattern(pattern));
     return dateTime.toString("dd-MMM-yy hh:mm:ss aa");
}

这篇关于了解JodaTime DateTime.parse(string,formatter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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