Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11334 > unrolled thread
| Started by | sahm <sahm007@gmail.com> |
|---|---|
| First post | 2012-01-14 23:44 -0800 |
| Last post | 2012-01-15 15:39 -0800 |
| Articles | 14 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | sahm <sahm007@gmail.com> |
|---|---|
| Date | 2012-01-14 23:44 -0800 |
| Subject | How can I use Operand (+ , - ) with Time |
| Message-ID | <34afdd1f-4549-48a9-878c-8678993e97ed@t30g2000vbx.googlegroups.com> |
Hi every One
I'm Try to do program to calculate the Over Time, I'm working with 24
hour not A.M. & P.M. and this is my Time format (HH:MM:SS) (00:00:00).
But how can I use operand (+, -) with Time
This is my Code
/////////////////////////////////////////////////////////////////////
import java.sql.Time;
import java.util.Date;
public class OverTimeDetailsDataClass {
String Emp_ID;
String OverTime_Doc_NO;
String OverTime_Date;
Time OverTime_Start_Time;
Time OverTime_End_Time;
Time OverTimeTotalHours;
OverTime_Start_Time =
Time.valueOf(OverTimeFromjTextField.getText());
OverTime_End_Time = Time.valueOf(OverTimeTOjTextField.getText());
OverTimeTotalHours = OverTime_End_Time - OverTime_Start_Time;
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Best
Salim
[toc] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2012-01-15 08:32 -0500 |
| Message-ID | <jeuk41$pq$1@dont-email.me> |
| In reply to | #11334 |
On 01/15/2012 02:44 AM, sahm wrote:
> Hi every One
>
> I'm Try to do program to calculate the Over Time, I'm working with 24
> hour not A.M.& P.M. and this is my Time format (HH:MM:SS) (00:00:00).
> But how can I use operand (+, -) with Time
See Stefan for the answer.
An alternative follows.
> This is my Code
>
> /////////////////////////////////////////////////////////////////////
> import java.sql.Time;
> import java.util.Date;
>
>
> public class OverTimeDetailsDataClass {
>
> String Emp_ID;
> String OverTime_Doc_NO;
> String OverTime_Date;
> Time OverTime_Start_Time;
> Time OverTime_End_Time;
> Time OverTimeTotalHours;
>
> OverTime_Start_Time =
> Time.valueOf(OverTimeFromjTextField.getText());
> OverTime_End_Time = Time.valueOf(OverTimeTOjTextField.getText());
> OverTimeTotalHours = OverTime_End_Time - OverTime_Start_Time;
> }
import java.sql.Time;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import javax.swing.JTextField;
public class OT {
public static void main(String[] args) {
JTextField OverTimeFromjTextField = null;
JTextField OverTimeTOjTextField = null;
Time OverTime_Start_Time;
Time OverTime_End_Time;
// Be aware: read the documentation for java.util.Calendar
TimeZone timeZone = TimeZone.getDefault();
Locale locale = Locale.getDefault();
Calendar startTime = Calendar.getInstance(timeZone,locale);
Calendar endTime = Calendar.getInstance(timeZone,locale);
OverTime_Start_Time =
Time.valueOf(OverTimeFromjTextField.getText());
OverTime_End_Time = Time.valueOf(OverTimeTOjTextField.getText());
startTime.setTime(OverTime_Start_Time);
endTime.setTime(OverTime_End_Time);
// use the java.util.Calendar.add(int field, int amount) method
// as described in the documentation.
}
}
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2012-01-15 08:52 -0500 |
| Message-ID | <jeul8g$71g$1@dont-email.me> |
| In reply to | #11338 |
On 01/15/2012 08:32 AM, Jeff Higgins wrote: > On 01/15/2012 02:44 AM, sahm wrote: >> Hi every One > > startTime.setTime(OverTime_Start_Time); > endTime.setTime(OverTime_End_Time); // setup any other required parameters here // see the documentation for java.util.Calendar > > // use the java.util.Calendar.add(int field, int amount) method > // as described in the documentation. // or perhaps the java.util.Calendar.roll(**) methods - see the docs
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2012-01-15 10:19 -0500 |
| Message-ID | <jeuqcs$32i$1@dont-email.me> |
| In reply to | #11338 |
On 01/15/2012 08:32 AM, Jeff Higgins wrote:
> On 01/15/2012 02:44 AM, sahm wrote:
>> Hi every One
>>
>> I'm Try to do program to calculate the Over Time, I'm working with 24
>> hour not A.M.& P.M. and this is my Time format (HH:MM:SS) (00:00:00).
>> But how can I use operand (+, -) with Time
>
> See Stefan for the answer.
> An alternative follows.
>
>> This is my Code
>>
>> /////////////////////////////////////////////////////////////////////
>> import java.sql.Time;
>> import java.util.Date;
>>
>>
>> public class OverTimeDetailsDataClass {
>>
>> String Emp_ID;
>> String OverTime_Doc_NO;
>> String OverTime_Date;
>> Time OverTime_Start_Time;
>> Time OverTime_End_Time;
>> Time OverTimeTotalHours;
>>
>> OverTime_Start_Time =
>> Time.valueOf(OverTimeFromjTextField.getText());
>> OverTime_End_Time = Time.valueOf(OverTimeTOjTextField.getText());
>> OverTimeTotalHours = OverTime_End_Time - OverTime_Start_Time;
>> }
>
> import java.sql.Time;
> import java.util.Calendar;
> import java.util.Locale;
> import java.util.TimeZone;
>
> import javax.swing.JTextField;
>
>
> public class OT {
>
> public static void main(String[] args) {
>
> JTextField OverTimeFromjTextField = null;
> JTextField OverTimeTOjTextField = null;
// The abstract class java.text.DateFormat
// and it's concrete java.text.SimpleDateFormat
// are good for getting/setting formatted input/output
>
> Time OverTime_Start_Time;
> Time OverTime_End_Time;
// Unless you are using the JDBC API
// you should avoid using the java.sql classes:
// java.sql.Date, java.sql.Time, java.sql.Timestamp
// in favor of java.util.Date, java.util.Calendar,
// java.util.GregorianCalendar, java.util.Locale,
// java.util.TimeZone, java.util.SimpleTimeZone
>
> // Be aware: read the documentation for java.util.Calendar
> TimeZone timeZone = TimeZone.getDefault();
> Locale locale = Locale.getDefault();
>
> Calendar startTime = Calendar.getInstance(timeZone,locale);
> Calendar endTime = Calendar.getInstance(timeZone,locale);
>
> OverTime_Start_Time =
> Time.valueOf(OverTimeFromjTextField.getText());
> OverTime_End_Time = Time.valueOf(OverTimeTOjTextField.getText());
>
> startTime.setTime(OverTime_Start_Time);
> endTime.setTime(OverTime_End_Time);
>
> // use the java.util.Calendar.add(int field, int amount) method
> // as described in the documentation.
>
> }
>
> }
>
>
[toc] | [prev] | [next] | [standalone]
| From | sahm <sahm007@gmail.com> |
|---|---|
| Date | 2012-01-15 07:55 -0800 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <4887cdd4-6a2e-49ac-9621-b594634fbf97@3g2000pbg.googlegroups.com> |
| In reply to | #11342 |
On Jan 15, 6:19 pm, Jeff Higgins <j...@invalid.invalid> wrote:
> On 01/15/2012 08:32 AM, Jeff Higgins wrote:
>
>
>
>
>
>
>
>
>
> > On 01/15/2012 02:44 AM, sahm wrote:
> >> Hi every One
>
> >> I'm Try to do program to calculate the Over Time, I'm working with 24
> >> hour not A.M.& P.M. and this is my Time format (HH:MM:SS) (00:00:00).
> >> But how can I use operand (+, -) with Time
>
> > See Stefan for the answer.
> > An alternative follows.
>
> >> This is my Code
>
> >> /////////////////////////////////////////////////////////////////////
> >> import java.sql.Time;
> >> import java.util.Date;
>
> >> public class OverTimeDetailsDataClass {
>
> >> String Emp_ID;
> >> String OverTime_Doc_NO;
> >> String OverTime_Date;
> >> Time OverTime_Start_Time;
> >> Time OverTime_End_Time;
> >> Time OverTimeTotalHours;
>
> >> OverTime_Start_Time =
> >> Time.valueOf(OverTimeFromjTextField.getText());
> >> OverTime_End_Time = Time.valueOf(OverTimeTOjTextField.getText());
> >> OverTimeTotalHours = OverTime_End_Time - OverTime_Start_Time;
> >> }
>
> > import java.sql.Time;
> > import java.util.Calendar;
> > import java.util.Locale;
> > import java.util.TimeZone;
>
> > import javax.swing.JTextField;
>
> > public class OT {
>
> > public static void main(String[] args) {
>
> > JTextField OverTimeFromjTextField = null;
> > JTextField OverTimeTOjTextField = null;
>
> // The abstract class java.text.DateFormat
> // and it's concrete java.text.SimpleDateFormat
> // are good for getting/setting formatted input/output
>
>
>
> > Time OverTime_Start_Time;
> > Time OverTime_End_Time;
>
> // Unless you are using the JDBC API
> // you should avoid using the java.sql classes:
> // java.sql.Date, java.sql.Time, java.sql.Timestamp
>
> // in favor of java.util.Date, java.util.Calendar,
> // java.util.GregorianCalendar, java.util.Locale,
> // java.util.TimeZone, java.util.SimpleTimeZone
>
>
>
>
>
>
>
>
>
> > // Be aware: read the documentation for java.util.Calendar
> > TimeZone timeZone = TimeZone.getDefault();
> > Locale locale = Locale.getDefault();
>
> > Calendar startTime = Calendar.getInstance(timeZone,locale);
> > Calendar endTime = Calendar.getInstance(timeZone,locale);
>
> > OverTime_Start_Time =
> > Time.valueOf(OverTimeFromjTextField.getText());
> > OverTime_End_Time = Time.valueOf(OverTimeTOjTextField.getText());
>
> > startTime.setTime(OverTime_Start_Time);
> > endTime.setTime(OverTime_End_Time);
>
> > // use the java.util.Calendar.add(int field, int amount) method
> > // as described in the documentation.
>
> > }
>
> > }
Thank you every one
I fix the problem with simple function
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Time getTotalOverTime(Time StrtTime, Time EndTime)
{
Time TotalHours = null;
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;
}
////////////////////////////////////////
Best
Salim
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-01-15 10:50 -0800 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <jev752$hdh$1@news.albasani.net> |
| In reply to | #11344 |
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
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-01-15 08:35 -0800 |
| Message-ID | <htv5h71nv7jpom3b3gga51vnb6uuadjhqt@4ax.com> |
| In reply to | #11334 |
On Sat, 14 Jan 2012 23:44:33 -0800 (PST), sahm <sahm007@gmail.com> wrote, quoted or indirectly quoted someone who said : > >I'm Try to do program to calculate the Over Time, I'm working with 24 >hour not A.M. & P.M. and this is my Time format (HH:MM:SS) (00:00:00). >But how can I use operand (+, -) with Time >This is my Code You need to do your calculations in milliseconds since 1970. Then you can simply subtract to find intervals. Then your questions becomes how to I convert local time to and from internal format. See http://mindprod.com/jgloss/calendar.html http://mindprod.com/jgloss/date.html http://mindprod.com/jgloss/time.html http://mindprod.com/jgloss/timezone.html -- Roedy Green Canadian Mind Products http://mindprod.com One of the most useful comments you can put in a program is "If you change this, remember to change ?XXX? too".
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-01-15 10:31 -0800 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <jev61p$eh7$1@news.albasani.net> |
| In reply to | #11334 |
On 01/15/2012 04:51 AM, Stefan Ram wrote: > sahm<sahm007@gmail.com> writes: >> But how can I use operand (+, -) with Time > > You need to find or implement a compiler or interpreter for > a language that supports operator overloading (Java does not). > > Or, convert all times into values of the primitive Java data > type »double« (such as a count of seconds) and then used »+« > with those double values. That tends to cause trouble. It's better to use 'java.util.Calendar' and its friends prior to Java 7, and the new time types for 7+. The application apparently tracks working hours. How much overtime does someone in New York get who works eight hours nornmally but works from midnight to eight a.m. on November 4, 2012? Naive calculations based on double or int values for number of seconds in an hour will give you the wrong answer. To get the right answer across all possible dates and locales (e.g., March 25, 2012, in London, UK), you will need all kinds of complicated calculation. Heck, just to get the right number of days in February (say, year 2100) requires some dancing. Tou will note that the OP's example committed this error. Why in the heck would you recommend 'double' as a time, date or interval type? It's very inappropriate. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-01-15 10:51 -0800 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <jev77l$hdh$2@news.albasani.net> |
| In reply to | #11356 |
Lew wrote: > Why in the heck would you recommend 'double' as a time, date or interval type? > It's very inappropriate. I take that back, slightly. 'double' is OK as an interval type if you lack better types. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2012-01-15 14:45 -0500 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <jev9tv$4sf$1@dont-email.me> |
| In reply to | #11356 |
On 01/15/2012 01:31 PM, Lew wrote: [snip] > > That tends to cause trouble. It's better to use 'java.util.Calendar' and > its friends prior to Java 7, and the new time types for 7+. > I've scanned the Release Notes and API documentation for Oracle's Java SE 7 and have been unable to find "new time types for 7+". Will you provide pointers for me? Thanks. [snip]
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2012-01-15 15:53 -0500 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <jevdt4$u3j$1@dont-email.me> |
| In reply to | #11363 |
On 01/15/2012 02:45 PM, Jeff Higgins wrote: > On 01/15/2012 01:31 PM, Lew wrote: > [snip] >> >> That tends to cause trouble. It's better to use 'java.util.Calendar' and >> its friends prior to Java 7, and the new time types for 7+. >> > I've scanned the Release Notes and API documentation for Oracle's Java > SE 7 and have been unable to find "new time types for 7+". Will you > provide pointers for me? > Thanks. After some more digging I find JSR 310: Date and Time API. I seems to be in the Early Draft Review stage. <http://jcp.org/en/jsr/summary?id=310> <http://sourceforge.net/apps/mediawiki/threeten/index.php?title=ThreeTen>
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-01-15 15:36 -0800 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <jevnun$pr8$1@news.albasani.net> |
| In reply to | #11363 |
Jeff Higgins wrote: > Lew wrote: >> That tends to cause trouble. It's better to use 'java.util.Calendar' and >> its friends prior to Java 7, and the new time types for 7+. >> > I've scanned the Release Notes and API documentation for Oracle's Java SE 7 > and have been unable to find "new time types for 7+". Will you provide > pointers for me? You're right, I'm wrong. I spoke of JSR 310 which was originally slated for Java 7 and didn't make it in. So it's back to Calendar and numeric intervals. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2012-01-15 20:07 +0000 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <jevbm7$14t$1@speranza.aioe.org> |
| In reply to | #11356 |
Lew <noone@lewscanon.com> wrote: (snip) > Why in the heck would you recommend 'double' as a time, date or interval type? > It's very inappropriate. With an available 64 bit integer type, I agree. Use long, not double. -- glen
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-01-15 15:39 -0800 |
| Subject | Re: How can I use Operand (+ , - ) with Time |
| Message-ID | <jevo2t$pr8$2@news.albasani.net> |
| In reply to | #11364 |
glen herrmannsfeldt wrote: > Lew wrote: > (snip) >> Why in the heck would you recommend 'double' as a time, date or interval type? >> It's very inappropriate. > > With an available 64 bit integer type, I agree. Use long, not double. That might work for intervals, but not dates or times. 'double' is just fine for intervals. 'long' is not at all fine for dates or times. I outlined the reasons upthread, which apparently you ignored, but the bottom line is that you have to reinvent 'java.util.Calendar' if you use 'long' to represent date/time types, and it's not typesafe. Really bad idea. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web