Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11357

Re: How can I use Operand (+ , - ) with Time

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: How can I use Operand (+ , - ) with Time
Date 2012-01-15 10:50 -0800
Organization albasani.net
Message-ID <jev752$hdh$1@news.albasani.net> (permalink)
References <34afdd1f-4549-48a9-878c-8678993e97ed@t30g2000vbx.googlegroups.com> <jeuk41$pq$1@dont-email.me> <jeuqcs$32i$1@dont-email.me> <4887cdd4-6a2e-49ac-9621-b594634fbf97@3g2000pbg.googlegroups.com>

Show all headers | View raw


sahm wrote:
> Thank you every one
>
> I fix the problem with simple function

It is neither simple nor correct.  It doesn't handle most date formats, it 
doesn't handle time zones, it doesn't handle Daylight Saving, it doesn't 
handle shifts that cross a midnight boundary.  Weirdly, it doesn't use *any* 
of the standard date or time types, which _would_ have been simple and _could_ 
have been correct.

Your variable names violate the Java coding conventions.  So does your brace 
indentation.

You show 'Time' as a type, but not its package. This will confuse anyone who 
thinks you mean the standard 'Time' class.

I would never let this code past a code review.  There is very little right 
about it.

> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
> Time getTotalOverTime(Time StrtTime, Time EndTime)

Why isn't this method 'public'?

>      {
>          Time TotalHours = null;

Why do you initialize this variable to 'null'?  The value is never used. 
Also, you should declare variables close to the point of use, not all at the 
top, and could you have invented more obscure, hard-to-interpret variable 
names?  I think not, save you used obfuscatory underscores throughout the names.

>          String ST, ET, TH;
>          int stH, stM, stS, etH, etM, etS, ttH, ttM, ttS;
>
>          ST = String.valueOf(StrtTime);
>          ET = String.valueOf(EndTime);
>
>          stH = Integer.parseInt(ST.substring(0, 2));
>          stM = Integer.parseInt(ST.substring(3, 5));
>          stS = Integer.parseInt(ST.substring(6, 8));
>
>          etH = Integer.parseInt(ET.substring(0, 2));
>          etM = Integer.parseInt(ET.substring(3, 5));
>          etS = Integer.parseInt(ET.substring(6, 8));
>
>          ttS = etS - stS;
>          if(ttS<  0)
>          {
>              ttS =+ 60;
>              etM =- 1;
>          }
>
>          ttM = etM - stM;
>          if(ttM<  0)
>          {
>              ttM =+ 60;
>              etH =- 1;
>          }
>
>          ttH = etH - stH;
>
>          TH = String.valueOf(ttH) +":"+ String.valueOf(ttM) +":"+
> String.valueOf(ttS);
>
>          TotalHours = Time.valueOf(TH);
>
>          return TotalHours;
>      }

Try again, using 'java.util.Calendar' and 'java.text.DateFormat' and their 
kin.  The code that does the interval calculation should not use 'String' or 
in any part of the interval calculation; that's too many purposes for one 
routine. There should be absofrickinlutely no parsing left to do by the time 
you calculate intervals.

Try making the method signature (for pre-Java 7 code, without the Joda library):

  /**
   * Calculates the interval in hours between two times.
   *
   * @param start Calendar start time of interval
   * @param finish Calendar finish time of interval
   * @return double the interval between the times in hours
   */
  public double interval(Calendar start, Calendar finish);

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can I use Operand (+ , - )  with Time sahm <sahm007@gmail.com> - 2012-01-14 23:44 -0800
  Re: How can I use Operand (+ , - )  with Time Jeff Higgins <jeff@invalid.invalid> - 2012-01-15 08:32 -0500
    Re: How can I use Operand (+ , - )  with Time Jeff Higgins <jeff@invalid.invalid> - 2012-01-15 08:52 -0500
    Re: How can I use Operand (+ , - )  with Time Jeff Higgins <jeff@invalid.invalid> - 2012-01-15 10:19 -0500
      Re: How can I use Operand (+ , - ) with Time sahm <sahm007@gmail.com> - 2012-01-15 07:55 -0800
        Re: How can I use Operand (+ , - ) with Time Lew <noone@lewscanon.com> - 2012-01-15 10:50 -0800
  Re: How can I use Operand (+ , - )  with Time Roedy Green <see_website@mindprod.com.invalid> - 2012-01-15 08:35 -0800
  Re: How can I use Operand (+ , - ) with Time Lew <noone@lewscanon.com> - 2012-01-15 10:31 -0800
    Re: How can I use Operand (+ , - ) with Time Lew <noone@lewscanon.com> - 2012-01-15 10:51 -0800
    Re: How can I use Operand (+ , - ) with Time Jeff Higgins <jeff@invalid.invalid> - 2012-01-15 14:45 -0500
      Re: How can I use Operand (+ , - ) with Time Jeff Higgins <jeff@invalid.invalid> - 2012-01-15 15:53 -0500
      Re: How can I use Operand (+ , - ) with Time Lew <noone@lewscanon.com> - 2012-01-15 15:36 -0800
    Re: How can I use Operand (+ , - ) with Time glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-01-15 20:07 +0000
      Re: How can I use Operand (+ , - ) with Time Lew <noone@lewscanon.com> - 2012-01-15 15:39 -0800

csiph-web