-1

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 
henrycharles
  • 1,019
  • 6
  • 28
  • 66
  • 3
    I don't even use Joda time, and I can tell you that pattern string is nothing like correct. Review [the documentation](http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html). – T.J. Crowder Jan 03 '17 at 11:53
  • Yes, your pattern is completely wrong, see pattern description. For example, patterns are usually case-sensitive. And why do you think that hour, minute, second and fraction of second (what you want?!) have any meaning when your input is just a calendar date without time part? – Meno Hochschild Jan 03 '17 at 12:00
  • Also, `LocalDate.now()` returns the date as of now, I'm not sure why you're expecting the output you've given. – mohammedkhan Jan 03 '17 at 16:32

1 Answers1

0

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.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41