This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Nov 25, 2008 5:17 AM by masijade RSS

Convert date to the format 25/11/2008

796220 Newbie
Currently Being Moderated
How can I convert a date in the format
Tue Nov 25 10:11:45 GMT+05:30 2008
to
25/11/2008
Please help.
Any help in this regard will be well appreciated with dukes.
  • 1. Re: Convert date to the format 25/11/2008
    PhHein Guru Moderator
    Currently Being Moderated
    Use two instances of SimpleDateFormat. Parse the first to Date and then format it.
  • 2. Re: Convert date to the format 25/11/2008
    807589 Newbie
    Currently Being Moderated
    You don't "convert" dates.

    You use SimpleDateFormat to display the output as you like.

    [Calculating Java dates: Take the time to learn how to create and use dates|http://www.javaworld.com/jw-12-2000/jw-1229-dates.html|With code samples!]
    [Formatting a Date Using a Custom Format|http://www.javaalmanac.com/egs/java.text/FormatDate.html|With more code samples! Because I care.]
    [Parsing a Date Using a Custom Format|http://www.javaalmanac.com/egs/java.text/ParseDate.html|I mean did PhHein give you code samples? No he did not ;p]
  • 3. Re: Convert date to the format 25/11/2008
    796220 Newbie
    Currently Being Moderated
    I tried this
    DateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
            try {
                   Date date = (Date)formatter.parse("Tue Nov 25 10:11:45 GMT+05:30 2008");
                   System.out.println(date);
    and got the exception
    java.text.ParseException: Unparseable date: "Tue Nov 25 10:11:45 GMT+05:30 2008"
         at java.text.DateFormat.parse(Unknown Source)
         at booodrive.Test.main(Test.java:12)
    Please help
  • 4. Re: Convert date to the format 25/11/2008
    791266 Explorer
    Currently Being Moderated
    Do you think that:
    "E, dd MMM yyyy HH:mm:ss Z"
    Matches the format:
    "Tue Nov 25 10:11:45 GMT+05:30 2008"
    Why do you think so if you do think that?

    Kaj
  • 5. Re: Convert date to the format 25/11/2008
    807589 Newbie
    Currently Being Moderated
    AneesAhamed wrote:
    I tried this
    DateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
    try {
                   Date date = (Date)formatter.parse("Tue Nov 25 10:11:45 GMT+05:30 2008");
                   System.out.println(date);
    and got the exception
    java.text.ParseException: Unparseable date: "Tue Nov 25 10:11:45 GMT+05:30 2008"
         at java.text.DateFormat.parse(Unknown Source)
         at booodrive.Test.main(Test.java:12)
    Excellent. As you can see the parsing method is working correctly then as it is using the format you have which bears really no resemblence to the String you tried to parse.
  • 6. Re: Convert date to the format 25/11/2008
    807589 Newbie
    Currently Being Moderated
    cotton.m wrote:
    Excellent. As you can see the parsing method is working correctly then as it is using the format you have which bears really no resemblence to the String you tried to parse.
    As opposed to a pattern like "EEE MMM dd HH:mm:ss ZZZZ yyyy"
  • 7. Re: Convert date to the format 25/11/2008
    796220 Newbie
    Currently Being Moderated
    I got it correct with the code given
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
            try {
                   Date date = (Date)formatter.parse("Tue Nov 25 10:11:45 GMT+05:30 2008");
                   System.out.println(date);
    Now how to convert to
    25/11/2008
    Please help.
  • 8. Re: Convert date to the format 25/11/2008
    807589 Newbie
    Currently Being Moderated
    AneesAhamed wrote:
    I got it correct with the code given
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
    try {
                   Date date = (Date)formatter.parse("Tue Nov 25 10:11:45 GMT+05:30 2008");
                   System.out.println(date);
    Now how to convert to
    25/11/2008
    Please help.
    Try. Harder.

    I mean this is not actually the dump your work and have cotton do it for you forum. That's not actually a forum anywhere, mostly.

    Look up the API for SimpleDateFormat and figure out what your new pattern is.
  • 9. Re: Convert date to the format 25/11/2008
    PhHein Guru Moderator
    Currently Being Moderated
    AneesAhamed wrote:
    I got it correct with the code given
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
    try {
                   Date date = (Date)formatter.parse("Tue Nov 25 10:11:45 GMT+05:30 2008");
                   System.out.println(date);
    Now how to convert to
    25/11/2008
    Please help.
    Reread replies 1 and 2!
  • 10. Re: Convert date to the format 25/11/2008
    masijade Explorer
    Currently Being Moderated
    AneesAhamed wrote:
    I got it correct with the code given
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
    try {
                   Date date = (Date)formatter.parse("Tue Nov 25 10:11:45 GMT+05:30 2008");
                   System.out.println(date);
    Now how to convert to
    25/11/2008
    Please help.
    Petition God (whichever diety you worship) for initiative and the capacity to use it.
  • 11. Re: Convert date to the format 25/11/2008
    807589 Newbie
    Currently Being Moderated
    masijade. wrote:
    AneesAhamed wrote:
    Please help.
    Petition God (whichever diety you worship) for initiative and the capacity to use it.
    I already said no. ;p
  • 12. Re: Convert date to the format 25/11/2008
    masijade Explorer
    Currently Being Moderated
    cotton.m wrote:
    masijade. wrote:
    AneesAhamed wrote:
    Please help.
    Petition God (whichever diety you worship) for initiative and the capacity to use it.
    I already said no. ;p
    The All Unmerciful Cotton!
  • 13. Re: Convert date to the format 25/11/2008
    796220 Newbie
    Currently Being Moderated
    Solved
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
                                  SimpleDateFormat parse = new SimpleDateFormat("dd/MM/yyyy");
                                  
                                  Date date = null;
                                  try {
                                       date = (Date)formatter.parse("Tue Nov 25 10:11:45 GMT+05:30 2008");
                                  } catch (ParseException e) {
                                       e.printStackTrace();
                                  }
                                  String parsed = parse.format(date);
  • 14. Re: Convert date to the format 25/11/2008
    807589 Newbie
    Currently Being Moderated
    sigh

    Not to overly complain here but somehow PhHein getting 10 dukes for this thread and me getting zero doesn't seem quite right...
1 2 Previous Next