• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting custom Date format globally

 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is it possible to set custom date format globally in desktop application?
Just like we set Look and Feel only once and the same is followed automatically throughout the application.
 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone please help me on this. I am unable to find anything on this even on net.
Right now I am converting date format wherever Im using Date function in my application.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set the default Locale by calling Locale.setDefault(...) but that will affect all the language- and country-specific settings, not just the date format.

You can format dates by using the java.text.SimpleDateFormat class, which will enable to you specify how dates should be formatted in detail.
 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
Is it possible to set custom locale in which i can keep language as US english only and change the date format?
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly, but you need more than just the locale. I've been checking it out a bit, and you need at least the following:
- the Locale
- a sub class of java.text.spi.DateFormatProvider
- a way to combine the two

Unfortunately, the latter is located in class sun.util.LocaleServiceProviderPool of which the source is not in the src.zip file in the JDK folder. Here is the calling code from DateFormat.get(int, int int, Locale):
flags is 1 for time, 2 for date and 3 for date/time. timeStyle and dateStyle are one of DateFormat.FULL, LONG, MEDIUM, SHORT or DEFAULT. loc is the Locale. DateFormatGetter is a private nested class:
It essentially only calls one of the methods of a DateFormatProvider instance, but the mapping is still somewhere in the LocaleServiceProviderPool. Perhaps you can read how to connect them in the Javadoc of java.util.spi.LocaleServiceProvider.
 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for your inputs...
Its working fine....!!
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share how you matched the DateFormatProvider to the Locale? I think I know how you did it, but I'm not sure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic