Tag Archives: time

Most idiomatic way to print a time difference in Java?

Questions: I’m familiar with printing time difference in milliseconds: long time = System.currentTimeMillis(); //do something that takes some time… long completedIn = System.currentTimeMillis() – time; But, is there a nice way print a complete time in a specified format (eg: HH:MM:SS) either using Apache Commons or even the dreaded platform API’s Date/Time objects? In other… Read More »

SimpleDateFormat producing wrong date time when parsing “YYYY-MM-dd HH:mm”

Questions: I am trying to parse string (YYYY-MM-dd HH:mm) to date, however getting wrong date than expected. CODE: Date newDate = null; String dateTime = “2013-03-18 08:30”; SimpleDateFormat df = new SimpleDateFormat(“YYYY-MM-dd HH:mm”, Locale.ENGLISH); df.setLenient(false); try { newDate = df.parse(dateTime); } catch (ParseException e) { throw new InvalidInputException(“Invalid date input.”); } Produces: Sun Dec 30… Read More »

Java Runtime.exec()

Questions: I can run this command from the command line without any problem (the validation script executes): c:/Python27/python ../feedvalidator/feedvalidator/src/demo.py https://das.dynalias.org:8080/das_core/das/2.16.840.1.113883.4.349/1012581676V377802/otherAdminData/careCoordinators and from java if I leave off the URL parameter and just do: String[] args1 = {“c:/Python27/python”, “../feedvalidator/feedvalidator/src/demo.py” }; Runtime r = Runtime.getRuntime(); Process p = r.exec(args1); it works fine. If I use certain URLs… Read More »

How to stop parsing xml document with SAX at any time?

Questions: I parse a big xml document with Sax, I want to stop parsing the document when some condition establish? How to do? Answers: Create a specialization of a SAXException and throw it (you don’t have to create your own specialization but it means you can specifically catch it yourself and treat other SAXExceptions as… Read More »

How to read request.getInputStream() multiple times

Questions: I have this code: @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { logger.info(“Filter start…”); HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; String ba = getBaId(getBody(httpRequest)); if (ba == null) { logger.error(“Wrong XML”); httpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST); } else { if (!clients.containsKey(ba)) { clients.put(ba, 1); logger.info(“Client map : init…”); }… Read More »

How can I compile and deploy a java class at runtime?

Questions: I am in the process of writing a rule engine that performs simple assignments as determined by conditional constructs. It is a prerequisite of the project that the rules be in XML format. I have modeled my XML schema to resemble simple code blocks. I wish to parse the XML and to then transform… Read More »

How to get the real path of Java application at runtime?

Questions: I am creating a Java application where I am using log4j. I have given the absolute path of configuration log4j file and also an absolute path of generated log file(where this log file are generated). I can get the absolute path of a Java web application at run time via: String prefix = getServletContext().getRealPath(“/”);… Read More »

AndroidRuntime error: Parcel: unable to marshal value

Questions: I am trying to pass a HashMap to a new activity using the intent.puExtra function. Stepping through the debugger it seems that it adds the HashMap no problem, however when startActivty() is called I get a runtime error stating that Parcel: unable to marshal value com.appName.Liquor. Liquor is a custom class that I created,… Read More »

Time complexity for java ArrayList

Questions: Is ArrayList an array or a list in java? what is the time complexity for the get operation, is it O(n) or O(1)? Answers: An ArrayList in Java is a List that is backed by an array. The get(index) method is a constant time, O(1), operation. The code straight out of the Java library… Read More »