Create file name using date and time
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 everytime I do when the output runs it creates a new file as it re-runs calling the dat(). I know what's going wrong I'm just not sure how to fix it, I want to permently writw to the same file. I hope this makes sense? :/
Thank you for any help in advance :)
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){
}
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();
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());
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.
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());
}
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............");
}
}
精彩评论