java USES the time format to generate a unique filename

  • 2020-05-30 20:19:13
  • OfStack

preface

Sometimes we need to take a screenshot, when we want to take a screenshot, someone used the time format, but in the time format: is not allowed in the file name of the characters, so it will report an error, how to generate only 1 time filename:

The sample code


package com.demo;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
public class TimeString {
 
  private String valueOfString(String str, int len) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < len - str.length(); i++) {
      sb.append("0");
    }
    return (sb.length() == 0) ? (str) : (sb.toString() + str);
  }
   
  public String getDateFormat(){
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return df.format(new Date());    
  } 
   
  public Date getDateFormat(String time){
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      return df.parse(time);
    } catch (ParseException e) {      
      e.printStackTrace();
    } 
    return null;
  }
 
  private String getTimeString(Calendar calendar) {   
    StringBuffer sb = new StringBuffer();
    sb.append(String.valueOf(calendar.get(Calendar.YEAR)))   
     .append(this.valueOfString(String.valueOf(calendar.get(Calendar.MONTH) + 1),2))
     .append(this.valueOfString(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)),2))
     .append(this.valueOfString(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)),2))
     .append(this.valueOfString(String.valueOf(calendar.get(Calendar.MINUTE)),2))
     .append(this.valueOfString(String.valueOf(calendar.get(Calendar.SECOND)),2))
     .append(this.valueOfString(String.valueOf(calendar.get(Calendar.MILLISECOND)),3));    
    return sb.toString();
  } 
   
  public String getTimeString(String time){
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(this.getDateFormat(time));
    return this.getTimeString(calendar);
  }
   
  public String getTimeString(){
    Calendar calendar = new GregorianCalendar();
    return this.getTimeString(calendar);
  }
   
  public static void main(String[] args) {
    TimeString ts = new TimeString();
    System.out.println(ts.getTimeString());
    System.out.println(ts.getTimeString("2015-4-30 8:51:52"));
  }
}

Although the getTimeString method above is accurate to milliseconds, it is still possible to generate the same file name, resulting in overwriting, so when generating the file name, check whether 1 exists:


package com.demo;
 
import java.io.File;
 
public class Test16 {
   
  public String getFileName(String path){
    File file = new File(path);
    if(file.exists()){
      return this.getFileName(file.getParent()+File.separator+(new TimeString().getTimeString())+".png");
    }
    return path;
  }
   
  public static void main(String[] args) {
    Test16 t = new Test16();
    System.out.println(t.getFileName("D:/1.txt"));
  }
 
}

In theory, this is also possible to produce the same file name, this is possible in multi-threading, the solution is very simple, their own brain to think about to go!

conclusion

The above is all about the time format in JAVA to generate only 1 file name, I hope the content of this article can bring you a definite help in your study or work, if you have any questions, you can leave a message to communicate.


Related articles: