Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Problem in date format

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Problem in date format

Hello,
I need to format dates depending on the locale. These dates must be in digital format. DateFormat that almost meets my needs:
Code:
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
The problem is that it only takes 2 figures in account for the year and I need 4. Someone would have any idea please? Thank you.
Reply With Quote
  #2  
Old 19-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: Problem in date format

Hello,
I do not know what format DateFormat.short but you can SimpleDateFormat formatter as you like:
For example:
Code:
Code:
SimpleDateFormat s = new SimpleDateFormat("YYYYMMDD");
System.out.System.out.println(s.format(new Date()));
I hope this will help you.
Reply With Quote
  #3  
Old 19-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
Re: Problem in date format

Hello,
Your solution is good but I do not think this is what will help him, because you lose all the information moves on the local ... It will display the date in short format according to the usual local user, ie:

* dd / MM / yy for en_US
MM-dd-yy for en_US
etc. ...

This is possible through the method DateFormat.getDateInstance ()
Reply With Quote
  #4  
Old 19-02-2010
Member
 
Join Date: Nov 2009
Posts: 347
Re: Problem in date format

Hello,
One solution would be to change the pattern returned ... It seems that it is therefore an object SimpleDateFormat the following code should do the trick:
Code:
       DateFormat dtfr = DateFormat.getDateInstance(DateFormat.SHORT);
       if (dtfr instanceof SimpleDateFormat) {
           SimpleDateFormat sdtfr = (SimpleDateFormat) dtfr;
           String ptr = sdtfr.toLocalizedPattern();
           if (ptr.indexOf(yyyy)==-1) {
               dtfr = new SimpleDateFormat(ptr.replaceFirst("yy", yyyy));
           }
       }
If the returned object is not a SimpleDateFormat can not do anything.
Otherwise we create a new SimpleDateFormat with 4-digit year if it is not the case.
Reply With Quote
  #5  
Old 19-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Re: Problem in date format

Hello,
I have this temporary solution to almost exactly:
Instead of:
Code:
if (ptr.indexOf(yyyy)==-1) {
               df = new SimpleDateFormat(ptr.replaceFirst("yy", yyyy));
           }
Code:
df = new SimpleDateFormat(pattern.replaceFirst("y +", yyyy));
I thought there was a less circuitous way to achieve what I want.
Reply With Quote
  #6  
Old 19-02-2010
Member
 
Join Date: Nov 2009
Posts: 359
Re: Problem in date format

Hello,
Actually re-reading the post it is specified and said: "depending on the locale," I therefore advise every beginner in java to be sure to read before going to Java and especially before trying to do some example, I feel so because every programmer should have the basic knowledge strong , that it the basics of the language should be strong.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem in date format"
Thread Thread Starter Forum Replies Last Post
Using date format in JSP KAMANA Software Development 6 29-07-2010 11:32 AM
Format Date and Time Script in ASP Beter 2 Burn Out Software Development 4 10-02-2010 02:22 AM
Insert date format in SQL via ASP Bansi_WADIA Software Development 4 04-12-2009 10:56 PM
Changing the date format Uzair Customize Desktop 3 03-04-2009 01:44 PM
whenCreated Date Format Seagull Ng Active Directory 3 04-01-2007 08:44 AM


All times are GMT +5.5. The time now is 05:19 AM.