1

I have the below method in which date is coming as parameter that is in form of string and that parameter name is dateString as shown below and ultimately the date is converted and stored n form of java.sql.Date which is also return type of this method.

public static java.sql.Date getSimpleDate11(String dateString) {

        if (dateString == null) {
            return null;
        }
        java.util.Date date = null;
        java.sql.Date sqlDate = null;

        try {

            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            df.setLenient(false);
            date = df.parse(dateString);
            sqlDate = new java.sql.Date(date.getTime());
        } catch (Exception pe) {
            throw new RuntimeException(
                    "The date entered is invalid or has incorrect format"
                            + dateString);
        }
        return sqlDate;
    }

Question:

I found that value is coming in this format 2014-07-23 (YYYY-MM-dd) and I want the return date (java.sql..Date) to be in 23-07-14 (dd-MM-YY).

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
user3440926
  • 79
  • 1
  • 2
  • 9
  • possible duplicate of [Calendar date to yyyy-MM-dd format in java](http://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java) – MadProgrammer Mar 22 '14 at 04:45
  • `Date` is a container for the number of milliseconds since January 1, 1970, 00:00:00 GMT - the format is irrlevent – MadProgrammer Mar 22 '14 at 04:46
  • @MadProgrammer Request you to please advise and show if possible how can I cahenge the format from YYYY-MM-dd to dd-MM-YY format , please advise – user3440926 Mar 22 '14 at 04:48
  • i thnk the section in whih it shows that question ia already ansered is different – user3440926 Mar 22 '14 at 04:49
  • You can't - that's the answer, the format of `Date` is based on local. `DateFormatter`'s are used to take a `Date` format and format them to some `String` value. `Date` is juts a container for the number of milliseconds since the epoch - it has no concept of format, it can't be changed and shouldn't be changed – MadProgrammer Mar 22 '14 at 04:55
  • *"i thnk the section in whih it shows that question ia already ansered is different"* - I think you're missing the point. You're trying to change the way `Date#toString` prints the `Date` format, you can't as the linked answer explains. – MadProgrammer Mar 22 '14 at 04:56
  • @MadProgrammer Thanks Request you to please adise then how can I achieve this , request you to please advise if possible can you please advise what changes I need to do in the code that I posted above. – user3440926 Mar 22 '14 at 04:57
  • You can't and you shouldn't need to. If the column in the database is some form of `DATE`, `DATETIME` or `TIMESTAMP`, then the JDBC driver should accept the `java.sql.Date` as is and will, as required, parse the value to and from the database. If the column is some other type, then using something like `SimpleDateFormat` to produce the format you want is the correct process, but you won't be able to use `java.sql.Date` - the format should be irrelevant, its the concept of "time" that's important – MadProgrammer Mar 22 '14 at 04:59
  • @MadProgrammer ok agreed then is there any alternative then to achieve this thing , any alternative as I am stuck up now – user3440926 Mar 22 '14 at 05:02

2 Answers2

0

This generates the date format you want - just use String as the return type instead of java.sql.Date:

public static String getSimpleDate11(String dateString) {
    if (dateString == null) {
        return null;
    }

    DateFormat dfIn = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat dfOut = new SimpleDateFormat("dd-MM-yy");

    try {
        Date date = dfIn.parse(dateString);
        return dfOut.format(date);
    } catch (ParseException e) {
        throw new RuntimeException(
                "The date entered is invalid or has incorrect format"
                        + dateString);
    }
}
0

In case you need to convert the incoming "yyyy-MM-dd" String date to a java.sql.Date object you already did everything right in the code you posted in your question.

A java.sql.Date object, just like a java.util.Date, stores the date you give it internally in some format you, as a programmer, don't have to care about. You just have to know that the date is stored in the object and that you can get it out of the object whenever you need it. If you're interested in technical details you can google for it but, as I said, in your case this doesn't matter.

Whenever you need a java.util.Date object just use the one you get out of your original getSimpleDate11(...) function. Whenever you need a String representation of the date in a certain format, like "dd-MM-yy", take the java.util.Date and plug it into a DateFormat object initialized with the output format you want, just like I did in my first answer with DateFormat dfOut (the format(...) method of DateFormat can handle both, java.util.Date and java.sql.Date objects).