0

Currently, I format the date like that:

DateFormat timeFormat = android.text.format.DateFormat.getDateFormat(
            MyApplication.getInstance().getApplicationContext());
String dateFormatted = timeFormat.format(dateTime.toDate());

The result is for example: "23-07-2016" (for french mobile); or "16-07-23" (for canadian mobile), etc.

In all cases, I want year is formatted on 2 digits: "23-07-2016" will become "23-07-16" "16-07-23" will stay the same ...

Ps: for information I use Yoda Date Time library.

How to do that please?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
anthony
  • 7,653
  • 8
  • 49
  • 101
  • 1
    is this of help? https://stackoverflow.com/questions/26102295/joda-time-datetime-formatting-based-on-locale – sschrass Oct 01 '16 at 15:50
  • With Joda-Time get the locale-specific short date pattern from `DateTimeFormat.patternForStyle("S-", Locale.getDefault())`. Use a regexp to replace any run of letters `y` with exactly `yy` to ensure two-digit year. Construct a formatter from `DateTimeFormat.forPattern()`. Format your date-time with it. Unfortunately the answers posted below use `DateFormat` and `SimpleDateFormat`, which is the last thing you should wish to do. – Ole V.V. Feb 11 '23 at 07:25

3 Answers3

0

Try using SimpleDateFormat, for example:

long date = <your UTC time in milliseconds>;
SimpleDateFormat formatter = new SimpleDateFormat ("d-MM-yyyy");
String s = formatter.format (date);

See API spec for details.

If by "android mobile format" you mean the date format which the user selected in android settings, you can get this value with

android.text.format.DateFormat.getDateFormat(context)

as you show in your sample code. After that, you'll probably need to parse it and substitute 4 digit year patterns with 2 digit year patterns and finish by using SimpleDateFormat as I show above.

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
0

OK I found a great solution. It's a workaround to modify directly the original pattern:

DateFormat timeFormat = DateFormat.getDateFormat(
            MyApplication.getInstance().getApplicationContext());

    if (timeFormat instanceof SimpleDateFormat) {
        String pattern = ((SimpleDateFormat) timeFormat).toPattern()
        // Change year format on 2 digits
        pattern = pattern.replaceAll("yyyy", "yy");            
        timeFormat = new SimpleDateFormat(pattern);
    }      

    return timeFormat.format(dateTime.toDate());

Thanks guys

anthony
  • 7,653
  • 8
  • 49
  • 101
0

I made a few adjustments:

public static String formatDate2DigitYear(@Nullable Date date) {
    if (date == null)
        return "";

    DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(App.get());

    if (dateFormat instanceof SimpleDateFormat) {
        String pattern = ((SimpleDateFormat) dateFormat).toPattern();

        if (pattern.matches("^[^y]*yyyy[^y]*$")) {
            pattern = pattern.replaceAll("yyyy", "yy");
        } else if (pattern.matches("^[^y]*y[^y]*$")) {
            pattern = pattern.replaceAll("y", "yy");
        }

        dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
    }

    return dateFormat.format(date);
}
Pavel Poley
  • 5,307
  • 4
  • 35
  • 66
  • The question informs us that the OP is using Joda-Time. So a answer using `SimpleDateFormat` and `Date` is a step backward. Those classes are poorly designed and long outdated. I recommend you don’t use them at all. – Ole V.V. Feb 11 '23 at 06:59
  • With Joda-Time get the locale-specific short date pattern from `DateTimeFormat.patternForStyle("S-", Locale.getDefault())`. Use a regexp to replace any run of letters `y` with exactly `yy` to ensure two-digit year the way you already do. Construct a formatter from `DateTimeFormat.forPattern()`. Format your date-time with it. – Ole V.V. Feb 11 '23 at 07:26