-3

How do I convert date format from

dd-mm-yyyy

to

YYMMDD

in java?

P.S
  • 113
  • 1
  • 1
  • 9
  • 2
    Hi! Please take the [tour] (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Sep 20 '18 at 06:25
  • For many purposes you shouldn’t just convert from one string format to another. Store your date as a `LocalDate`. If you receive a string, parse it into a `LocalDate` first. Only when you need to let a string go out of your system (to the user, a text file, another system). format into one. – Ole V.V. Sep 20 '18 at 06:44
  • To parse: `LocalDate.parse("29-11-2018", DateTimeFormatter.ofPattern("dd-MM-uuuu"))`. To format: `yourLocalDate.format(DateTimeFormatter.ofPattern("uuMMdd"))`. Note that the format pattern strings are case sensitive: you need uppercase `M` and lowercase `u` and `d`. – Ole V.V. Sep 20 '18 at 06:47
  • Usually I’d say to search before asking. Exactly when it comes to date formats in Java, you’ll be likely to hit old pages that show you to use `SimpleDateFormat`, which is bad advice these days, though. So just this once you’re excused for not showing a search effort. :-) See if you can’t find useful inspiration here: [Java 8 – How to convert String to LocalDate and LocalDate to String in specific format](https://www.javabrahman.com/quick-tips/java-8-how-to-convert-string-to-localdate-and-localdate-to-string-in-specific-format/). – Ole V.V. Sep 20 '18 at 06:51

1 Answers1

0

I think you are seeking the solution like date formatting. You can try to use DateTimeFormatter with your own pattern.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd");
RogerSK
  • 393
  • 1
  • 18