10

I need to get a java.sql.date in the following format "MM-dd-yyyy", but I need it to stay a java.sql.date so I can put it into a table as date field. So, it cannot be a String after the formatting, it has to end up as a java.sql.date object.

This is what I have tried so far:

java.util.Date 
today=new Date();
String date = formatter.format(today); 
Date todaydate = formatter.parse(date);
java.sql.Date fromdate = new java.sql.Date(todaydate.getTime());
java.sql.Date todate=new java.sql.Date(todaydate.getTime()); 
String tempfromdate=formatter.format(fromdate);
String temptodate=formatter.format(todate); 
java.sql.Date fromdate1=(java.sql.Date) formatter.parse(tempfromdate); 
java.sql.Date todate1=(java.sql.Date) formatter.parse(temptodate);
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
Pradeep
  • 123
  • 1
  • 1
  • 11

6 Answers6

15

You can do it the same way as a java.util.Date (since java.sql.Date is a sub-class of java.util.Date) with a SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat(
    "MM-dd-yyyy");
int year = 2014;
int month = 10;
int day = 31;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1); // <-- months start
                                    // at 0.
cal.set(Calendar.DAY_OF_MONTH, day);

java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());
System.out.println(sdf.format(date));

Output is the expected

10-31-2014

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • yes but when u format Date u will get the output only as String type. – Pradeep Jun 20 '14 at 05:32
  • 2
    @user3758849 That is what it means to format a Date. Which is another way of saying that Date(s) aren't String(s); or if you prefer, String(s) aren't Date(s). – Elliott Frisch Jun 20 '14 at 05:35
  • 1
    yes i agree.but i could not check this formatted date which is in string and the database column which is in date. This is my problem. – Pradeep Jun 20 '14 at 05:55
2

Use below code i have convert today date. learn from it and try with your code

          Date today = new Date();

        //If you print Date, you will get un formatted output
        System.out.println("Today is : " + today);

        //formatting date in Java using SimpleDateFormat
        SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd-yyyy");
        String date = DATE_FORMAT.format(today);
        System.out.println("Today in MM-dd-yyyy format : " + date);

            Date date1 = formatter.parse(date);
    System.out.println(date1);
    System.out.println(formatter.format(date1));
Nirav Prajapati
  • 2,987
  • 27
  • 32
  • Did you check my code? Even i've converted today's date – Pradeep Jun 20 '14 at 05:38
  • java.util.Date today=new Date(); String date = formatter.format(today); Date todaydate = formatter.parse(date); Done it already. i need to pass the sql date to my query. – Pradeep Jun 20 '14 at 05:47
  • Very useful construction String date = DATE_FORMAT.format(today); It helped me. – hariprasad Apr 07 '17 at 11:36
1

A simpler solution would be to just convert the date in the query to epoch before comparing.

SELECT date_column from YourTable where UNIX_TIMESTAMP(date_column) > ?;

Then, simply pass date.getTime() when binding value to ?.

NOTE: The UNIX_TIMESTAMP function is for MySQL. You'll find such functions for other databases too.

Varun Achar
  • 14,781
  • 7
  • 57
  • 74
1
java.util.Date today=new Date();
java.sql.Date date=new java.sql.Date(today.getTime()); //your SQL date object
SimpleDateFormat simpDate = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(simpDate.format(date)); //output String in MM-dd-yyyy

Note that it does not matter if your date is in format mm-dd-yyyy or any other format, when you compare date (java.sql.Date or java.util.Date) they will always be compared in form of the dates they represent. The format of date is just a way of setting or getting date in desired format.

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
0

The formatter.parse will only give you a java.util.Date not a java.sql.Date

once you have a java.util.Date you can convert it to a java.sql.Date by doing

java.sql.Date sqlDate = new java.sql.Date (normalDate.getTime ());

Also note that no dates have any built in format, it is in reality a class built on top of a number.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

For anyone reading this in 2017 or later, the modern solution uses LocalDate from java.time, the modern Java date and time API, instead of java.sql.Date. The latter is long outdated.

Formatting your date

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd-uuuu", Locale.US);
    LocalDate fromDate = LocalDate.now(ZoneId.of("Asia/Kolkata"));
    String tempFromDate = fromDate.format(formatter);
    System.out.println(tempFromDate);

This prints something like

11-25-2017

Don’t confuse your date value with its textual representation

Neither a LocalDate nor a java.sql.Date object has any inherent format. So please try — and try hard if necessary — to keep the two concepts apart, the date on one side and its presentation to a user on the other.

It’s like int and all other data types. An int can have a value of 4284. You may format this into 4,284 or 4 284, 004284 or even into hex representation. This does in no way alter the int itself. In the same way, formatting your date does not affect your date object. So use the string for presenting to the user, and use LocalDate for storing into your database (a modern JDBC driver or other modern means of database access wil be happy to do that, for example through PreparedStatement.setObject()).

Use explicit time zone

Getting today’s date is a time zone sensitive operation since it is not the same date in all time zones of the world. I strongly recommend you make this fact explicit in the code. In my snippet I have used Asia/Kolkata time zone, please substitute your desired time zone. You may use ZoneId.systemDefault() for your JVM’s time zone setting, but please be aware that this setting may be changed under our feet by other parts of your program or other programs running in the same JVM, so this is fragile.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161