2011-03-09 120 views
26

我希望你能帮助我,我试图打电话给另一个班的日期,看起来像“2011-03-09 06-57-40”,我想用这个创建下面的文件,但每当我输出运行时它会创建一个新文件,因为它重新运行调用dat()。我知道发生了什么问题,我只是不知道如何解决这个问题,我想将它写入同一个文件。我希望这是有道理的? :/使用日期和时间创建文件名

感谢您的帮助提前:)

date d = new date(); 
    String cdate = d.date(); 


    String f = h; 

    try{ 
     PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(cdate + ".tsv", true))); 
     out.print(f); 
     out.print("\t"); 
     out.close(); 
    }catch (IOException e){ 
    } 
+0

你能澄清你到底想要做什么吗?我有两个想法:存储您使用的日期/文件名。或者你可以只使用他的日期,而不是日志文件或类似的东西。顺便说一句,这是Java?也许可以添加编程语言作为标记。 – Yashima 2011-03-09 07:08:44

+0

嗨,很抱歉,我不是很清楚,这是使用Java,我想创建一个名为今天的日期和时间的文件,所以它总是独一无二的,然后我想写入数据。这有帮助吗?非常感谢。 – Tom 2011-03-09 07:13:22

+0

@詹姆斯,它不..你能详细解释一下这个问题吗? – 2011-03-09 07:30:12

回答

9

我会尝试,并回答都是一样的。以最可控的方式可能使用获得的日期或时间字符串下面的代码

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

查找http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html。这可能有助于理解。您还可以添加小时/分钟或您需要格式化的字符串。

另一种选择可能是始终将日历中的毫秒,秒,分钟等“低”字段设置为零。

cal.set(Calendar.MINUTE,0); 

如果从另一个类检索您的日期,并不能直接创建一个日历,你也可以把日期到日历(注:只格式化你不需要日历)

cal.setTime(date); 

也许这有助于更好地控制创建的文件名/文件。

33

要创建一个名为当前日期/时间的文件:

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

你应该考虑的是,如果你在一个循环中写入多个文件,那么你可能会覆盖一些文件,因为时间是平等的。 – Khan 2018-02-28 10:31:23

3

这将只是多一点的效率 - 只有一个的SimpleDateFormat和Date对象,为每个文件,以及没有字符串连接。

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()); 
} 
16

这一个可能更容易。只有一行代码将文件的名称指定为日期和时间。

String out = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss'.tsv'").format(new Date()); 
1
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............"); 

     } 
}    
2

与此

public class TimeBasedFile { 
    public static void main(String[] args) { 


    Date date = new Date() ; 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ; 
    File file = new File("D:\\datebasedFile\\"+dateFormat.format(date) + ".tsv") ; 

    try(BufferedWriter out = new BufferedWriter(new FileWriter(file))) { 
     out.write("Writing to file"); 
     // code for what data you want to store 
     System.out.println("the file is created successfully"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
} 

它尝试创建的每个新文件,并每次执行。

相关问题