• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

rounding a fraction+time

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i don't know..whether this is the right forum tp post this doubt..
First of all my doubt is,how to find the difference between 2 time values in java.
i have 2 values
time1= "9.20"
time2="18.00"
what i want is the difference 8.40.how to get it?
also the second doubt how to round a fraction to 2 decimal points...
8.6667899 to 8.67.
pls help
thanx in advance
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"googly man"

Pleave review our naming policy and adjust your display name accordingly.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"meera",
Thank you for changing your display name, but it still not correct.

We require display names to be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Thanks,
Dave
[ October 10, 2005: Message edited by: David O'Meara ]
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-------------------------------------------------------------
the second doubt how to round a fraction to 2 decimal points...
8.6667899 to 8.67.
-------------------------------------------------------------

U can use NumberFormat Class. check the API for the method, i think its setMaximumFractionDigit(2). check the api for the method explanation.

----------------------------------------------------
the difference between 2 time values in java
----------------------------------------------------

U can convert the time to milliseconds, find the difference as its a integer and then convert back to hours.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Construct two GregorianCalendar objects gc1 and gc2
2. Calculate Elapsed Seconds
long getElapsedSeconds(gc1, gc2) {
Date d1 = gc1.getTime();
Date d2 = gc2.getTime();
long l1 = d1.getTime();
long l2 = d2.getTime();
long difference = Math.abs(l2 - l1);
return difference / 1000;
}
3.
long seconds = getElapsedSeconds(gc1, gc2)
int hours = (int)seconds / 3600;
seconds = seconds - (hours * 3600);
int minutes = (int) seconds / 60;
int ss = (int)seconds - (minutes * 60);
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this ro round up to tow decimals.

public static double roundDoubleValue(double val2Round) {
/*
* Method to round off a recurring double value to a two decimal precision.
*
*/
BigDecimal bd = new BigDecimal(val2Round);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
// now to remove zero after decimal
return bd.doubleValue();
} // end of method roundDoubleValue
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic