Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Format Quickie

 
Graham VMead
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm working on a system where the user wants to see dates as

DD:MM:YYYY

As there isn't a default pattern for this in DateFormat class I did the following


I've noticed that with an American US Locale 9th of April 2006 is shown as
09:04:2006 as per the pattern.

This is true to the pattern I asked for but "wrong" for US users. I wonder if there is a concise way for me to both format dates as DD:MM:YYYY AND also MM D:YYYY dependent on the Locale? e.g in UK its 09:04:2006 and 04:09:2006 in U.S ?

TIA Graham
 
Alana Sparx
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
couldn't you use the value of getCurrentLocale() to determine the format of the date. summat like
[code]
String format = null;
if(getCurrentLocale().equals("whatever US locale String looks like"))
{
format="MM.dd.yyyy";
}
else
{
format = "dd.MM.yyyy";
}

and then use the String 'format' when initialising the DateFormat class?
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a requirement that leads you into basic questions that don't have good answers (that's the case with your post) then that means the requirement was a bad idea.

I find that the ISO format "YYYY-MM-DD" works perfectly well for US users, and I expect it would be equally acceptable to users all over the world. It's obvious that the month comes before the day when you do it that way. So I would recommend you try that.
 
Alana Sparx
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

I find that the ISO format "YYYY-MM-DD" works perfectly well for US users, and I expect it would be equally acceptable to users all over the world

not so sure Europeans would like it much - I'm unwilling to accept I'm in a minority of one!


Cheers
As
 
Stefan Evans
Bartender
Pie
Posts: 1670
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way of doing it is having the dateformat string as a property in a resource bundle.
That way you can configure it for each locale, with a minimum of fuss.

However you do have to retrieve it from the bundle anytime you want to format a date...
[ July 17, 2006: Message edited by: Stefan Evans ]
 
Graham VMead
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies chaps, Stefan I like your suggestion and will try that.

I find that the ISO format "YYYY-MM-DD" works perfectly well for US users, and I expect it would be equally acceptable to users all over the world.


I know what your saying Paul but this assumes I control/have a say in what the customer wants and I'm afraid my customer is a large mega-corp that makes cars and whatever they say goes no questions asked even if its illogical
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic