1

I have an application where I want to change the date format in 'From' and 'To'.

Lets assume, I have a date range like 2018-06-11 - 2018-06-14. Now, I want to change it in some other format as 11 June, 2018 - 14 June, 2018.

Please refer the code below, I have tried:

        String strDate = "2018-06-11 - 2018-06-14";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(strDate);
            format = new SimpleDateFormat("dd MMMM, yyyy");
            String strFinalDate = format.format(date);
            tv4.setText(strFinalDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

Also, I tried the below code as well:

        String strDate = "2018-06-11 - 2018-06-14";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd - yyyy-MM-dd");
        try {
            Date date = format.parse(strDate);
            format = new SimpleDateFormat("dd MMMM, yyyy - dd MMMM, yyyy");
            String strFinalDate = format.format(date);
            tv4.setText(strFinalDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

First, set of code runs smoothly, but converts only the 'From' and doesn't even checks for 'To' date. And, second set of code returns an error which says, java.text.ParseException: Unparseable date: "2017-01-11 - 2017-01-14

Have I skipped anything in this code?

Please notify me if I have.

Thanks!

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Neha Beniwal
  • 623
  • 1
  • 6
  • 12
  • these packages are deprecated try date and time – Lokesh Pandey Jun 11 '18 at 09:45
  • @Lokesh I have to use this.. – Neha Beniwal Jun 11 '18 at 09:48
  • I will say take date from `String strDate = "2018-06-11 - 2018-06-14";` and split the dates using `-` and then change the format and then append it using `-` – Lokesh Pandey Jun 11 '18 at 09:50
  • 2
    Split `String strDate = "2018-06-11 - 2018-06-14";` into two different `String` variables e.g. `String fromDate = "2018-06-11";` and `String toDate = 2018-06-14";` Then try to parse and format these two strings. – pleft Jun 11 '18 at 09:51
  • Thanks mate! it worked!! – Neha Beniwal Jun 11 '18 at 10:02
  • 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. Jun 11 '18 at 10:17
  • just looking at the example code, what actually did you expect to have into the date object: `Date date = format.parse("2018-06-11 - 2018-06-14");` ?? – mihail Jun 11 '18 at 14:42

2 Answers2

2

I suggest

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
    String strDate = "2018-06-11 - 2018-06-14";
    String[] strDates = strDate.split(" - ");
    if (strDates.length != 2) {
        throw new IllegalStateException("Wrong range format " + strDate 
                + ", must be yyyy-MM-dd - yyyy-MM-dd");
    }
    String strFinalDate = LocalDate.parse(strDates[0]).format(dateFormatter)
            + " - " + LocalDate.parse(strDates[1]).format(dateFormatter);
    System.out.println(strFinalDate);

Output on my Java with US English locale:

11 June 2018 - 14 June 2018

You don’t get the comma after “June” because this is not considered standard, so consider if this isn’t really an advantage. The output will depend on locale setting, which may be an advantage too. Or specify explicit locale for example:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
            .withLocale(Locale.UK);

I am using and recommending java.time, the modern Java date and time API.

What went wrong in your code?

No matter if you use an old-fashiuoned Date or a modern LocalDate, such an object can only hold one date, not a date range. In your first attempt SimpleDateFormat parsed the first date (since it matched your pattern) and then ignored the remainder of the string. So when you formatted the parsed date, you got

11 June, 2018

In your seconds attempt both dates were parsed, but into the same Date object, so only the values of the second date were kept. When printing the date its values were also printed twice since the format pattern for formatting contains the format twice:

14 June, 2018 - 14 June, 2018

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

As I stated in my comment above, you have to split your strDate String into two different String variables. Have a look at the code below

    String strDate = "2018-06-11 - 2018-06-14";
    String[] fromToDatesStr = strDate.split(" - ");
    String fromDateStr = fromToDatesStr[0];
    String toDateStr = fromToDatesStr[1];
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date fromDate = null;
    Date toDate = null;
    try {
        fromDate = format.parse(fromDateStr);
        toDate = format.parse(toDateStr);
        format = new SimpleDateFormat("dd MMMM, yyyy");
        String strFromFinalDate = format.format(fromDate);
        String strToFinalDate = format.format(toDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

String strFromFinalDate and String strToFinalDate now contain the dates formatted as you desire.

pleft
  • 7,567
  • 2
  • 21
  • 45
  • That's what I wrote in my comment before you. But why don't you let OP try ? – Lokesh Pandey Jun 11 '18 at 09:59
  • @Lokesh That's what I also wrote into my comment (posted almost the same time as yours). However, I don't see any reason why not to answer the OP question. – pleft Jun 11 '18 at 10:01
  • You have all the rights to answer but I believe the difference in giving logic and giving solution – Lokesh Pandey Jun 11 '18 at 10:03
  • If the given logic solution didn't worked then you would have given your code – Lokesh Pandey Jun 11 '18 at 10:03
  • If I were a teacher and the OP was my student I would agree with you. If my answer is against SO rules please flag it and moderators will take care of it. Thanks – pleft Jun 11 '18 at 10:10
  • I am not against your answer. I am just saying let the OP try. Because you have already written this in your comment and then few minutes later an answer. I don't see any point of that comment. – Lokesh Pandey Jun 11 '18 at 10:13