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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:16:58+00:00 2026-05-22T03:16:58+00:00

I subtract two dates from each other because I need the time difference between

  • 0

I subtract two dates from each other because I need the time difference between them. I toggle the usual problems but when I write it to MySQL (Time) I see there is always 1 hour too much difference. If I expect 2 min difference I get 1 h 2 min. Now I think it’s a timezone problem but is there a way to solve this … ?

Date totalDate = visit.getTotalTime(); 
Date visitDate = visit.getVisitTime(); 
java.sql.Time sqlTotalTime = new java.sql.Time(totalDate.getTime() - visitDate.getTime()); 
ps.setTime(4, sqlTotalTime); 

gives 1h 2 sec difference and not 2 sec:

08/Dec/2010:07:27:54 
08/Dec/2010:07:27:52 

Thank you.

  • 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-22T03:16:59+00:00Added an answer on May 22, 2026 at 3:16 am

    You’re not explaining how exactly you are subtracting the two dates or what “the usual problems” are according to you.

    To get the time difference between two dates, you can call getTime() on both of them to get the number of milliseconds since 01-01-1970 00:00:00 GMT and then subtract these two values:

    long differenceInMilliseconds = date1.getTime() - date2.getTime();
    

    Ofcourse, if you want the difference in seconds, minutes, hours etc. then divide this value by the appropriate factor.

    Note that a java.util.Date object does by itself not hold any information about time zones.

    edit (after your edit) It looks like you are trying to store the time difference in a column of type TIMESTAMP or something similar. Don’t do that; TIMESTAMP etc. are meant for time values, not time difference values. Just store the time difference in a numeric column.

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

Sidebar

Related Questions

i need to get a difference of two dates ,one date from a table
how to find whether the sunday falls between two dates.if sunday exist subtract one
I want to subtract between two date time values using SQL in MySQL such
So, I am trying to calculate the time between two dates that fits certain
I have two time values on a page: 8:00 22:30 I need to subtract
Possible Duplicate: how to calculate difference between two dates using java I'm trying something
I want to display the elapsed time between two dates in a string. Let's
To get TimeSpan in minutes from given two Dates I am doing the following
How to subtract two time values in SQL Server 2008. I am using time
How do I subtract 30 days from a date in PowerBuilder? I have 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.