fork download
  1. The legacy date-time API is known to be [confusing and error-prone][1]. Since you are using Java-7, unfortunately, either you will have to live with it or you can backport the code using [modern date-time API](https://w...content-available-to-author-only...e.com/technical-resources/articles/java/jf14-date-time.html) to Java-7 using [ThreeTen-Backport][2] library.
  2.  
  3. I recommend you address the following problems with your code:
  4.  
  5. 1. Never use a date-time parsing/formatting API without a `Locale`.
  6. 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
  7.  
  8. The following code incorporates these comments:
  9.  
  10. import java.text.SimpleDateFormat;
  11. import java.util.Locale;
  12.  
  13. public class Main {
  14. public static void main(String[] args) {
  15. // Today
  16. java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
  17.  
  18. System.out.println(getSQLDateToString(date, "MM/dd/yyyy"));
  19. System.out.println(getSQLDateToString(date, "dd/MM/yyyy"));
  20. }
  21.  
  22. public static String getSQLDateToString(java.sql.Date date, String format) {
  23. return new SimpleDateFormat(format, Locale.ENGLISH).format(date);
  24. }
  25. }
  26.  
  27. **Output:**
  28.  
  29. 02/07/2021
  30. 07/02/2021
  31.  
  32. **Using the modern date-time API**
  33.  
  34. Use `java.sql.Date#toLocalDate` to convert a `java.sql.Date` to `LocalDate`.
  35.  
  36. import java.time.LocalDate;
  37. import java.time.format.DateTimeFormatter;
  38. import java.util.Locale;
  39.  
  40. public class Main {
  41. public static void main(String[] args) {
  42. // Today
  43. java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
  44.  
  45. System.out.println(getSQLDateToString(date, "MM/dd/yyyy"));
  46. System.out.println(getSQLDateToString(date, "dd/MM/yyyy"));
  47. }
  48.  
  49. public static String getSQLDateToString(java.sql.Date date, String format) {
  50. LocalDate localDate = date.toLocalDate();
  51. DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format, Locale.ENGLISH);
  52. return dtf.format(localDate);
  53. }
  54. }
  55.  
  56. **Output:**
  57.  
  58. 02/07/2021
  59. 07/02/2021
  60.  
  61.  
  62. Learn about the modern date-time API from **[Trail: Date Time](https://d...content-available-to-author-only...e.com/javase/tutorial/datetime/index.html)**.
  63.  
  64. [3]: https://w...content-available-to-author-only...e.com/technical-resources/articles/java/jf14-date-time.html
  65. [4]: https://w...content-available-to-author-only...n.org/threetenbp/
  66. [5]: https://d...content-available-to-author-only...e.com/javase/7/docs/api/java/text/DateFormat.html#format(java.util.Date)
  67. [6]: https://d...content-available-to-author-only...e.com/javase/7/docs/api/java/text/DateFormat.html#parse(java.lang.String)
  68.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class, interface, or enum expected
The legacy date-time API is known to be [confusing and error-prone][1]. Since you are using Java-7, unfortunately, either you will have to live with it or you can backport the code using [modern date-time API](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html) to Java-7 using [ThreeTen-Backport][2] library.
^
Main.java:5: error: illegal character: '`'
 1. Never use a date-time parsing/formatting API without a `Locale`.
                                                           ^
Main.java:5: error: illegal character: '`'
 1. Never use a date-time parsing/formatting API without a `Locale`.
                                                                  ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                            ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                  ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                     ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                                                   ^
Main.java:6: error: illegal character: '#'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                                                                    ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                                                                           ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                                                                                                                                               ^
Main.java:6: error: illegal character: '#'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                                                                                                                                                                ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                                                                                                                                                                      ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ^
Main.java:6: error: illegal character: '`'
 2. If your method can not handle an exception (i.e. can not do something to recover from the failure), it should simply throw the exception so that the calling method can get an opportunity to handle it appropriately. Therefore, using `e.printStackTrace()` in your method, `getSQLDateToString` is a bad idea. By the way, [`SimpleDateFormat#format`][3] does not throw an exception. Probably, you got confused with [`SimpleDateFormat#parse`][4] which throws `ParseException`.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ^
Main.java:27: error: class, interface, or enum expected
**Output:**
^
Main.java:34: error: illegal character: '`'
Use `java.sql.Date#toLocalDate` to convert a `java.sql.Date` to `LocalDate`.
    ^
Main.java:34: error: illegal character: '#'
Use `java.sql.Date#toLocalDate` to convert a `java.sql.Date` to `LocalDate`.
                  ^
Main.java:34: error: illegal character: '`'
Use `java.sql.Date#toLocalDate` to convert a `java.sql.Date` to `LocalDate`.
                              ^
Main.java:34: error: illegal character: '`'
Use `java.sql.Date#toLocalDate` to convert a `java.sql.Date` to `LocalDate`.
                                             ^
Main.java:34: error: illegal character: '`'
Use `java.sql.Date#toLocalDate` to convert a `java.sql.Date` to `LocalDate`.
                                                           ^
Main.java:34: error: illegal character: '`'
Use `java.sql.Date#toLocalDate` to convert a `java.sql.Date` to `LocalDate`.
                                                                ^
Main.java:34: error: illegal character: '`'
Use `java.sql.Date#toLocalDate` to convert a `java.sql.Date` to `LocalDate`.
                                                                          ^
Main.java:37: error: class, interface, or enum expected
    import java.time.format.DateTimeFormatter;
    ^
Main.java:38: error: class, interface, or enum expected
    import java.util.Locale;
    ^
Main.java:56: error: class, interface, or enum expected
**Output:**
^
26 errors
stdout
Standard output is empty