conversion of date in string variable to date variable - java

how to convert date in string variable to date variable..??
Date in database is in yyyy-MM-dd format..
im entering in dd-MM-yyyy format..and trying to convert it in db format so that i can use 'between' query.. code is below
<%String date1=(String)request.getAttribute("from");%>
<%String date2=(String)request.getAttribute("to");%>
<%String empcode=(String)request.getAttribute("occ");%>
<%SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");%>
<%Date date = new Date(); %>
<%Date fromdate=formatter.parse(date1);%>
<%Date todate=formatter.parse(date2);%>

You're using getAttribute, but I suspect you wanted getParameter (if you're trying to get information submitted as part of a GET or POST request).
Here's what getAttribute works with:
Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. For example, for requests made using HTTPS, the attribute javax.servlet.request.X509Certificate can be used to retrieve information on the certificate of the client. Attributes can also be set programatically using setAttribute(java.lang.String, java.lang.Object). This allows information to be embedded into a request before a RequestDispatcher call.
Here's what getParameter works with:
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
Looking at the date formatting, you've mentioned two different formats in your question. If the date1 string looks like 2013-12-19, then that code will work. If it's 19-12-2013 then that code won't work because you've told SimpleDateFormat to use "yyyy-MM-dd" but you want "dd-MM-yyyy".
In both cases, the way to debug this is to look at what date1 and date2 contain. That would point the way toward how to fix it.

SimpleDateFormat formatter=new SimpleDateFormat("dd-MM-yyyy");
Date fromdate=formatter.parse(date1);
SimpleDateFormat formatter2=new SimpleDateFormat("yyyy-MM-dd");
String newDate1 = formatter2.format(fromdate);
newDate1 is now in format yyyy-MM-dd.
You can again create the date object with the new string
formatter2.parse(newDate1);
but not necessary I suppose

Related

how to send date through URL in ajax

Hi i have to append Date to a URL, maintaining its Datatype as Date and not as String
When in append Date to URL as "?date=" + $(target).val();
i fetch it through #RequestParameter in my Controller
But the problem here lies that i cant access it as Date but as String.
If i access it as Date it does not gives an error but changes the format of the Date from 2017-05-12 00:00:00 to something like Fri MAY 12 00:00:00 IST 2017.
So i need to send data as date through AJAX so when i access in my Controller i can maintain its format
As far as i know when i fetch the data as Date it gives me internal representation of Date because there is internal conversion of String to Date
Thus i need to know how i can send date through URL in AJAX
You can't pass types via GET URL parameters.
You need to convert the type on your java code.
For example:
new SimpleDateFormat( "MM/dd/yyyy H:m:s" ).parse( #RequestParam( "date" ) )
It's all in the javadoc for SimpleDateFormat

Java XML RPC date iso 8601 with timezone

I am having a hard time trying to make a web service client work. It is a XML RPC specification. I am using Apache WS XML-RPC library, which I find full of holes that causes problem due to Serialization. I have to send a Date parameter for the library to add the tags , however the web service expects it with the TZ, that means adding -0500 at the end of the Date object. If I dont send it as Date Object, it wont add the tags and it will fail. And when trying to do this:
DateFormat df = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ");
String fecha = df.format(new Date());
Date date = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ").parse(fecha);
And using parameter date, and it always sends it as
<dateTime.iso8601>20130517T20:30:33</dateTime.iso8601>
Can't find a way for it to send it as Date object in the format above but with the -0500 at the end. Any help would be appreciated.

Lotus Notes : Insert date value in Lotus Notes form using java agent

I am trying to upload the data from a delimited text file to the lotus notes form using java agent. The issue arises when I try to insert the date value to the notes document. After insert when i use ComputeWithForm, then it returns false. I am using simpledateformat to format the date in MM/dd/yyyy format, but it is still not working. Below is the excerpt from my code.
String delim, key, thekey, myDate;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy);
myDate = dateFormat.format(date);
newdoc.replaceItemValue("UploadDBDate", myDate);
Any help will be great.
Thanks,
Himanshu
myDate is a String object. The replaceItemValue method does not know that you have put a date into that String, therefore it treats it as ordinary text. If uploadDBDate is a DateTime field, that causes a type mismatch during the computeWithForm operation.
The Lotus classes for Java include a DateTime class. The Session class has a createDateTime method that you pass a "mm/dd/yyyy" string and return a DateTime object. Then you can pass that DateTime object into replaceItemValue instead of passing in myDate.
I would recommend you to do those things:
1) disable computewithform and simply save document and then verify field UploadDBDate, does it have correct value? does it have corrrect type?
2) if everything is fine with UploadDBDate then there is a problem on a form, so try to investigate what calculation you do on the form, because problem is there.

RichFaces 4.2 Calendar. How can I set the InputField of Calendar by JavaScript?

I have to copy a Date String at client side to the Calendar input field "renewal_date_input". The following JavaSript did not work:
document.getElementById('AddressDetails:renewal_date_input').value = renewalDateCombo.value.toString();
Although with:
alert("written date is: " + document.getElementById('AddressDetails:renewal_date_input').value);
We can read out the correct date string written previously.
But the string does not appear in the input field.
Manual Input is enabled:
enableManualInput="true"
Perhaps the JavaScript Function setInputField: function(dateStr, event) shipped with rich faces calendar.js could help, but how do I use it?
Use the setValue() function on the date component, passing a Date object as argument
var theCalendar = document.getElementById('AddressDetails:renewal_date_input');
theCalendar.setValue(document.getElementById('AddressDetails:renewal_date_input').getValue()); //Here I assume renewal_date_input is also a calendar component

request.getParameter String value pass as date to java

In my JSP page I have a field which is date and when I getting as request.getParameter("dateVal"); gives me
15-Dec-2012 12:21.
I would like to pass this value to my database procedure and insert/update into table.
How can I pass the value as setDate using prepareCall to database?
Thanks
First step would be using SimpleDateFormat to parse it to a fullworthy java.util.Date instance in the controller:
Date date = new SimpleDateFormat("dd-MMM-yyyy HH:mm.", Locale.ENGLISH).parse(dateVal);
Then you can just create a java.sql.Date around its time in the database layer:
statement.setDate(1, new java.sql.Date(date.getTime()));
Unrelated to the concrete problem, please note that java.sql.Date doesn't remember the time part. If you have actually a DATETIME or TIMESTAMP field in the DB and not a DATE field, then rather use setTimestamp() with a java.sql.Timestamp instead. This way the time part will also be stored.
#BalusC 's answer is perfect. But as an alternative solution you can use the function provided by database to convert String to Date while querying. For example(in case you use Oracle),
to_date(date_in_String, format)
Try this :
new SimpleDateFormat("dd-MM-yyyy HH:mm").parse(mydate);

Categories