在android中创建文件名Date

时间:2015-04-21 22:38:57

标签: android file

我有一个备份数据库的应用程序:

// Method To Backup Database//
public void OnClick_Backup(View v) {

    // Vibrates For 50 Mill//
    vibe.vibrate(50);

    int a = Calendar.DAY_OF_YEAR;

    int b = Calendar.DAY_OF_MONTH;

    File sd = new File(Environment.getExternalStorageDirectory()+"/C.S. Tracker Backups");
    if(!sd.exists()){
        sd.mkdirs();
    }
        try {
            File sd2 = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd2.canWrite()) {
                String currentDBPath = "//data//jordanzimmittidevelopers.com.communityservicelogger//databases//community_service_Database";
                String backupDBPath = "/C.S. Tracker Backups/C.S. Backup";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd2, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                    Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Exception ignored) {
        }
    }

这里的代码告诉我们数据库文件应该是什么路径:

   String backupDBPath = "/C.S. Tracker Backups/C.S. Backup";

如何让C.S. Backup更改为备份时的日期。例如,假设他们现在点击备份。我希望它说4-21-15。 Thaks

1 个答案:

答案 0 :(得分:2)

您可以使用SimpleDateFormat类来获得所需的结果。

SimpleDateFormat mFormatter = new SimpleDateFormat("MM-dd-yy");

Calendar c = Calendar.getInstance();
String backupDBPath =  mFormatter.format(c.getTime());