字符串到joda LocalDate,格式为“dd-MMM-yy” [英] String to joda LocalDate in format of "dd-MMM-yy"

查看:656
本文介绍了字符串到joda LocalDate,格式为“dd-MMM-yy”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB和joda时间2.2。将数据从Mysql备份到XML并将其还原。在我的表中,我有一个Date属性,格式为16-Mar-05。我成功地将其存储在XML中。但是当我想从XML读取并将其放回Mysql表中时,我无法获得正确的格式。

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.

这是我的XMLAdapter类,这里是unmarshal方法input String是16-Mar-05,但我不能以16-Mar-05的格式获取localDate变量,尽管我将模式设置为dd-MMM-yy。我发布了我尝试的所有选项,如何在16d-05-05格式的dd-MMM-yy中获取我的localDate?

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?

谢谢!!

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;
    }
}


推荐答案

所以我拿了你的代码并运行它,它对我来说很好......

So I took your code and ran it and it works fine for me...

问题,我认为,你所拥有的是你期待 LocalDate 用于维护原始解析对象的格式的对象,这不是 LocalDate 的工作方式。

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 is a representation of date or period in time, it is not a format.

LocalDate 有一个 toString 方法,可用于转储对象的值,它,这是对象用来提供人类可读表示的内部格式。

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.

要格式化日期,您需要使用某种格式化程序,这将采用该模式你想要和日期值并返回字符串

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

例如,以下代码...

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));

生成...

2005-03-16/16-Mar-05
2005-03-16/16-Mar-05
2005-03-16/16-Mar-05

在您对此感到不安之前,这就是Java Date 也可以。

Before you get upset about this, this is how Java Date works as well.

这篇关于字符串到joda LocalDate,格式为“dd-MMM-yy”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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