0

I got the following variables to begin with the conversion process of a date:

val format = "dd/MM/yyyy"
val sdf = SimpleDateFormat(format, Locale("pt", "PT"))

Until here everything ok. Then I got some values (val_1 and val_2) that correspond to dates(like this "01/01/2001" in string) picked up from editexts. (I don't think this is important to write, but gives the question some context). The problem is that I want the date to have first the day, then the month and finally the year, just like the format... but this is what android gives:

enter image description here

As you can see, it gives me first the month, then the day...and in this way, my program doesn't work at all. Whan can I do?

Pedro Relvas
  • 678
  • 7
  • 19
  • 1
    Exactly in which formate you want those dates `dateFromConvert` and `dateToConvert`? – DHAVAL A. Jul 24 '19 at 11:52
  • bro image that you attached is showing the value of dateFromConvert and dateToConvert which is correct, check the Log , Log will give you the right value which you have print. you need to store the value after formating is done then check that value – ChandraShekhar Kaushik Jul 24 '19 at 11:54
  • @DHAVALASODARIYA I want this "dd-MM-yyyy" and it gives me that big string with the month first... – Pedro Relvas Jul 24 '19 at 12:02
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 24 '19 at 15:11
  • Possible duplicate of [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format) – Ole V.V. Jul 24 '19 at 15:12
  • The string `Wed Jul 24 00:00:00 GMT+01:00 2019` is the result of `Date.toString()`. Apparently your debugger calls the `toString` method to get a string to show you (what else could it do?) It doesn’t matter. If you need to output the date to the user, format it back into a string. – Ole V.V. Jul 24 '19 at 15:21
  • In an attempt to understand your goal better (and not as a serious suggestion): If your string is correct, why not just use that instead of your `Date`? – Ole V.V. Jul 24 '19 at 15:39
  • Just replace `format.format(dateFromConvert)` with `sdf.format(dateFromConvert)` in both `println`. This will print the value you want. – DHAVAL A. Jul 25 '19 at 04:12

1 Answers1

0

Try with:

val sdf = DateFormat.format("dd-MM-yyyy", Locale("pt", "PT")).toString();

or:

val sdf = SimpleDateFormat(format, Locale("pt", "PT")).toString();
AndyNope
  • 427
  • 6
  • 12