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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:36:02+00:00 2026-05-20T08:36:02+00:00

I am trying to calculate the difference between given two times and i find

  • 0

I am trying to calculate the difference between given two times and i find some problem in it.
Suppose we have times value are “11:AM” and 10:00 AM”.
I am able to calculate but there is a bit of confusion in AM ,PM.
Thanks in advance

I am using following code:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate {

public static String timeDiff(String time1, String time2) {
    String Time = null;
    int difference;
    String a1 = time1.substring(6);
    String a2 = time2.substring(6);

    try {
        // Define a date format the same as the expected input
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm a");
        // Get time values of the date objects
        long l1 = sdf.parse(time1).getTime();
        long l2 = sdf.parse(time2).getTime();
        difference = (int) (l2 - l1);
        difference = (difference < 0 ? -difference : difference) / 60000;
        if (difference > 60) {
            int Hour = difference / 60;
            int Mins = difference % 60;
            if (Mins == 0)
                Time = Hour + " Hours";
            else
                Time = Hour + " Hours" + " " + Mins + " Mins";
        } else {
            Time = difference + " " + "Mins";
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return Time;
}

public static void main(String[] args) {
    String diff = StringToDate.timeDiff("10:00 AM", "01:00 PM");
    System.out.println("Difference: " + diff);
}

}

  • 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-20T08:36:03+00:00Added an answer on May 20, 2026 at 8:36 am

    You can get date objects for both start & end time.
    Then get time in millis & subtract.

    Difference in Millis = endDate.getTime() – startDate.getTime()

    Convert the difference to hours/minutes.

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

Sidebar

Related Questions

Possible Duplicate: how to calculate difference between two dates using java I'm trying something
I'm trying to solve a problem (in php, but programming language doesn't matter). I'm
I'm trying to prepare data for a graph using LINQ. The problem that i
I'm trying to switch from two-stage rotation to one-stage rotation (to avoid the console
I am trying to calculate the correct mouse coordinates, in user coordinate space, in
I'm trying to convert spherical coordinates (namely latitude and longitude from a GPS device)
I've been trying to get this Excel function working correctly, and I've hit a
I am trying to create a UDF that does 2 different things depending on
I have a symbol originally created at [-100,-100] coordinate (left top corner). Now I
I've got a little objective-c utility program that renders a convex hull. (This is

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.