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

S

sahm

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
 
S

Stefan Ram

sahm said:
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.
 
J

Jeff Higgins

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.

}

}
 
J

Jeff Higgins

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
 
J

Jeff Higgins

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
 
S

sahm

See Stefan for the answer.
An alternative follows.
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());

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

Roedy Green

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
 
L

Lew

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

Lew

sahm said:
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);
 
L

Lew

Lew said:
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.
 
J

Jeff Higgins

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

glen herrmannsfeldt

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

Jeff Higgins

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

Lew

glen said:
Lew wrote:
(snip)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top