aspose file tools*
The moose likes Java in General and the fly likes setting custom Date format globally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "setting custom Date format globally " Watch "setting custom Date format globally " New topic
Author

setting custom Date format globally

Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
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.


Regards,
Mahesh Kedari - Fidus Technologies Ltd.
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
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

Joined: Aug 16, 2005
Posts: 14435
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
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

Joined: Oct 27, 2005
Posts: 19790
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Thanks Rob for your inputs...
Its working fine....!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Can you share how you matched the DateFormatProvider to the Locale? I think I know how you did it, but I'm not sure.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting custom Date format globally