Home arrow static arrow Java Programming [Archive] - date format question
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - date format question
This topic has 66 replies on 5 pages.    1 | 2 | 3 | 4 | 5 | Next »

Posts:16
Registered: 7/26/04
date format question  
Aug 5, 2004 7:56 AM



 
Hi All,

If anyone could shed some light on this topic, that would be wonderful.

Basically, I'm using the local date instance and formatting a date to short. I'm doing this so that everyone can see the dates in their native format.

This works fantastic - BUT - for consistency's sake, I need to display a 4-digit year, not a 2-digit year.

Does anyone know how to display the date in the users native format, while forcing a 4-digit year instead of a 2 digit?

Example:
DateFormat.getDateInstance(DateFormat.SHORT).format(sdf.parse(dataCls.getPValue("DATE_VAL")))

Returns: in the US MM/DD/YY, in the UK DD/MM/YY, in brazil..... blah blah
I need it to return: in the US MM/DD/YYYY, in the UK DD/MM/YYYY, etc...

Thanks in advance,
Lisa
 

Posts:24,036
Registered: 2/3/03
Re: date format question  
Aug 5, 2004 7:58 AM (reply 1 of 66)



 
[url=http://www.javaalmanac.com/egs/java.text/FormatDate.html]Formatting a Date Using a Custom Format[/url]
[url=http://www.javaalmanac.com/egs/java.text/ParseDate.html]Parsing a Date Using a Custom Format[/url]
 

Posts:357
Registered: 8/5/04
Re: date format question  
Aug 5, 2004 8:03 AM (reply 2 of 66)



 
could you please send me an email at xterminator890i@hotmail.com (yaw)

i would like to begin from there
 

Posts:24,036
Registered: 2/3/03
Re: date format question  
Aug 5, 2004 8:03 AM (reply 3 of 66)



 
Having re-read your OP, I think those links may not help...
 

Posts:24,036
Registered: 2/3/03
Re: date format question  
Aug 5, 2004 8:04 AM (reply 4 of 66)



 
could you please send me an email at
xterminator890i@hotmail.com (yaw)

??


i would like to begin from there

What does me sending you an email have to do with "beginning from there"?
 

Posts:319
Registered: 11/8/00
Re: date format question  
Aug 5, 2004 8:07 AM (reply 5 of 66)



 
could you please send me an email at
xterminator890i@hotmail.com (yaw)

??


i would like to begin from there

What does me sending you an email have to do with
"beginning from there"?

sounds like you've pulled.

please don't ask for replys by email it stops others learning and also stops others reviewing code that is posted.
 

Posts:357
Registered: 8/5/04
Re: date format question  
Aug 5, 2004 8:10 AM (reply 6 of 66)



 
for concealment purpose

i agree but please take this one as an exception
 

Posts:31,095
Registered: 4/30/99
Re: date format question  
Aug 5, 2004 8:12 AM (reply 7 of 66)



 
Try this:
SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT);String pat = sdf.toLocalizedPattern();int pos = pat.indexOf("yy");if (pos >= 0) pat = pat.substring(0, pos) + "yy" + pat.substring(pos);sdf.applyLocalizedPattern(pat);System.out.println(sdf.format(new Date()));
It works in my locale, but test it in others. By the way the API docs for DateFormat say the cast to SimpleDateFormat may not work in rare cases, so catch any ClassCastException on that line and skip the rest of the code in that case.

PC²
 

Posts:7,258
Registered: 1/31/02
Re: date format question  
Aug 5, 2004 8:16 AM (reply 8 of 66)



 
To the OP (who isn't the id that's asking for some sort of personal relationship with yawmark - which is both sweet and a little creepy) -

If the MEDIUM format doesn't work for you (where you get your 4 digit year, but lose the numeric month/date) then you'll likely have to do some sort of locale-specific formatting decisions with SimpleDateFormat, because I don't see a way to add your own styles to the DateFormat class (though I didn't look terribly hard)

Good Luck

Lee
 

Posts:357
Registered: 8/5/04
Re: date format question  
Aug 5, 2004 8:17 AM (reply 9 of 66)



 
OMG this hotmail email is BOMBARDING me with >> date lavalife match .... I am getting "CRAZY"
 

Posts:37,103
Registered: 3/30/99
Re: date format question  
Aug 5, 2004 8:19 AM (reply 10 of 66)



 
for concealment purpose

i agree but please take this one as an exception

No. There's nothing secret or proprietary about what yawmark or anybody else would discuss with you about formatting a date. And if there is, then you should enter into a legally binding contract, and will probably have to pay the person who's doing the work.

I'm not sure I understand you question, but if I do, you want to use the default format for a locale, BUT if that involves a 2-digit year you want to change it to a 4-digit year.

Sounds like the steps are:

1) obtain the default format string (look into Locale, DateFormat, etc.)

2) use regex to change "MM" to "MMMM"

3) create a new DateFormat from that modified string

You can't be sure that a given default format will even use digits though. It might use month names: Jan or January, etc. What do you wish to do in this case?
 

Posts:37,103
Registered: 3/30/99
Re: date format question  
Aug 5, 2004 8:20 AM (reply 11 of 66)



 
To the OP (who isn't the id that's asking for some
sort of personal relationship with yawmark

Ahhh... I see now. Oops.

- which is
both sweet and a little creepy) -

Indeed. Even more so since it just popped out of the blue and isn't the OP.
 

Posts:31,095
Registered: 4/30/99
Re: date format question  
Aug 5, 2004 8:21 AM (reply 12 of 66)



 
Sounds like the steps are:

1) obtain the default format string (look into
Locale, DateFormat, etc.)

2) use regex to change "MM" to "MMMM"

3) create a new DateFormat from that modified string

Right. The code in Reply 7 is a working example of that.
 

Posts:24,036
Registered: 2/3/03
Re: date format question  
Aug 5, 2004 8:26 AM (reply 13 of 66)



 
for concealment purpose

What are you trying to conceal?

i agree but please take this one as an exception

As mentioned:

Don't ask people to reply by private email. Most Forum regulars believe solving problems should be a public, transparent process during which a first try at an answer can and should be corrected if someone more knowledgeable notices that it is incomplete or incorrect. Also, they get some of their reward for being respondents from being seen to be competent and knowledgeable by their peers (not to mention the possibility of collecting some of those precious [url=http://forum.java.sun.com/rewardFaq.jsp]Duke Dollars[/url]).
 

Posts:357
Registered: 8/5/04
Re: date format question  
Aug 5, 2004 8:31 AM (reply 14 of 66)



 
Try it & see what the topic is all about.
 
This topic has 66 replies on 5 pages.    1 | 2 | 3 | 4 | 5 | Next »