Java 如何为文件io获取兼容的时间戳?

Java 如何为文件io获取兼容的时间戳?,java,android,date,file-io,timestamp,Java,Android,Date,File Io,Timestamp,我正在写一个方法,将文件保存到android上的SD卡上。我想将其作为“description\u code\u uniqueTimeStamp.png”格式 我尝试了几种日期和时间格式,但在尝试将文件写入无效格式时收到错误。可能由于:或 谁能为我提供一个唯一的时间戳解决方案(可以降到秒),它对文件名有效 谢谢为什么不获取时间戳字符串,然后通过一个编码器运行该字符串,以取出不允许的字符,例如 public static String encodeString(String string) {

我正在写一个方法,将文件保存到android上的SD卡上。我想将其作为“description\u code\u uniqueTimeStamp.png”格式

我尝试了几种日期和时间格式,但在尝试将文件写入无效格式时收到错误。可能由于:或

谁能为我提供一个唯一的时间戳解决方案(可以降到秒),它对文件名有效


谢谢

为什么不获取时间戳字符串,然后通过一个编码器运行该字符串,以取出不允许的字符,例如

public static String encodeString(String string) {
        if (string != null) {
            string.replace("&", "&");
            string.replace("<", "&lt;");
            string.replace(">", "&gt;");
            string.replace("\"", "&quot;");
            string.replace("'", "&apos;");
        }
        return string;
    }
公共静态字符串编码字符串(字符串){
if(字符串!=null){
字符串。替换(“&”、“&;”;
字符串。替换(“,”);
字符串。替换(“\”,“”);
字符串。替换(“”,“&apos;”);
}
返回字符串;
}
请澄清“唯一时间戳”(另请参见system.currenttimemillis)
final String prefix = "description_code_";
final String format = "yyyy-MM-dd'T'HH-mm-ss";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
String filename = prefix + sdf.format(new Date(0)));