Results 1 to 4 of 4
  1. #1
    soichi is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default Clear the Date object for cron

    Hi.

    I am developing a servlet that generates the date of its implementations.
    This servlet works regularly with cron. And I hope that the date and time of implementation gets updated at every time. But it does not.

    For example,

    Java Code:
    public class GetCalendarServlet extends HttpServlet {
    
     Date today = new Date();
     static SimpleDateFormat df = new SimpleDateFormat("MMM/d(EEE)HH:mm");
     String todaySt = df.format(today);
    
    }
    Once the Date object is generated, the servlet seems hold the data for every cron implementation.
    Is there anyway to update it ?

    Thanks in advance.

    soichi

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Sure, because a Servlet class is loaded the first time a request is made to the servlet. That is the same instance of the object is used for every subsequent request thread until the application is reloaded, which also implies the servlet must be thread safe too right.

    So instead of having the today property a member of the servlet class, create a method to retrieve the current date each time it is invoked, such as ,

    Java Code:
    public class GetCalendarServlet extends HttpServlet {
    
     static SimpleDateFormat df = new SimpleDateFormat("MMM/d(EEE)HH:mm");
    
     /**
      * Gets the current date each time invoked.
      * @return the date now as a formatted string
      */
     protected String getTodayString() {
       return df.format(new Date());
      }
    
    /** Sample invocation of the getTodayString() */
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String todayString = getTodayString();
      }
    
    }

  3. #3
    soichi is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Thanks. It is working as I wished.

    Thanks again.

    soichi

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Oh.
    Do not use a static SimpleDateFormat.
    It is not threadsafe.

    Now, in this case it may not matter so much, but you may find the minutes being out should two threads hit the formatter at just the right time.

Similar Threads

  1. Calendar object? - Getting date from user
    By hiei_yasha in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 04:58 AM
  2. cron expression
    By enggvijaysingh@gmail.com in forum Advanced Java
    Replies: 6
    Last Post: 11-23-2010, 09:18 PM
  3. Problem with Date Object
    By R O C K Y in forum Advanced Java
    Replies: 4
    Last Post: 02-15-2009, 05:37 PM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 09:32 PM
  5. how to clear the data of an object
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 11:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •