3

In my application I want store a file into SD card.
What I would like is that when the app stores this file, add the now date as filename and for this I wrote the below codes.

But when users change the device language to UTF-8 languages (such as : Farsi - Arabic , ...) the file is stored with UTF-8 characters!

For example : store file with this name : COM_MyVideo_۲۰۲۰-۱-۵
but i want store with this name : COM_MyVideo_2020-1-5

My codes:

private String getFileSaveName() {
    videoName = GoodPrefs.getInstance().getString(ConstKeys.TEST_NAME, "MyApp");
    videoName = videoName + "-" + GoodPrefs.getInstance().getInt(ConstKeys.TEST_ID, 0) + "-";
    String filename = "yyyyMMdd_HHmmss";
    //Required to handle preference change
    filename = filename.replace("hh", "HH");
    String prefix = "COM_" + videoName;
    Date today = Calendar.getInstance().getTime();
    SimpleDateFormat formatter = new SimpleDateFormat(filename);
    return prefix + formatter.format(today);
}

How can I fix it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
DJ Al
  • 283
  • 1
  • 4
  • 10
  • https://stackoverflow.com/questions/23612392/arabic-date-on-android-app – Prasanth S Jan 14 '20 at 07:31
  • 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. Jan 16 '20 at 05:55

1 Answers1

2

try his , first create the current datetime as a timestamp, after convert the timestamp into a local english date, and format it as you need.

 Date date= new Date();

 long time = date.getTime();

 Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time * 1000);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
MoxGeek
  • 458
  • 6
  • 17