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 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

Related Questions

The following code has been created from a different post, but I now have
I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
Can anyone tell how correct the following code below. Iam tryin to create a
I have the following code in SQL (2005) which calculates the avarage user logins
I need to solve the following question which i can't get to work by
I have the following code to create a UIPickerView: pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f,
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically
I use the following code to create countdowns in Javascript. n is the number
I am currently using the following code to create a web request: Dim myRequest

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.