Search code examples
javaandroidandroid-date

How to store file with date into SD card?


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?


Solution

  • 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();