Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:06:44+00:00 2026-06-02T11:06:44+00:00

In my servlet, when a request comes in, I create a date object. Date

  • 0

In my servlet, when a request comes in, I create a date object.

Date now = new Date();

I want to perform a cache lookup, and I need to create a helper function that will format the date to:

2012.04.24-10:34

which is: yyyy-MM-dd,HH:mm

I had this using simple date format:

public class DateHelpers {
      public static final DateFormat minuteDateFormat = new SimpleDateFormat("yyyy-MM-dd,HH:mm", Locale.US);

      public static String getDateKey(Date now) {
         // use minuteDateFormat to format date
      }
}

But I was reading that simple date format isn’t thread-safe.

I am not parsing a string to a dateformat, so maybe there is a better and faster way to do this?

I am taking a date object and formatting it to a string, and it must be thread safe as many requests will be using this DateHelper static method to get the key from the date.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:06:46+00:00Added an answer on June 2, 2026 at 11:06 am

    Taking a step back, if you are really pressed for performance, I would avoid formatting the date at all and just use the unix timestamp for key (i.e. System.currentTimeMllis()). If you want to lookup based on existing date object date.getTime() gives you the timestamp.

    If you want to round it to a minute, you can int-divide and multiply by 60,000 – it’s way faster and guaranteed threadsafe.

    For the cases when I format dates, I ususally either create the date format on demand (it is not that expensive), or I have an pool of utility objects which I explicitly associate with each worker.

    The thread local solution will work, but over time it is too easy to lose track of all the TL’s and you end up with code that is more difficult to debug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to intercept a request in a filter/servlet and add a few parameters
I want to send a request to another server from my servlet and get
I want to create a servlet that will allow me to upload image files
I want to create a servlet method like below. In this method I want
I am trying to dispatch in a servlet request handler to the JSP processor
I have a Filter Servlet that filters request from a Servlet. I do not
1)My environment is web application, I develop servlet to receive request. A) In some
Possible Duplicate: Where are the request method constants in the Servlet API? I'm looking
I am making an ajax request to my servlet and the servelt responds with
In the servlet 2.4 spec you can define dispatchers in the filter config [REQUEST

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.