0

I am working on dates in java. The real task is to search the data in between two dates from oracle database. I took the default date format of java using SimpleDateFormat as dd-MMM-yyyy ( the date appears in same format in oracle 10g xe) but when I run the project in netbeans, the date converts to YYYY-MM-DD ( I printed the date in console). This became issue for me to do searching, Can any on suggest me solution for this? .I also pasted my code below here

DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
java.util.Date from_date2 = (Date)sdf.parse(from_date1);
java.sql.Timestamp ts1 = new java.sql.Timestamp(from_date2.getTime());
String from_date = ts1.toString();
System.out.println(ts1.toString());
java.util.Date to_date2 = sdf.parse(to_date1);
java.sql.Timestamp ts2 = new java.sql.Timestamp(to_date2.getTime());
System.out.println(ts2.toString());
String to_date = ts2.toString();
Simulant
  • 19,190
  • 8
  • 63
  • 98
Satya Achanta
  • 47
  • 1
  • 10
  • 1
    From the doc of Timestamp's `toString`: `a String object in yyyy-mm-dd hh:mm:ss.fffffffff format` – Alexis C. May 16 '14 at 14:24
  • 1
    Why are you formatting your date to text? Use the date method(s) of [PreapredStatement](http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setDate%28int,%20java.sql.Date%29) for update / insert, and the [ResultSet](http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getDate%28java.lang.String%29) for read(s). – Elliott Frisch May 16 '14 at 14:24
  • hello Elliott Frisch, I am trying to make date to text but I need the date in the format 12-MAR-14 only, even No need of getting date and time, I just need Date part and want tom retrieve the data basing on that date – Satya Achanta May 16 '14 at 15:08

2 Answers2

0

instead of

DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
java.util.Date from_date2 = (Date)sdf.parse(from_date1);
java.sql.Timestamp ts1 = new java.sql.Timestamp(from_date2.getTime());
String from_date = ts1.toString();
System.out.println(ts1.toString());

use your DateFormat to format the output. The toString of Timestamp used a different default format:

DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
java.util.Date from_date2 = (Date)sdf.parse(from_date1);
System.out.println(sdf.format(from_date2));
Simulant
  • 19,190
  • 8
  • 63
  • 98
  • Hello @ Simulant, I got the date in the format Thu Mar 13, but I need that in 12-MAR-2014 or 12-MAR-14, Can u please help me out – Satya Achanta May 16 '14 at 15:07
0

I don't understand why you create a Date, then you convert it in Time to display it. Just do :

Date yourDate = ...
DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println(sdf.format(yourDate));

It will display your date as you need. But, maybe what you need is a java.sql.Date ? So, check this: How to convert java.util.Date to java.sql.Date?

Community
  • 1
  • 1
lpratlong
  • 1,421
  • 9
  • 17
  • Hello Ipratlong, I am just wondering can i get java date in format 13-MAR-14 format. As I thought that i cannot get in that format, I tried to change the date in sql developer NLS settings, for me the format of date changed in SQL developer but not in the oracle database, i dont know why it happened for me – Satya Achanta May 16 '14 at 15:13
  • dd-MMM-yyyy will give you 13-Mar-2014. If you want to match 13-MAR-14, you can use "dd-MMM-yy". But I don't know if there is a way to get the month directly in uppercase. You can find all patterns here: http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html – lpratlong May 16 '14 at 15:31
  • @Ipratlong: I tried all those ways, but not getting the desired format from java – Satya Achanta May 16 '14 at 15:32
  • What do you get right now with dd-MMM-yy ? If you really need uppercase on month, just call `.toUpperCase()` on the `sdf.format` result. – lpratlong May 16 '14 at 15:34
  • NO @Ipratlong: I need the date in format 13-MAR-14, but I got Fri Mar 14. That is the only thing bothering me – Satya Achanta May 16 '14 at 15:39
  • That's not normal. I just try it and it works. `java.util.Date date = new Date(); java.text.DateFormat sdf = new java.text.SimpleDateFormat("dd-MMM-yy"); System.out.println(sdf.format(date));` – lpratlong May 16 '14 at 15:44
  • Hey let me know how can i send image to you @Ipratlong, i will send image of my output to you – Satya Achanta May 16 '14 at 15:46
  • I don't know if it's possible on this site. Maybe you should upload your image yo a free site then share the URL here. – lpratlong May 16 '14 at 15:49
  • No again same error repeated, i am taking the input date in the form of 13-Apr-14 and when I converted into date and then printing it, it comes like Thu May 13 java.text.DateFormat sdf=new java.text.SimpleDateFormat("dd-MMM-yy"); java.util.Date ts1=(Date)sdf.parse(from_date1); System.out.print(ts1); java.util.Date ts2=(Date)sdf.parse(to_date1); System.out.println(ts2); the above is the code i used to convert the input 13-may-14 into the required format – Satya Achanta May 16 '14 at 15:51
  • Ok, so you did not read our answers... What you are printing is not the Formatted date but the Date object... You have to do `sdf.format(to_date1)` in the System.out.println if you want to see the date as the well-formatted text. And I don't understand why you are trying to do that since your "from_date1" variable contains what you need. – lpratlong May 16 '14 at 15:59
  • yes Ipratlong, when I am trying to use from_date1 as it is, the value is changing to "thu mar 13", so I am trying to customize it – Satya Achanta May 16 '14 at 16:02
  • Yes @Ipratlong: I got it, I am sorry for bothering you, there is no need of converting into timestamp and dates, I am sorry for that, thanks for helping me – Satya Achanta May 16 '14 at 16:07
  • When you do `System.out.println(to_date1)`, it displays the result of `to_date1.toString()`. Just take a look at the code of the toString method and you will understand http://www.docjar.com/html/api/java/util/Date.java.html . That's why you have to use `System.out.println(sdf.format(to_date1))` instead! – lpratlong May 16 '14 at 16:08