-1

Is there a way to change to format of java.sql.date from yyyy-mm-dd to dd-MMM-yy?

The code I have so

java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
Kneelac
  • 15
  • 1
  • 5
  • Yes you can. Follow the question https://stackoverflow.com/questions/3178690/best-way-to-convert-java-sql-date-from-yyyy-mm-dd-to-dd-mmmm-yyyy-format . Just change the pattern as per your need. – Kamal Singh Nov 25 '17 at 05:45
  • 1
    Both `java.util.Date` and `java.sql.Date` are outmoded, troublesome old legacy classes. Use only java.time classes instead. `myResultSet.getObject( … , LocalDate.class ).format( DateTimeFormatter.ofPattern( "dd-MMM-uu" ) )` – Basil Bourque Nov 25 '17 at 06:28
  • Possible duplicate of [How do I format a java.sql.date into this format: “MM-dd-yyyy”?](https://stackoverflow.com/questions/24320378/how-do-i-format-a-java-sql-date-into-this-format-mm-dd-yyyy). I know it’s not the same format, but I trust you can grap the idea and adapt it. – Ole V.V. Nov 25 '17 at 06:59
  • Any particular reason why you are using the long outdated `Date` classes? Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 25 '17 at 07:02

1 Answers1

0

You can format java.sql.Date with java.text.SimpleDateFormat:

String strDate = new SimpleDateFormat("dd-MMM-yy").format(new java.sql.Date(0));
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    The troublesome classes used here have been legacy for years, supplanted by the java.time classes built into Java 8 and Java 9, and back-ported to Java 6 & Java 7 & Android. – Basil Bourque Nov 25 '17 at 06:30