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

The Archive Base Latest Questions

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

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class test { /* * Calculate the

  • 0
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class test {

    /*
     * Calculate the difference between two date/times   * 
     * 
     */

    private static long dateDiff(Date toDate, Date fromDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(toDate);
        long ms = cal.getTimeInMillis();
        cal.setTime(fromDate);
        ms -= cal.getTimeInMillis();
        return ms;
    }

    public static void main(String[] args) {

        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        Date d1 = null;
        Date d2 = null;

        try {
            d1 = sdf.parse("11:00:00");
            d2 = sdf.parse("10:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }

        long result = dateDiff(d1, d2);

        Date time = new Date(result);

        System.out.println(time);
    }
}

When I run it I get this result :

Thu Jan 01 02:00:00 CET 1970

I would expect 1 hour difference ?! again a problem with Timezone??

Any idea how I can fix it.

thx all

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

    I don’t know what you expect this to do, but what you are actually doing is outputting the date corresponding to one hour after midnight on Jan 1 1970, using the default timezone.

    You seem to want to Date to represent a duration (i.e. a number of seconds). It doesn’t do that, and neither will the Date formatters render a Date as a duration.


    I need the time difference between two Date fields and then put it in MySql (time format)

    For what you are trying to do, you need calculate the duration value as a long, then use the java.sql.Time(long) constructor to create a Time object. You can either serialize this object using its toString() method or use it as a parameter in a JDBC prepared statement.

    It turns out that my advice above is incorrect too.

    Your real problem is that the SQL Time type is for representing times … not durations. In fact, SQL does not have a dedicated duration type, so the best you can do is represent the duration as an integer number of seconds or milliseconds or whatever.


    (For the more general case, the Joda Time libraries are generally thought to provide the best APIs for manipulating dates, times and related temporal values. But for this simple case, the standard J2SE libraries should suffice … provided that you use them correctly.)

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

Sidebar

Related Questions

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class Test {
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
i have created synchronized arrayList like this import java.text.SimpleDateFormat; import java.util.*; class HelloThread {
/ --------------------------------------- -----------------Quizzes.java------------ ---------------------------------------- / import java.util.Scanner; import java.text.NumberFormat; public class Quizzes { public
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
package pkgPeople; import java.io.Serializable; import java.text.DecimalFormat; public class Person implements Serializable{ private String name;
import javax.swing.JOptionPane; import java.text.DecimalFormat; public class CandleLine { public static void main(String[] args) {
I am trying to calculate the difference between given two times and i find
this problem is killing me, here is my code import java.text.ParseException; import java.text.SimpleDateFormat; import
import java.io.*; public class Auto { /** * @param args * @throws IOException */

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.