Results 1 to 5 of 5

Thread: format date

  1. #1
    JAVAWE is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default format date

    hello
    can any help, how to get the right date from an String like this "2014-01-10T09:41:16.000+0000"

    my code is:
    Java Code:
            String strDate = "2014-01-10T09:41:16.000+0000";
            String day = "";
            String format = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
            Locale locale = new Locale("es", "ES");
            SimpleDateFormat formater = new SimpleDateFormat(format, locale);
            formater.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));
    
            Calendar cal = Calendar.getInstance();
    
            try {
                cal.setTimeInMillis(formater.parse(strDate).getTime());
                String offerDate = cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR);
                System.out.println(offerDate);
            } catch (Exception e){
                System.out.println(e.getMessage());
            }
    in the result i give something like this: "10-0-2014", i want the result like that "10-01-2014"

    thanks in advance :)

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,580
    Rep Power
    5

    Default Re: format date

    Well two small issues.

    - you're running into the Calendar Month Trap; it starts counting at January = month 0! You need to add 1 to the month that Calendar returns, but that would then format as "1" and not "01".
    - you need to print numbers that consist of a single digit as two digits, you can't blindly append them to the String

    BUT! You are already using a SimpleDateFormatter to PARSE the date, might you not also use a SimpleDateFormatter to FORMAT the date in the way you want?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: format date

    Simply set a new pattern of what you want and use the format method of the SimpleDateFormat class. It takes a Data object which can be returned by the SimpleDateFormat parse method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    JAVAWE is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: format date

    that it's solved by adding 1 to month :)

  5. #5
    JAVAWE is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Date Format
    By riddhishah28 in forum AWT / Swing
    Replies: 1
    Last Post: 02-19-2011, 03:46 PM
  2. Format date
    By mine0926 in forum New To Java
    Replies: 11
    Last Post: 08-04-2010, 01:56 AM
  3. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 12:17 PM
  4. How to change the format of date?
    By jboy in forum New To Java
    Replies: 1
    Last Post: 09-09-2009, 01:22 PM
  5. Date Format
    By learnspring in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 05:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •