• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble formatting a Date based on the User's Locale

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble figuring out an easy way to format a date based on the current user's locale without manually passing in a format string. I realize you can do something like this:

DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );

However, this does not get me what I want because DateFormat.SHORT is basically M/d/yy. For the default English_US Locale, I want my dates to look like MM/dd/yyyy. As you can see by the code below, I created a little hack to get this, but I feel like there's got to be a better and cleaner way to do this...right? Also, another issue I'm running into is that when I'm logged in as a users' locale which uses '-' instead of '/' (i.e. 21-01-2009), I get a parse exception, thrown by the parseDate method below. Can anyone provide any input? Thanks.

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you can try java.text.SimpleDateFormat class.

This class allows us to format the Date into a certain pattern (i.e. yyyy-MM-dd HH:mm:ss) and also can parse the a String to Date with a pattern.

Please read the Java SE API. It will be helpful.

 
James McKee
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wei GENG wrote:Maybe you can try java.text.SimpleDateFormat class.

This class allows us to format the Date into a certain pattern (i.e. yyyy-MM-dd HH:mm:ss) and also can parse the a String to Date with a pattern.

Please read the Java SE API. It will be helpful.



I'm aware of that class, but I don't want to pass in a specific string to the SimpleDateFormat class. I want to know if there's a way JAVA can figure out the formatting for me, based on the user's locale.
 
James McKee
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any help would be appreciated
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll have to use SimpleDateFormat I'm afraid, unless the default Java format for a locale matches what you expect (which in your example it didn't.)
 
reply
    Bookmark Topic Watch Topic
  • New Topic