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?

anthony
  • 7,349
  • 8
  • 44
  • 94
  • is this of help? https://stackoverflow.com/questions/26102295/joda-time-datetime-formatting-based-on-locale – sschrass Oct 01 '16 at 15:50

2 Answers2

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
  • 18,754
  • 17
  • 53
  • 91
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,349
  • 8
  • 44
  • 94