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 8384509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:25:19+00:00 2026-06-09T17:25:19+00:00

I am trying to set the year of a java.util.Date . he time stamp

  • 0

I am trying to set the year of a java.util.Date.

he time stamp I need to parse does not include the year so I did this:

private static final SimpleDateFormat logTimeStampFormat = 
    new SimpleDateFormat("MMM dd HH:mm:ss.SSS");

boolean isAfterRefDate (String line, Date refDate) {        
    try {
        Date logTimeStamp = logTimeStampFormat.parse(line);
        logTimeStamp.setYear(2012);      // But this is deprecated!
        return logTimeStamp.after(refDate);
    } catch (ParseException e) {
        // Handle exception
    }        
}

To avoid using a deprecated method, I do like this:

private static final SimpleDateFormat logTimeStampFormat = 
    new SimpleDateFormat("MMM dd HH:mm:ss.SSS");

private static Calendar cal = Calendar.getInstance();

boolean isAfterRefDate (String line, Date refDate) {
    try {
        Date logTimeStamp = logTimeStampFormat.parse(line);
        cal.setTime(logTimeStamp);
        cal.set(Calendar.YEAR, 2012);
        logTimeStamp = cal.getTime();            
        return logTimeStamp.after(refDate);
    } catch (ParseException e) {
        // Handle exception
    }        
}

I just don’t think that this is the best way to solve this problem. I have to first set the calendar object properly and then get the date object back from it while earlier I could just modify the date object directly.

Can someone suggest a better approach?

  • 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-09T17:25:21+00:00Added an answer on June 9, 2026 at 5:25 pm

    Can someone suggest a better approach.

    Sure – try to avoid using Date and Calendar in the first place. Use Joda Time instead, which is much better.

    Setting the year on a Date is an inherently ambiguous operation – what time zone is this year meant to be? What would you expect to happen if you’re setting a year of 2013 on a previous date of February 29th 2012?

    Using Joda Time will make your code much clearer in terms of what kind of data you’re really expecting. You can always convert to/from Date and Calendar at API boundaries if you really need to.

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

Sidebar

Related Questions

Trying to get Oracle to Get a date set it to year only and
I'm trying to set the date of another datepicker exactly 1 year on from
I'm trying to set an alarm for a particular date and time but so
Trying to set a flag in the post to allow processing or not. The
When trying to set DoMatchParen I get the following error: E492: Not an editor
Currently I am trying to get the date (Month and Year specifically) and display
In my Android Application , I am trying to convert Date/Time to Milliseconds, check
I need to set a max year on the DatePickerDialog that I am using
I am trying to write some code to correctly set an expiration date given
Hi all I have been trying to fix this but could not fix, the

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.