-5

How can i convert date of type dd.mm.yy to dd/mm/yy ?

i'm using Selenium + Java

Thanks

Lior p
  • 45
  • 6
  • 2
    You don't need Selenuim for date conversion, just read about `java.time.LocalDate` and `java.time.format.DateTimeFormatter`... – deHaar Oct 18 '21 at 07:19
  • I tried and i found many formats but i didn't find nothing about dd.mm.yy – Lior p Oct 18 '21 at 07:21
  • Why selenium is tagged ? It is job of binding language. – cruisepandey Oct 18 '21 at 08:39
  • 1
    Does this answer your question? [Going from MM/DD/YYYY to DD-MMM-YYYY in java](https://stackoverflow.com/questions/4169634/going-from-mm-dd-yyyy-to-dd-mmm-yyyy-in-java) – Masoud Keshavarz Oct 18 '21 at 12:10
  • Your usage of *type* confuses me, please use correct jargon for us to understand what you mean. The *type* is whether you've got a `String`, a modern `LocalDate` (recommended) or an outdated `Date` (not recommended). Whether your string follows dd/mm/yy or dd.mm.yy is called its *format*. – Ole V.V. Oct 18 '21 at 16:32

1 Answers1

3

If you are using java.util.Date :

SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("dd.MM.yy");
Date unformattedDate = dateTimeFormatter.parse("your date here");

dateTimeFormatter = new SimpleDateFormat("dd/MM/yy");
String formattedDate = dateTimeFormatter.format(unformattedDate);

This is a legacy package and the below code is recommended

If you are using java.time.*

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yy");
LocalDate unformattedDate = LocalDate.parse("your date here", dateTimeFormatter);

dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yy");
String formattedDate = dateTimeFormatter.format(unformattedDate);

And I would make the following change to your pattern. Instead of using y I would use u for the year since it also covers AD and BC.

Example: dd/MM/uu.

For more refer to this.

Renis1235
  • 4,116
  • 3
  • 15
  • 27
  • 4
    To all readers: Do not use the first example, it's practically legacy code. Only use it in case you are not able or allowed to use `java.time`. Second example is worth a +1, but might be improved by mentioning a difference between the pattern characters `y` and `u`. – deHaar Oct 18 '21 at 07:39
  • The second code throws an exception `java.time.format.DateTimeParseException: Text '18.10.21' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2021, DayOfMonth=18, MinuteOfHour=10},ISO of type java.time.format.Parsed` –  Oct 18 '21 at 07:42
  • 1
    @saka1029 change `mm` to `MM`. `mm` is used for minutes. `MM` is used for Months. – Renis1235 Oct 18 '21 at 08:03
  • 1
    @deHaar thank you for your comment. What do you mean by `y` and `u`? – Renis1235 Oct 18 '21 at 08:07
  • 2
    @Renis1235 - It is very important for you to understand what deHaar has mentioned. Check [this](https://stackoverflow.com/a/65928023/10819573) to learn about it. – Arvind Kumar Avinash Oct 18 '21 at 08:09
  • 2
    @ArvindKumarAvinash thank you! I updated the answer and learned something as well. – Renis1235 Oct 18 '21 at 08:16