java 字符串到 joda LocalDate 格式为“dd-MMM-yy”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17559772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String to joda LocalDate in format of "dd-MMM-yy"
提问by user149855
I am using JAXB and joda time 2.2. to backup the data from Mysql to XML and restore it back. in my Table I have a Date attribute in format of "16-Mar-05". I successfully store this in XML. but when I want to read it from XML and put it back in Mysql table, I cant get the right format.
我正在使用 JAXB 和 joda time 2.2。将数据从 Mysql 备份到 XML 并将其恢复。在我的表中,我有一个格式为“16-Mar-05”的日期属性。我成功地将其存储在 XML 中。但是当我想从 XML 读取它并将其放回 Mysql 表中时,我无法获得正确的格式。
this is my XMLAdapter class, here in unmarshal method the input String is "16-Mar-05", but I cant get the localDate variable in the format of "16-Mar-05", although I am setting pattern to "dd-MMM-yy". I posted all the options I tried, how can I get my localDate in "dd-MMM-yy" like 16-Mar-05format?
这是我的 XMLAdapter 类,在解组方法中,输入字符串是“16-Mar-05”,但我无法以“16-Mar-05”的格式获取 localDate 变量,尽管我将模式设置为“dd- MMM-YY”。我发布了我尝试过的所有选项,如何以“dd-MMM-yy”格式获取我的 localDate,例如 16-Mar-05 格式?
Thanks!!
谢谢!!
public class DateAdapter extends XmlAdapter<String, LocalDate> {
// the desired format
private String pattern = "dd-MMM-yy";
@Override
public String marshal(LocalDate date) throws Exception {
//return new SimpleDateFormat(pattern).format(date);
return date.toString("dd-MMM-yy");
}
@Override
public LocalDate unmarshal(String date) throws Exception {
if (date == null) {
return null;
} else {
//first way
final DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MMM-yy");
final LocalDate localDate2 = dtf.parseLocalDate(date);
//second way
LocalDate localDate3 = LocalDate.parse(date,DateTimeFormat.forPattern("dd-MMM-yy"));
//third way
DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("dd-MMM-yy");
DateTime dateTime = FORMATTER.parseDateTime(date);
LocalDate localDate4 = dateTime.toLocalDate();
return localDate4;
}
}
回答by MadProgrammer
So I took your code and ran it and it works fine for me...
所以我拿了你的代码并运行它,它对我来说很好用......
The problem, I think, you're having is that you're expecting a LocalDate
object to maintain the format that you original parsed the object with, this is not how LocalDate
works.
我认为,您遇到的问题是您希望LocalDate
对象保持您最初解析对象时使用的格式,这不是LocalDate
工作方式。
LocalDate
is a representation of date or period in time, it is not a format.
LocalDate
是日期或时间段的表示,它不是格式。
LocalDate
has a toString
method which can be used to dump the value of the object, it, this is a internal format used by the object to provide a human readable representation.
LocalDate
有一个toString
方法可以用来转储对象的值,它是对象用来提供人类可读表示的内部格式。
To format the date, you need to use some kind of formater, that will take the pattern you want and a date value and return a String
要格式化日期,您需要使用某种格式化程序,它将采用您想要的模式和日期值并返回一个 String
For example, the following code...
例如,下面的代码...
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String date = "16-Mar-05";
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MMM-yy");
LocalDate localDate2 = dtf.parseLocalDate(date);
System.out.println(localDate2 + "/" + dtf.print(localDate2));
//second way
LocalDate localDate3 = LocalDate.parse(date, DateTimeFormat.forPattern("dd-MMM-yy"));
System.out.println(localDate3 + "/" + dtf.print(localDate3));
//third way
DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("dd-MMM-yy");
DateTime dateTime = FORMATTER.parseDateTime(date);
LocalDate localDate4 = dateTime.toLocalDate();
System.out.println(localDate4 + "/" + FORMATTER.print(localDate4));
Produced...
产...
2005-03-16/16-Mar-05
2005-03-16/16-Mar-05
2005-03-16/16-Mar-05
Before you get upset about this, this is how Java Date
works as well.
在您对此感到不安之前,这也是 Java 的Date
工作方式。