javajodatimedatetime-formattuxedo

Converting CCYYMMDDHHMMSSsss into java using joda time format


Hi I have Date time format with century I was trying to convert it into string using Joda date time format but I am getting junk data while converting it I am not sure why this is happening

Code

LocalDate date = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormat.forPattern("CCYYMMDDHHMMSSsss");
String str = date.toString(fmt);
System.out.println(str);

this is the output

20170103��0100���

but output should come like this

20160620091223711 

Solution

  • If you want the current date and time to be formatted, you should try this:

    DateTime dt = DateTime.now();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("CCYYMMddHHmmssSSS");
    String str = fmt.print(dt);
    System.out.println(str);
    

    Here, a DateTime object is used, the pattern is according to specification and the print method of DateTimeFormatter is used.