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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:35:41+00:00 2026-06-11T11:35:41+00:00

I need to be able to compare two dates, only based on the year

  • 0

I need to be able to compare two dates, only based on the year and the month (i.e. without taking notice of the day), and that in JAVA and HQL.

Let’s say I need to check if d1 is less than or equals d2. Here is what I tried:

JAVA

calendar.setTime(d1);
int y1 = calendar.get(Calendar.YEAR);
int m1 = calendar.get(Calendar.MONTH);
calendar.setTime(d2);
int y2 = calendar.get(Calendar.YEAR);
int m2 = calendar.get(Calendar.MONTH);
return y1 <= y2 && m1 <= m2;

HQL

select item from Item item
where year(item.d1) <= year(:d2)
and month(item.d1) <= month(:d2)

The algorithm is the same in the both pieces of code above, but it’s wrong:

  • 2011-10 LTE 2012-09 should return true but will return false because 2011 < 2012 but 10 !< 09

If I use a OR instead of a AND, it’s still wrong:

  • 2013-01 LTE 2012-05 should return false but will return true because 2013 !< 2012 but 01 < 05

So, how should I process? Please, I need it for JAVA and HQL.

  • 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-11T11:35:43+00:00Added an answer on June 11, 2026 at 11:35 am

    This should work.

    select item from Item item
    where year(item.d1) < year(:d2) or
         (year(item.d1) = year(:d2)
          and month(item.d1) <= month(:d2))
    

    Same for Java:

    y1 < y2 || (y1 == y2 && m1 <= m2)
    

    You could keep second check as y1 <= y2 but it would be a little redundant.

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

Sidebar

Related Questions

We need a script that will compare two directories of files and for each
I need to compare two arrays. If I test if Object1==Object2 I am testing
I need to be able to determine if two sounds are very similar. The
In .NET, I need a way to compare two files. I thought of a
I need to build a mobile application which is able to compare data in
Suppose I've got a generic MyClass<T> that needs to compare two objects of type
I need to compare strings from two text files. While separating the strings using
in my code, I need to compare two strings to see if they are
I need to compare two directories, and produce some sort of structured output (text
I need to compare two office documents, in this case two word documents and

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.