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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:43:19+00:00 2026-06-16T15:43:19+00:00

In my application I need to compare two different dates given in below formats.

  • 0

In my application I need to compare two different dates given in below formats.

Inputs:
there are 2 input dates in String format.

String actual="11/12/2012 11:26:04 AM";
String expected="21/12/2012 09:49:12 AM";

I am trying to use below java code for comparision.


SimpleDateFormat format= new SimpleDateFormat("dd/MM/YYYY hh:mm:ss a");
Date date1 = format.parse(actual);
System.out.println("Formated date1 is: "+format.format(date1).toString());       
// prints  :  01/01/2012 11:26:04 AM  Why????

Date date2= format.parse(expected);
System.out.println("Formated date2 is: "+format.format(date2).toString());        
// prints :  01/01/2012 09:49:12 AM  Why????

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
if( !(cal1.compareTo(cal2)<=0))
{
 result=false;
 String errMsg +="Actual:"+actual+" date is not before or equal to expected:"+expected+" date\n";
 System.out.println(errMsg);
}

But the above code is not working as expected. please check the wrong output mentioned in comments

I think there is something wrong with the formatting.
can anyone please help me.

  • 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-16T15:43:20+00:00Added an answer on June 16, 2026 at 3:43 pm

    your format should be :

    SimpleDateFormat format= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    

    Notice year in lowercase y

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

Sidebar

Related Questions

Given two object arrays I need to compare the differences between the two (when
In my Java application i need to compare two list's element whether it is
I have a C++ application in which I need to compare two values and
I'm working on an application with two input fields that's validated in different ways
I have two android applications that I have developed. I need to compare the
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to compare two lists where each list contains about 60,000 objects. what
I'm working on an ASP.NET web application. There's a bill page which has two
I have two web applications deployed in two different weblogic servers. Application 1 is
I am developing an application in Javascript using Appcelerator and I need to compare

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.