Pradeep
Pradeep

Reputation: 133

How do I format a java.sql.date into this format: "MM-dd-yyyy"?

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);

Upvotes: 11

Views: 114336

Answers (6)

Anonymous
Anonymous

Reputation: 86282

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.

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44834

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.

Upvotes: 0

Mustafa sabir
Mustafa sabir

Reputation: 4360

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.

Upvotes: 2

Nirav Prajapati
Nirav Prajapati

Reputation: 3005

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));

Upvotes: 2

Varun Achar
Varun Achar

Reputation: 15109

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.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201437

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

Upvotes: 15

Related Questions