1

I have this code

public static void main(String[] args) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    String parsed = "15/07/2015";
    java.util.Date date = format.parse(parsed);
    java.sql.Date sql = new java.sql.Date(date.getTime());
    System.out.println(sql);
}

where I am trying to get the date format as 15/07/2015 to insert into the DB. Whereas, everytime it provides me an output as 2015-07-15. I do not understand why is it so.

  • Is it because the default date date formatting for java.sql.Date is yyyy-MM-dd.?
  • Or, do I need to set a different date format after parsing into java.sql.Date?

Any suggestions or guidance would help. Thanks

2
  • 1
    You're not storing a date string in your DB, you're storing a date. Commented Jul 15, 2015 at 17:04
  • Yes you are right. Does it mean that I will be unable to view the date from my DB as dd-MM-yyyy? Commented Jul 15, 2015 at 17:06

2 Answers 2

3

A java.sql.Date, like a java.util.Date, stores a date value that is unformatted. When you pass it to System.out.println, toString() is called on it. The toString() method on java.sql.Date prints the date in "yyyy-mm-dd" format.

You don't need to set any date format. If you need a specific format in Java, you can use your SimpleDateFormat object to format it into a String; a java.sql.Date is a java.util.Date. But you can pass the java.sql.Date directly to the database; that is for what it was designed. Most likely your database has some kind of conversion function(s) for formatting and parsing date values like Java does.

Sign up to request clarification or add additional context in comments.

3 Comments

So, here is what I am actually trying : In a Jtable I need to filter the dates, say between Date1 and Date2 and give the search results based on that. So, here will it work if I provide Date in any format?
I would parse whatever string you're getting as a date in Java, pass it as a java.sql.Date to the database, receive the java.sql.Dates back from the DB, and then format them when displaying them to the user.
@mustangDC You are confusing date-time values with strings. That is like confusing the characters "1" and "3" for the number 13. Databases do not store date-time values as strings, they have date-time data types. Java does not handle date-time values as strings; Java offers at least 3 date-time frameworks with classes to represent date-time values: [1] java.util.Date/.Calendar, [2] java.time built into Java 8 and later, [3] the 3rd-party Joda-Time library. The java.sql.* classes shuttle data between databases and Java apps w/o string conversions.
1

You are using java.sql.Date corresponds to SQL DATE which means and it will store in the format of year-month-date. You can not format a date. If you want it to be in the format 2015-07-15 then you will have to convert date to string using date formatter.

SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
String date1 = format1.format(date);   //date is your date from db

here date1 will be in the required format

Step 1) Store the date in database as date only

  java.util.Date date = format.parse(parsed);
  // code to store date to db

Step2 ) While displaying data to JTable

Get date from database

date = getdatefromdatabase(); // method like this

then use the date formatter

   SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
   String date1 = format1.format(date);   //date is your date from db

Send this date1 to jtable.

6 Comments

Thanks for the info. But as I am trying to filter a JTable ultimately based on date ranges. Will it work if I provide a Date format like this?
yes you can do it . you should store it in database format and when you display it you need to format it.you dont need to store it in that format. And if you want to store in that format then change datatype to string and store by formatting the date
Could you help me with setting the date format to dd-MM-yyyy before sending it to a JTable. ?
i have editted the code.can help you more if you will show me the code where you get the data to display to jtable
Actually I am using rs2xml and I found that I need to modify the code, if I want to do such a thing. But, nevertheless it gives me the correct date filtering in whatever format(currently using dd-MM-yyyy) I provide it to a JTable through a datepicker. So, as a conclusion : Java is so Holy that it understands everything :). Solved my problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.