Link to home
Start Free TrialLog in
Avatar of wasabi3689
wasabi3689Flag for United States of America

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><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?
Avatar of wasabi3689
wasabi3689
Flag of United States of America image

ASKER

Can I do the following like this

<select name="endtime"><option selected value=now()>now</option><option>01:00</option><option>02:00
ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rlbalan
rlbalan

<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 .
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
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...
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>
 
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")
                             ^
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.,
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"))
>>  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...
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?
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.
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();
                             
                             
                        }
                   
                 %>
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..