1

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.

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?

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;
    }
}
Gray
  • 115,027
  • 24
  • 293
  • 354
user149855
  • 111
  • 2
  • 2
  • 10

1 Answers1

5

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

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

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for your reply!yes, thats it I dont want this:2005-03-16 I want this: 16-Mar-05. but not printed I need to save it in a variable so I can return it. How can I do that? – user149855 Jul 10 '13 at 14:17
  • 1
    Use one of the formatters, as demonstrated in the code, to for at the Date to the String representation you need. If the database expects a Date object, then you have nothi to worry about, as the database will store the representation of the date in a format it needs and will return you a date object which you can then format as you need. – MadProgrammer Jul 10 '13 at 20:42