Solved

# word "now" convert to time value

Posted on 2007-09-28
178 Views
I have a drop-down in my jsp as below

<select name="endtime" selected ><option>now</option><option>01:00</option><option>02:00</option><option>03:00</option><option>04:00</option><option>05:00</option><option>06:00</option><option>07:00</option><option>08:00</option><option>09:00</option><option>10:00</option><option>11:00</option><option>12:00</option><option>13:00</option><option>14:00</option><option>15:00</option><option>16:00</option><option>17:00</option><option>18:00</option><option>19:00</option><option>20:00</option><option>21:00</option><option>22:00</option><option>23:00</option><option>24:00</option></select>

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?
0
Question by:wasabi3689

Author Comment

Can I do the following like this

<select name="endtime"><option selected value=now()>now</option><option>01:00</option><option>02:00
0

LVL 19

Accepted Solution

if ( value == "endtime")
{
Calender cal =Calender.getInstance();
Integer hr = new Integer( cal.get(Calender.HOUR_OF_DAY));
Integer min = new Integer(( cal.get(Calender.HOUR_OF_DAY));
String time = hr.toString();+":"+min.toString();
}
0

LVL 3

Expert Comment

<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 .
0

Author Comment

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\apache-tomcat-5.5.17_base\work\Catalina\localhost\IP_Port_MAC\org\apache\jsp\SearchFront_jsp.java:418: ';' expected
function getNow()
^
1 error
0

LVL 19

Expert Comment

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...
0

Author Comment

I got the following error
Caused by: javax.servlet.ServletException: Unparseable date: "10/1/2007 getNow()"
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
at org.apache.jsp.SearchResultByIP_jsp._jspService(SearchResultByIP_jsp.java:547)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)

Here is code in jsp

<select name="endtime"><option selected value = "getNow()">now</option><option>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>

0

Author Comment

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\apache-tomcat-5.5.17_base\work\Catalina\localhost\IP_Port_MAC\org\apache\jsp\SearchFront_jsp.java:436: cannot find symbol
symbol  : variable value
location: class org.apache.jsp.SearchFront_jsp
if ( value == "now")
^
0

LVL 3

Expert Comment

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.,
0

Author Comment

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\apache-tomcat-5.5.17_base\work\Catalina\localhost\IP_Port_MAC\org\apache\jsp\SearchFront_jsp.java:436: cannot find symbol
symbol  : variable value
location: class org.apache.jsp.SearchFront_jsp
if (value.equals("now"))
0

LVL 19

Expert Comment

>>  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("endtime");

& then my code...
0

Author Comment

for Kuldeepchaturvedi:

<%

String MyEndTime = request.getParameter("endtime");

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.toString();
}
%>

But I have the following error

Caused by: javax.portlet.PortletException
at org.apache.pluto.core.impl.PortletRequestDispatcherImpl.include(PortletRequestDispatcherImpl.java:62)
at com.test.IP_Port_MAC.doView(IP_Port_MAC.java:185)
at javax.portlet.GenericPortlet.doDispatch(GenericPortlet.java:250)
at javax.portlet.GenericPortlet.render(GenericPortlet.java:178)
at org.apache.pluto.core.PortletServlet.dispatch(PortletServlet.java:205)
at org.apache.pluto.core.PortletServlet.doGet(PortletServlet.java:145)
Caused by: java.lang.NullPointerException
at org.apache.jsp.SearchFront_jsp._jspService(SearchFront_jsp.java:454)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)

But when I comment out the code, I have no problem.

What is wrong?
0

LVL 19

Expert Comment

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.
0

Author Comment

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.toString();

should be   String endtime = hr.toString()+":"+min.toString(); right?

Here it's my code

- <select name="endtime"  ><option value="now">now</option><option >01:00</option><option>02:00</option><option>03:00</option><option>04:00</option><option>05:00</option><option>06:00</option><option>07:00</option><option>08:00</option><option>09:00</option><option>10:00</option><option>11:00</option><option>12:00</option><option>13:00</option><option>14:00</option><option>15:00</option><option>16:00</option><option>17:00</option><option>18:00</option><option>19:00</option><option>20:00</option><option>21:00</option><option>22:00</option><option>23:00</option><option>24:00</option></select>

<%

String MyEndTime = request.getParameter("endtime");

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.toString();

}

%>
0

LVL 19

Expert Comment

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..

0

## Featured Post

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.