wasabi3689
asked on
word "now" convert to time value
I have a drop-down in my jsp as below
<select name="endtime" selected ><option>now</option><opti on>01:00</ option><op tion>02:00 </option>< option>03: 00</option ><option>0 4:00</opti on><option >05:00</op tion><opti on>06:00</ option><op tion>07:00 </option>< option>08: 00</option ><option>0 9:00</opti on><option >10:00</op tion><opti on>11:00</ option><op tion>12:00 </option>< option>13: 00</option ><option>1 4:00</opti on><option >15:00</op tion><opti on>16:00</ option><op tion>17:00 </option>< option>18: 00</option ><option>1 9:00</opti on><option >20:00</op tion><opti on>21:00</ option><op tion>22:00 </option>< option>23: 00</option ><option>2 4:00</opti on></selec t>
in my drop-down there is a choice "now". I want to convert it to the current time (no date) just like 02:00.... when user pick "now" choice.
How to do it?
<select name="endtime" selected ><option>now</option><opti
in my drop-down there is a choice "now". I want to convert it to the current time (no date) just like 02:00.... when user pick "now" choice.
How to do it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<option value = "getNow()" >now</option>
where getNow is
function getNow()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
//var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
//s=checkTime(s)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
PS : this will submit the time on the client's clock as the value of now. Is this what you wanted ??
If you need server time, check the instructions from kuldeep .
where getNow is
function getNow()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
//var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
//s=checkTime(s)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
PS : this will submit the time on the client's clock as the value of now. Is this what you wanted ??
If you need server time, check the instructions from kuldeep .
ASKER
I got the following error from your code
An error occurred at line: 307 in the jsp file: /SearchFront.jsp
Generated servlet error:
C:\Documents and Settings\.netbeans\5.5\apa che-tomcat -5.5.17_ba se\work\Ca talina\loc alhost\IP_ Port_MAC\o rg\apache\ jsp\Search Front_jsp. java:418: ';' expected
function getNow()
^
1 error
An error occurred at line: 307 in the jsp file: /SearchFront.jsp
Generated servlet error:
C:\Documents and Settings\.netbeans\5.5\apa
function getNow()
^
1 error
the function provided by wasabi is a java script function. Make sure you are putting it outside of <% %> tags of JSP.. other wise compiler will treat it as Java code...
ASKER
I got the following error
Caused by: javax.servlet.ServletExcep tion: Unparseable date: "10/1/2007 getNow()"
at org.apache.jasper.runtime. PageContex tImpl.doHa ndlePageEx ception(Pa geContextI mpl.java:8 58)
at org.apache.jasper.runtime. PageContex tImpl.hand lePageExce ption(Page ContextImp l.java:791 )
at org.apache.jsp.SearchResul tByIP_jsp. _jspServic e(SearchRe sultByIP_j sp.java:54 7)
at org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:97)
Here is code in jsp
<select name="endtime"><option selected value = "getNow()">now</option><op tion>01:00 </option>
I put your code as JS and put it on as follows
<script type="text/javascript">
function getNow()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
//var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
//s=checkTime(s)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</Script>
Caused by: javax.servlet.ServletExcep
at org.apache.jasper.runtime.
at org.apache.jasper.runtime.
at org.apache.jsp.SearchResul
at org.apache.jasper.runtime.
Here is code in jsp
<select name="endtime"><option selected value = "getNow()">now</option><op
I put your code as JS and put it on as follows
<script type="text/javascript">
function getNow()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
//var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
//s=checkTime(s)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</Script>
ASKER
I am using Kuldeepchaturvedi:' code, I have the following error. Also
if ( value == "now") or
if ( value == "endtime")
An error occurred at line: 325 in the jsp file: /SearchFront.jsp
Generated servlet error:
C:\Documents and Settings\netbeans\5.5\apac he-tomcat- 5.5.17_bas e\work\Cat alina\loca lhost\IP_P ort_MAC\or g\apache\j sp\SearchF ront_jsp.j ava:436: cannot find symbol
symbol : variable value
location: class org.apache.jsp.SearchFront _jsp
if ( value == "now")
^
if ( value == "now") or
if ( value == "endtime")
An error occurred at line: 325 in the jsp file: /SearchFront.jsp
Generated servlet error:
C:\Documents and Settings\netbeans\5.5\apac
symbol : variable value
location: class org.apache.jsp.SearchFront
if ( value == "now")
^
Check your code, you are confusing here with JS and Java.
Kuldeeps code is Java .. so it should be <% if (value.equals("now")) { .... } %>
If you are using my JS code, then the value at the server WILL NOT be "now", instead it will be the actual value of now.,
Kuldeeps code is Java .. so it should be <% if (value.equals("now")) { .... } %>
If you are using my JS code, then the value at the server WILL NOT be "now", instead it will be the actual value of now.,
ASKER
i have the following error. What is wrong?
An error occurred at line: 325 in the jsp file: /SearchFront.jsp
Generated servlet error:
C:\Documents and Settings\netbeans\5.5\apac he-tomcat- 5.5.17_bas e\work\Cat alina\loca lhost\IP_P ort_MAC\or g\apache\j sp\SearchF ront_jsp.j ava:436: cannot find symbol
symbol : variable value
location: class org.apache.jsp.SearchFront _jsp
if (value.equals("now"))
An error occurred at line: 325 in the jsp file: /SearchFront.jsp
Generated servlet error:
C:\Documents and Settings\netbeans\5.5\apac
symbol : variable value
location: class org.apache.jsp.SearchFront
if (value.equals("now"))
>> if (value.equals("now"))
the code that I had put in was more of a suggestive code..
you need to declare the string and populate it..
String value = request.getParameter("endt ime");
& then my code...
the code that I had put in was more of a suggestive code..
you need to declare the string and populate it..
String value = request.getParameter("endt
& then my code...
ASKER
for Kuldeepchaturvedi:
<%
String MyEndTime = request.getParameter("endt ime");
if (MyEndTime.equals("now"))
{
Calendar cal2 =Calendar.getInstance();
Integer hr = new Integer( cal2.get(Calendar.HOUR_OF_ DAY));
Integer min = new Integer(( cal2.get(Calendar.HOUR_OF_ DAY)));
String time = hr.toString()+":"+min.toSt ring();
}
%>
But I have the following error
Caused by: javax.portlet.PortletExcep tion
at org.apache.pluto.core.impl .PortletRe questDispa tcherImpl. include(Po rtletReque stDispatch erImpl.jav a:62)
at com.test.IP_Port_MAC.doVie w(IP_Port_ MAC.java:1 85)
at javax.portlet.GenericPortl et.doDispa tch(Generi cPortlet.j ava:250)
at javax.portlet.GenericPortl et.render( GenericPor tlet.java: 178)
at org.apache.pluto.core.Port letServlet .dispatch( PortletSer vlet.java: 205)
at org.apache.pluto.core.Port letServlet .doGet(Por tletServle t.java:145 )
Caused by: java.lang.NullPointerExcep tion
at org.apache.jsp.SearchFront _jsp._jspS ervice(Sea rchFront_j sp.java:45 4)
at org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:97)
But when I comment out the code, I have no problem.
What is wrong?
<%
String MyEndTime = request.getParameter("endt
if (MyEndTime.equals("now"))
{
Calendar cal2 =Calendar.getInstance();
Integer hr = new Integer( cal2.get(Calendar.HOUR_OF_
Integer min = new Integer(( cal2.get(Calendar.HOUR_OF_
String time = hr.toString()+":"+min.toSt
}
%>
But I have the following error
Caused by: javax.portlet.PortletExcep
at org.apache.pluto.core.impl
at com.test.IP_Port_MAC.doVie
at javax.portlet.GenericPortl
at javax.portlet.GenericPortl
at org.apache.pluto.core.Port
at org.apache.pluto.core.Port
Caused by: java.lang.NullPointerExcep
at org.apache.jsp.SearchFront
at org.apache.jasper.runtime.
But when I comment out the code, I have no problem.
What is wrong?
It will happen if the value of the mytime is coming as null in the request..
can you post the whole code??
I think you are still not posting the correct request data where it needs to be.
can you post the whole code??
I think you are still not posting the correct request data where it needs to be.
ASKER
first I think
Integer min = new Integer(( cal2.get(Calendar.HOUR_OF_ DAY))); should be
Integer min = new Integer(( cal2.get(Calendar.minute)) ); right?
2. String time = hr.toString()+":"+min.toSt ring();
should be String endtime = hr.toString()+":"+min.toSt ring(); right?
Here it's my code
- <select name="endtime" ><option value="now">now</option><o ption >01:00</option><option>02: 00</option ><option>0 3:00</opti on><option >04:00</op tion><opti on>05:00</ option><op tion>06:00 </option>< option>07: 00</option ><option>0 8:00</opti on><option >09:00</op tion><opti on>10:00</ option><op tion>11:00 </option>< option>12: 00</option ><option>1 3:00</opti on><option >14:00</op tion><opti on>15:00</ option><op tion>16:00 </option>< option>17: 00</option ><option>1 8:00</opti on><option >19:00</op tion><opti on>20:00</ option><op tion>21:00 </option>< option>22: 00</option ><option>2 3:00</opti on><option >24:00</op tion></sel ect>
<%
String MyEndTime = request.getParameter("endt ime");
if (MyEndTime.equals("now"))
{
Calendar cal2 =Calendar.getInstance();
Integer hr = new Integer( cal2.get(Calendar.HOUR_OF_ DAY));
Integer min = new Integer(( cal2.get(Calendar.MINUTE)) );
String endtime = hr.toString()+":"+min.toSt ring();
}
%>
Integer min = new Integer(( cal2.get(Calendar.HOUR_OF_
Integer min = new Integer(( cal2.get(Calendar.minute))
2. String time = hr.toString()+":"+min.toSt
should be String endtime = hr.toString()+":"+min.toSt
Here it's my code
- <select name="endtime" ><option value="now">now</option><o
<%
String MyEndTime = request.getParameter("endt
if (MyEndTime.equals("now"))
{
Calendar cal2 =Calendar.getInstance();
Integer hr = new Integer( cal2.get(Calendar.HOUR_OF_
Integer min = new Integer(( cal2.get(Calendar.MINUTE))
String endtime = hr.toString()+":"+min.toSt
}
%>
yup you are correct about the mistakes on MINUTE and HOUR OFDAY things..
now is the code pasted by you above is in the same page??
the way it is suppose to work is that when user submits your page containing the drop down..
you get this value in whatever servlet/jsp you are using to process it..
the Java piece of code is suppose to go in that jsp/servlet... & not just after your drop down..
now is the code pasted by you above is in the same page??
the way it is suppose to work is that when user submits your page containing the drop down..
you get this value in whatever servlet/jsp you are using to process it..
the Java piece of code is suppose to go in that jsp/servlet... & not just after your drop down..
ASKER
<select name="endtime"><option selected value=now()>now</option><o