views:

89

answers:

4

I have a spring web application that runs in Tomcat. I must set the date and number format for my application to a special format.

Can I set the format in any descriptor to the special in my application or I can set the all system format only?

A: 

java.util.Date objects do not have a format by themselves (they only represent the date and time value, just like integers only represent a number value and don't know anything about formatting numbers).

There is no system-wide default date format setting. When you print a Date object by (implicitly or explicitly) calling toString() on it, it will be printed with a fixed, default format that you can't change:

System.out.println(new Date());
// Example output: Wed Aug 04 09:46:57 CEST 2010

If you want to show a date with a specific format, use a java.text.DateFormat object to format it. For example:

DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(df.format(new Date()));
// Example output: 04-08-2010 09:48:47
Jesper
"There is no system-wide default date format setting." - not really true. The getXXInstance() methods of DateFormat are a kind of system-wide locale-dependant default.
Michael Borgwardt
Ok, but you cannot change that setting, there are no `setXXInstance()` methods in class `DateFormat`.
Jesper
A: 

You can absolutely format a date any way you want, no matter if you use Spring or not. First though make sure that you really need a "special formatting", not just a format that is default for a certain locale (like deCH or svSE or enGB) because if you just want to convert a date to your countries native date formatting you can simply use

String currentDate = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date());

And if you really want a custom formatting, you can do like this

String currentDateMyFormat = new SimpleDateFormat("dd:MMM:yy HH.mm").format(new Date());

Of course you can reverse the process (from String to Date) by replacing format with parse.

Hope this answers your question because just like Xu before me, I am not sure I understood your question completely ;)

sicn
A: 

I want to use this pattern: yyyy.mm.dd.

This code is wrong because it's not a standard locale pattern:

String currentDate = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date());

But I don't want type the pattern everywhere in the application, I want set the pattern once.

I want if I type this code:

String currentDate = SimpleDateFormat.getDateInstance(DateFormat.SHORT).format(new Date());

The result is: 2010.08.04.

Is it possible?

A: 

Since you're talking about doing something application-wide, let me remind you to be cautious with any java.text.Format subclass (including DateFormat, SimpleDateFormat, etc.). As noted in the Javadoc, Formats are generally not thread-safe. I know from personal experience that DateFormats are not.

Therefore, if you're considering setting something up for use across your application, I recommend only defining the format String and sharing it around the application. Instances of a Format class are best-defined in a thread-safe manner, such as on the stack of a method call.

RonU