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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:41:25+00:00 2026-05-11T03:41:25+00:00

I created the following code to calculate the duration between two timestamps which can

  • 0

I created the following code to calculate the duration between two timestamps which can come in two different formats:

public class dummyTime { public static void main(String[] args) {     try {         convertDuration('2008-01-01 01:00 pm - 01:56 pm');         convertDuration('2008-01-01 8:30 pm - 2008-01-02 09:30 am');     } catch (Exception e) {         e.printStackTrace();     } }  private static String convertDuration(String time) throws Exception {     String ts[] = time.split(' - ');     SimpleDateFormat formatNew = new SimpleDateFormat('HH:mm');     Date beg, end;     String duration = null;      beg = getDateTime(ts[0]);     end = getDateTime(ts[1], beg);      duration = formatNew.format(end.getTime() - beg.getTime());     System.out.println(duration + ' /// ' + time + ' /// ' + beg + ' /// '             + end);      return duration; }  private static Date getDateTime(String dateTime) throws ParseException {     DateFormat formatOldDateTime = new SimpleDateFormat(             'yyyy-MM-dd hh:mm aa');     DateFormat formatOldTimeOnly = new SimpleDateFormat('hh:mm aa');     Date date = null;      try {         date = formatOldDateTime.parse(dateTime);     } catch (ParseException e) {         date = formatOldTimeOnly.parse(dateTime);     }      return date; }  private static Date getDateTime(String dateTime, Date orig)         throws ParseException {     Date end = getDateTime(dateTime);      if (end.getYear() == 70) {         end.setYear(orig.getYear());         end.setMonth(orig.getMonth());         end.setDate(orig.getDate());     }      return end; } } 

The output it generates is:

01:56 /// 2008-01-01 01:00 pm - 01:56 pm /// Tue Jan 01 13:00:00 CET 2008 /// Tue Jan 01 13:56:00 CET 2008 14:00 /// 2008-01-01 8:30 pm - 2008-01-02 09:30 am /// Tue Jan 01 20:30:00 CET 2008 /// Wed Jan 02 09:30:00 CET 2008 

My questions are:

  1. Why are the results always wrong (always +1h)?
  2. What is a better way to identify timestamps without day? == 70 doesn’t look good and the getDay & setDay functions are deprecated too.

Many many thanks, this issue has been driving me crazy for several hours.

  • 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. 2026-05-11T03:41:25+00:00Added an answer on May 11, 2026 at 3:41 am
    1. At my computer this is off by 2 hours, because I’m at GMT+2, and you’re probably at GMT+1. Note that formatNew.format(end.getTime() - beg.getTime()); receives date, i.e. treats your 56 minutes as 1970-01-01-00:56:00 GMT+1. To fix this quickly, call formatNew.setTimeZone( TimeZone.getTimeZone( 'GMT' ) );

    2. For the 2nd item, you can check if format-yyyy-MM-dd failed (you catch a parse error), and this is how you know that there’s no year.

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

Sidebar

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm afraid there's no syntactical sugar for nullable types in… May 11, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer Postgres doesn't use threads. Each connection is handled by a… May 11, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer The trick is to use the CURRVAL of the sequence.… May 11, 2026 at 7:26 pm

Related Questions

I am attempting to programmatically monitor the size of a SQL Server database, so
I'm teaching/helping a student to program. I remember the following process always helped me
I am completely perplexed with how my Queue is function. I am attempting (and
I'm doing a raytracer hobby project, and originally I was using structs for my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.