Solved

date/time Question

Posted on 2004-09-24
16
188 Views
Last Modified: 2010-03-31
I have:

public String getTotalTime(){
            DecimalFormat df = new DecimalFormat();
            return df.format(((this.getDateTill().getTime() - this.getDateFrom().getTime()) / 1000 / 3600) - this.getBreak());
}

if i have:

dateTill =10:00  (DATE format)
dateFrom =14:00  (DATE format)
break = 0.25  (FLOAT format)

i get the totalTime as: 3,75    (i need it to be 3.35 or 3:35)

How can i do it?  Thanks.
0
Comment
Question by:jaggybala1
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 12142304
use SimpleDateFormat
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12142307
0
 

Author Comment

by:jaggybala1
ID: 12142393
if i use SimpleDateFormat, can i use float value of 'break' ?

could you please tell me how should i code for DATE with FLOAT using SimpleDateFormat ?
0
 

Author Comment

by:jaggybala1
ID: 12142469
PS: I have been restricted only to use FLOAT for 'break' field.
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12142572
Make it a calender object and subtract the time from its minutes and then get the format
0
 
LVL 18

Assisted Solution

by:armoghan
armoghan earned 20 total points
ID: 12142629
Calendar cal = Calendar.getInstance();
cal.setTime(someDateObject)
cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) - this.getBreak());

cal.getTime();


This is kind of pseudo code
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12143210
>> (i need it to be 3.35 or 3:35)
Sure?

Doesn't a break of 0.25 stand for one quarter?
Then 4h - 1 quarter = 3:45
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 60 total points
ID: 12143346
Why not just some maths:

public String getTotalTime(){

    long brutoDiff = (this.getDateTill().getTimeInMillis() - this.getDateFrom().getTimeInMillis())/1000;  // in seconds
    long breakDiff = (long)(3600.0*this.getBreak);  // in seconds
    long difference = brutoDiff - breakDiff;  // in seconds
    long hours = difference / 3600;
    long minutes = ( difference - (hours * 3600) ) / 60;
    return hours + ":" + minutes;   // returns "3:45" in your example
}
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 37

Expert Comment

by:zzynx
ID: 12143398
Oh, 0.25 stands for 25 minutes?
Then replace in the above

       long breakDiff = (long)(3600.0*this.getBreak);  // in seconds
by
       long breakDiff = (long)(6000.0*breakTime);  // in seconds


Then it returns "3:35"
0
 

Author Comment

by:jaggybala1
ID: 12143734
Actually, i see that:

>> Doesn't a break of 0.25 stand for one quarter?
>> Then 4h - 1 quarter = 3:45

is right!

but i get the totalTime as: 3,75

how can i get it as 3.75

Thanks.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12143783
>> i get the totalTime as: 3,75
>> how can i get it as 3.75

Don't see the difference...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12143968
Oh, you want to be sure you always have a point (instead of a colon in your locale)?
Is it that?

public String getTotalTime(){

    long brutoDiff = (this.getDateTill().getTimeInMillis() - this.getDateFrom().getTimeInMillis())/1000;  // in seconds
    long breakDiff = (long)(3600.0*this.getBreak);  // in seconds
    long difference = brutoDiff - breakDiff;  // in seconds
    long hours = difference / 3600;
    long minutes = ( difference - (hours * 3600) ) / 60;
    float result = (float)hours + (float)minutes/60.0F;

    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    java.text.DecimalFormat fmt = new DecimalFormat();
    fmt.setDecimalFormatSymbols(dfs);
    return fmt.format(result);     // returns "3.75" in your case
}
0
 
LVL 37

Accepted Solution

by:
zzynx earned 60 total points
ID: 12144071
So in fact this simply asnwers you question, right?
;°)

public String getTotalTime(){
          DecimalFormat df = new DecimalFormat();
          DecimalFormatSymbols dfs = new DecimalFormatSymbols();
          dfs.setDecimalSeparator('.');
          fmt.setDecimalFormatSymbols(dfs);
          return df.format(((this.getDateTill().getTime() - this.getDateFrom().getTime()) / 1000 / 3600) - this.getBreak());
}

Remark: sure you want to overrule number formatting corresponding with the user's locale?
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 20 total points
ID: 12150491
You need to split the time into seperate Hours/minutes/seconds then format them into a string.  See if this thread helps:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21134849.html
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12166934
Thanks for accepting
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12168038
Thanks :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 41
micro services vs rest web services 16 50
Strange loading of website behaviour 3 23
GUI builder for Eclipse? 8 11
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now