Create file name using date and time

后端 未结 5 1809
清酒与你
清酒与你 2020-12-14 00:31

I hope you could help me, I\'m trying to call in the date from another class and looks like \"2011-03-09 06-57-40\", I want to use this to create the file below but everytim

相关标签:
5条回答
  • 2020-12-14 00:56

    To create a file named the current date/time:

    Date date = new Date() ;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;
    File file = new File(dateFormat.format(date) + ".tsv") ;
    BufferedWriter out = new BufferedWriter(new FileWriter(file));
    out.write("Writing to file");
    out.close();
    
    0 讨论(0)
  • 2020-12-14 01:08
    public class BELogs {
    
        private final static Logger logger = Logger.getLogger(BSELogs.class
                .getName()); 
    
                 boolean makeDir = false;
                 Date date = new Date();
                 SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy") ;
                 String curDate =dateFormat.format(date);
                 FileHandler fh;     
    
                   public BSELogs()  {
    
             try {  
    
                 File file = new File("/home//Desktop/Logs "+curDate);
                 makeDir = file.mkdir();
                 fh = new FileHandler(file+"/MyLogFile.log "+curDate,true);  
                 logger.addHandler(fh);
                 // Set the logger level to produce logs at this level and above.
                 logger.setLevel(Level.FINE);
                 SimpleFormatter formatter = new SimpleFormatter();  
                 fh.setFormatter(formatter);  
    
              } catch (SecurityException ex) {  **strong text**
                 ex.printStackTrace();  
              } catch (IOException e) {  
                   e.printStackTrace();  
               }  
    
        logger.info("Data Log Genrated............"); 
    
          }
    }             
    
    0 讨论(0)
  • 2020-12-14 01:12

    I'll try and answer all the same. To obtain a date or time string in the most controlled manner possible use the following code

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String dateStr = dateFormat.format(cal.getTime());
    

    Look up http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html . It might help with understanding. You can also add hour/minute or what you need to the formatted String.

    Another option might be to always set the "lower" fields like milliseconds, seconds, minutes in the Calendar to zero.

    cal.set(Calendar.MINUTE,0);
    

    If you are retrieving your date from another class and cannot directly create a calendar you can also put the date into the calendar (note: for just formatting you don't need the calendar)

    cal.setTime(date);
    

    Maybe this helps getting more control of the created filename / file.

    0 讨论(0)
  • 2020-12-14 01:15

    This one probably much easier. Only one line of a code to assign the name of the file as date and time.

    String out = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss'.tsv'").format(new Date());
    
    0 讨论(0)
  • 2020-12-14 01:20

    It will be just a bit more efficient - only one SimpleDateFormat and Date object for each file, as well as no String concatenation.

    private  final static String getDateTime()  
    {  
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");  
        df.setTimeZone(TimeZone.getTimeZone("PST"));  
        return df.format(new Date());  
    }  
    
    0 讨论(0)
提交回复
热议问题