06:03:00 - 06:00:00 = 12:03:00 WHY ?

A

alomrani

Hi all ,
I'd really appreciate an insight on this ..

i am trying to subtract two date values

dateS = "06:03:00"
dateE = "06:00:00"

SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

java.util.Date dateStart = timeFormat.parse(dateS);
java.util.Date dateEnd = timeFormat.parse(dateE);

long diff = dateStart.getTime() - dateEnd.getTime();
return timeFormat.format(new Date(diff));


06:03:00 - 06:00:00 = 12:03:00 WHY ? I want my answer to be 00:03:00

Thanks alot
 
L

Luc The Perverse

Hi all ,
I'd really appreciate an insight on this ..

i am trying to subtract two date values

dateS = "06:03:00"
dateE = "06:00:00"

SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

java.util.Date dateStart = timeFormat.parse(dateS);
java.util.Date dateEnd = timeFormat.parse(dateE);

long diff = dateStart.getTime() - dateEnd.getTime();
return timeFormat.format(new Date(diff));


06:03:00 - 06:00:00 = 12:03:00 WHY ? I want my answer to be 00:03:00

Maybe you need to learn to differentiate a Date/Time from a time span

When you subtract one date from another you get a time span - not a new
date - if that helps ;)

Think about what you are really seeing

If you take 6:03:00 (in seconds) and subtract 6:00:00 (in seconds) you will
get 3 minutes. And what time is three minutes into the day? I'll give
you a hint - there is no zeroth hour
 
D

Danno

Hi all ,
I'd really appreciate an insight on this ..

i am trying to subtract two date values

dateS = "06:03:00"
dateE = "06:00:00"

SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

java.util.Date dateStart = timeFormat.parse(dateS);
java.util.Date dateEnd = timeFormat.parse(dateE);

long diff = dateStart.getTime() - dateEnd.getTime();
return timeFormat.format(new Date(diff));


06:03:00 - 06:00:00 = 12:03:00 WHY ? I want my answer to be 00:03:00

Thanks alot

06:03:00 - 6:00:00 = 180000ms
if you make a date out of 180000ms you get 12:03:00 AM. Because all
dates start on January 1, 1970 12:00:00 AM GMT.
 
D

Danno

Hi all ,
I'd really appreciate an insight on this ..

i am trying to subtract two date values

dateS = "06:03:00"
dateE = "06:00:00"

SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

java.util.Date dateStart = timeFormat.parse(dateS);
java.util.Date dateEnd = timeFormat.parse(dateE);

long diff = dateStart.getTime() - dateEnd.getTime();
return timeFormat.format(new Date(diff));


06:03:00 - 06:00:00 = 12:03:00 WHY ? I want my answer to be 00:03:00

Thanks alot


BTW, change your simpleDateFormat construct to this:
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
see if that works for ya. My previous post applies, BTW
 
M

Mark Space

Luc said:
you a hint - there is no zeroth hour

Well, in military time there is. Maybe he can get the result he wants
by changing the formatting of the output. Left as an exercise for the
reader, I'm too lazy to look it up.
 
L

Luc The Perverse

Mark Space said:
Well, in military time there is. Maybe he can get the result he wants by
changing the formatting of the output. Left as an exercise for the
reader, I'm too lazy to look it up.

He needs to use the classes in the way they were indended, not find
workarounds.

What is wrong with something like this?

String fTime(int numSecs){
int se = numSecs%60;
numSecs/=60;
int mi = numSecs%60
int hr = numSecs/60;
String[] va = {"0" + Integer.toString(hr), "0" + Integer.toString(mi),
"0" + Integer.toString(se)};
String ret = "";
for(String c : va){
if(!ret.equals(""))
ret+=":";
ret+=c.substring(c.length() - 2);
}
return ret;
}

(Note I have not compiled this - so it may have errors)
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top