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

  • Home
  • SEARCH
  • 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 3316382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:27:46+00:00 2026-05-17T22:27:46+00:00

import java.io.*; public class testing { public static void main(String a[]) throws Exception{ Date

  • 0
import java.io.*;

public class testing {
    public static void main(String a[]) throws Exception{
        Date d1=new Date();
        Thread.sleep(2000);
        Date d2=new Date();
        if(d1.equals(d2)){
            System.out.println("Both equal");
        }else{
            System.out.println("Both not equal");
        }
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        c1.clear(Calendar.HOUR);
        c1.clear(Calendar.MINUTE);
        c1.clear(Calendar.SECOND);
        c2.clear(Calendar.HOUR);
        c2.clear(Calendar.MINUTE);
        c2.clear(Calendar.SECOND);
        if(c2.compareTo(c1) == 0){
            System.out.println("Cal Equal");
        }else {
            System.out.println("Cal Not Equal");
        }
    }
}

When I run the above code multiple times of which each time (for printing if conditions of date) ‘Both not equal’ is printed but (for if condition of Calendar) sometimes it prints ‘Cal Equal’ and sometimes ‘Cal Not Equal’. Can anyone please explain me why this is so?

The main reason I was trying this because I want to compare two dates. Both have same day, month and Year but different time of the day when the objects were created. I want them to be compared equal(same). How should I do this?

  • 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-05-17T22:27:47+00:00Added an answer on May 17, 2026 at 10:27 pm

    Truncate the Milliseconds field

    Calendars have milliseconds, too. Add this:

    c1.clear(Calendar.MILLISECOND);
    c2.clear(Calendar.MILLISECOND);
    

    But it’s easier to achieve that functionality using DateUtils.truncate() from Apache Commons / Lang

    c1 = DateUtils.truncate(c1, Calendar.DATE);
    c2 = DateUtils.truncate(c2, Calendar.DATE);
    

    This removes all hours, minutes, seconds and milliseconds.

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

Sidebar

Related Questions

import java.io.*; public class TerminateWhen { public static void main(String args[]) throws IOException {
import java.io.*; public class Auto { /** * @param args * @throws IOException */
import java.util.*; import org.directwebremoting.util.Logger; public class People { public People() { people = new
I am new to Java and I am attempting to use JDBC to connect
I'm looking for a simple method of converting between java.util.Date and javax.xml.datatype.XMLGregorianCalendar in both
I am using tomcat 6 on windows. Here is the code I am testing.
I am trying to create 10.000 hexagon connected each other like bee combs.I want
I develop a web application for Tomcat 6, and I'm face to face with
I need to pass an array of objects to a PL-SQL function My ORACLE
I realize, if I didn't not include line jComboBox1.addItem(Cause Text Field To Override); When

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.