The problem with String.format () is that you will be forced to use the indices of your variables in the format string, and a template similar to %1$ty-%1$tm-%1$td-%2$03d may be problematic for the user:
Calendar time = Calendar.getInstance(); int number = 7; String msg = String.format("Data-%1$ty-%1$tm-%1$td-%2$03d.xml", time, number);
In one of my projects, I decided to use Apache Velocity (here is an example: http://www.javaworld.com/javaworld/jw-12-2001/jw-1228-velocity.html ). This is a fairly powerful tool for this kind of tasks, but it makes it possible to use meaningful variable names in your format Date-${year}-${month}-${day}-${number}.xml . First, you need to add the appropriate variables to the speed context:
VelocityContext context = new VelocityContext(); context.put("number", "007"); context.put("year", ...);
but then it will be much easier for the user to specify a format string.
robertstrack
source share