Results 1 to 6 of 6

Thread: Problem in date format

  1. #1
    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.

  2. #2
    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.

  3. #3
    Join Date
    Nov 2009
    Posts
    518

    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 ()

  4. #4
    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.

  5. #5
    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.

  6. #6
    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.

Similar Threads

  1. Using date format in JSP
    By KAMANA in forum Software Development
    Replies: 6
    Last Post: 29-07-2010, 10:32 AM
  2. Insert date format in SQL via ASP
    By Bansi_WADIA in forum Software Development
    Replies: 4
    Last Post: 04-12-2009, 10:56 PM
  3. Changing the date format
    By Uzair in forum Customize Desktop
    Replies: 3
    Last Post: 03-04-2009, 12:44 PM
  4. How to convert date format in postgres
    By Sayam in forum Software Development
    Replies: 3
    Last Post: 18-03-2009, 11:08 PM
  5. whenCreated Date Format
    By settler in forum Active Directory
    Replies: 3
    Last Post: 04-01-2007, 08:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,079,597.46876 seconds with 16 queries