0

This Question might have been answered here quite often but I could not find an answer specific to my question, I as well tried to go through http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html and infer something but to no avail.

I have a simple string date YYYY-MM-DD and I want to convert it into corresponding Date type like DD-MM-YYYY and save it in a database under a date type column. Any help would be highly appreciated.

David Brossard
  • 13,584
  • 6
  • 55
  • 88
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • 1
    What is the problem with `order by`? – Masudul Nov 27 '13 at 12:22
  • possible duplicate of [String to Date in Different Format in Java](http://stackoverflow.com/questions/882420/string-to-date-in-different-format-in-java) – Sully Nov 27 '13 at 12:24
  • Yes the order by is the problem, I am not interested in the time stamp however the Date is important and the way it is presented that is DD-MM-YYYY – Skynet Nov 27 '13 at 12:25
  • Duplicate of http://stackoverflow.com/questions/11446420/parse-string-to-date-java – David Brossard Nov 27 '13 at 12:25
  • Yes I already mentioned that it has been answered, but I want a specific solution and I want to understand how it works. – Skynet Nov 27 '13 at 12:27
  • why are you bothering to convert it to a string *before* sorting? That's a lot more work than sorting by the long value and then converting the dates to your format. – MadConan Nov 27 '13 at 13:05
  • I am converting a string to Date not the other way round. – Skynet Nov 27 '13 at 13:10

4 Answers4

2

try this way

String DateStr="2012-12-12";
Date d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr); 

java.sql.Date d1 = new java.sql.Date(d.getTime());

Now if you are using PreparedStatement then

pt.setDate(1,d1);
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • @BalusC Thanks for leaving a comment.Sorry i didnt find any mistake. – SpringLearner Nov 27 '13 at 12:36
  • @BalusC sorry for the mistake.Just compiled and pasted it here – SpringLearner Nov 27 '13 at 12:44
  • Finally the right answer covering all aspects stated in the question! I removed the downvote. Please interpret that undeserved sympathy upvote as my upvote. – BalusC Nov 27 '13 at 12:45
  • @BalusC Thanks for removing the downvote but what is mean by **Please interpret that undeserved sympathy upvote as my upvote** – SpringLearner Nov 27 '13 at 12:48
  • When your answer was downvoted to -1, some nitwit counteracted it by upvoting it back to 0 while your answer was still wrong. This is not right and it's called a "sympathy upvote". – BalusC Nov 27 '13 at 12:49
  • @BalusC I think a user just upvoted all the answers,every ones answer changed from -1 to 0.well it would have been nice if you just pointed the mistake before downvoting.Well thanks again now – SpringLearner Nov 27 '13 at 12:52
  • If a mistake is obvious by simply running the code in answer yourself, I always downvote (and also always remove downvote once fixed). – BalusC Nov 27 '13 at 12:54
  • Its actually a good practice, because so many answers mislead and the best should get filtered. – Skynet Nov 27 '13 at 12:54
0

Use a TreeMap instead of HashMap. As Date already implements Comparable, it will be sorted automatically on insertion.

Map<Date, ArrayList> m = new TreeMap<Date, ArrayList>();

Alternatively, if you have an existing HashMap and want to create a TreeMap based on it, pass it to the constructor:

Map<Date, ArrayList> sortedMap = new TreeMap<Date, ArrayList>(m);
constantlearner
  • 5,157
  • 7
  • 42
  • 64
0

If you scenario is to convert a string to date then save it to your DB, then i think that you don't have to convert the string to date, just pass the date string with a proper format(this depends on your DB vendor) to your insert method, and it will be saved successfully into your DB.

Salah
  • 8,567
  • 3
  • 26
  • 43
  • Its a lightweight database, called as SQlite. Not so robust. Because I need calculations based on this date, it would be apt to store it in Date format. – Skynet Nov 27 '13 at 12:29
0

When you have strings like "10-07-2015" and "01-08-2017" in your database and use ORDER BY the printed result will be in the sequence of "01-08-2017" , "10-07-2015" .

Same thing happens when you put the string dates into a Treemap, because both Treemap and ORDER BY identify the input as numbers, not as dates.

When you use a different date format, like "yyyy-MM-dd", here the print output is in the correct sequence, "2015-07-10" , 2017-08-01". But it is in the wrong date format.

One workaround solution would be to convert dates into longs before adding them to the database :

Date date_1 = Mon Jul 10 16:06:22 BRT 2015;
Date date_2 = Fri Aug 01 14:04:23 BRT 2017;

Long long_Date_1 = date_1.getTime();    // (prints something like 1502133102260)
Long long_Date_2 = date_2.getTime();    // (prints something like 1233335544343)

When you put these longs into the database, you can retrieve them sorted from the database and easily convert them into date string with the desired format:

Date date_1 = new Date ( long_Date_1 );
Date date_2 = new Date ( long_Date_2 );

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

formatter.format(date_1));  // prints "10-07-2015" 
formatter.format(date_2));  // prints  "01-08-2017" 
rainer
  • 3,295
  • 5
  • 34
  • 50