Reputation: 7
the web service receives two string Hora_in and Hora_out, I'm trying to convert those string to date, subtract the conversion of the two strings and convert the result to string, but the error appears.
WS00041: Service invocation threw an exception with message : null; Refer to the server log for more details
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;
@WebService(serviceName = "Parking")
@Stateless()
public class Parking {
/**
* This is a sample web service operation
* @param Hora_in
* @param Hora_out
* @return
* @throws java.text.ParseException
*/
@WebMethod(operationName = "Reserva")
public String Reserva(@WebParam(name = "Hora_in") String Hora_in, @WebParam(name = "Hora_out") String Hora_out) throws ParseException {
String parqueo;
Locale locale = new Locale("es","CO");
String hourf = "HH:mm:ss 'Z'";
SimpleDateFormat format = new SimpleDateFormat(hourf, locale);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date Hin = format.parse(Hora_in);
Date Hout = format.parse(Hora_out);
long diff = Hin.getTime() - Hout.getTime();
Date difh = new Date(diff);
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss 'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
parqueo=dateFormat.format(difh);
return parqueo;
}
}
below WS00041, the following message appears:
Exceptions details : java.lang.reflect.InvocationTargetException
javax.servlet.ServletException: java.lang.reflect.InvocationTargetException
at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:342)
at org.glassfish.webservices.monitoring.WebServiceTesterServlet.invoke(WebServiceTesterServlet.java:106)
at org.glassfish.webservices.EjbWebServiceServlet.service(EjbWebServiceServlet.java:143)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:226)
at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:178)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:313)
... 24 more
Caused by: jaxws.ParseException_Exception: Unparseable date: "10:00"
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:147)
at com.sun.xml.ws.client.sei.StubHandler.readResponse(StubHandler.java:253)
at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:203)
at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:290)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:92)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:161)
at com.sun.proxy.$Proxy444.reserva(Unknown Source)
... 29 more
What should I change to do the subtraction?
Upvotes: 0
Views: 120
Reputation: 338316
Your error is likely unrelated to parsing a string of a time-of-day, as the exception relates to a class in the .reflect
package.
You are using the wrong kind of class for a time-of-day. The java.util.Date
class is for a date-with-time value. You have no date.
Also you are using troublesome old legacy date-time classes supplanted years ago by the java.time classes.
For time-of-day value, use LocalTime
.
LocalTime start = LocalTime.parse( "12:34" ) ;
LocalTime stop = LocalTime.parse( "15:00" ) ;
I have no ide why your string inputs for a time-of-day would hove a Z
on the end. A Z
in date-time means UTC. And UTC has no meaning in the context of a time-of-day only value. So I will ignore the 'Z'
part of your Question.
Duration
Calculate elapsed time using Duration
.
Duration d = Duration.between( start , stop ) ;
No need to specify a Locale
when parsing a time-of-day string in standard ISO 8601 format.
A Locale
is only needed when localizing/translating where we require a human language and cultural norms.
I suspect that you should be using a date along with your time-of-day, and a time zone as well. But the Question does not explain the business problem, so I am only guessing given your naming.
Be aware that time-of-day calculations are limited to a single generic 24-hour day. You cannot go past midnight into the next day. So your stop time must always be later (greater) than your start time (unless you want a negative number result, going backwards in time).
Learning to debug is crucial to being a programmer.
When encountering a problem like this, separate out what you suspect is the cause (parsing time-of-day) into a simple little throwaway app. Get your basic functionality working before trying it in the larger scope of your complicated app (web app, servlets, web container, etc.). If your code works in the throwaway app but not the real (complicated) app, then you know the actual cause of the problem almost certainly lies elsewhere.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 2