• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to use time in java?  RSS feed

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey! I have a question on how to use time in java such as calculate the days months and years for a program. I have to find the time it takes for someone to become a millionaire. Thats really the only problem i am having


 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of questions,
What are the number of working days ?
If your program is asking for wages per hour what purpose does wage per day on the fourth week have ?
Can you further explain the scenario that you are trying to accomplish ?


 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay so for the first for loop the average pay is 7.50 per hour (40 hours a week) the second loop has the pay only to $32 a day for 4 weeks. the third loop has the initial pay increased by 0.2 % and its supposed to be doubled every two weeks.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help I am having serious trouble.

 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I wont be able to help you much If you don't provide the details. Just pasting a chunk of code does not help. You initially posted this code without further explanation about the 0.2% increase and doubling logic.

If you are looking for the class you need to use to contain the time difference, you should look at LocalDateTime and ChronoUnit.
More specifically,


 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More information can be found here : https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
 
Wazim Karim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid,
I think what you're looking for is the Period Class; located in the java.time package.
I won't bother delving into the logic behind your program. But what I gather is this:
You're trying to figure out how long it would take a person to become a millionaire based on working 40 hours a week, 5 days a week at a set wage. For example, $7.50 per hour. Now, keep in mind that work days differ; For example, a work week might be 40 hours per week in the US. But, 30 Hours a week in Europe. But
There are LOTS of variables to consider. But lets assume that it's a 40 hour week, 5 days a week @ 7.50 per hour.
You can figure that 7.50 * 40 is $300 per week.
300 per week * 4 (weeks per month) is 1200 per month.  So if you take 1 million dollars and divide it by 1200 dollars per month that gives you  833.33 months.
833.33 months divide by 12 months per year is about 69.44 years. Lets just round down to 69 years.

Now using the Period Class I mentioned above; you can obtain a period of 69 years like this.

Peroid p = Period.of(69,0,0);

Here's an example (lets say it took 69 years, 5 months and 3 days) to become a millionaire..

Period p = Period.of(69,5,3);  //this is a period of 69 days, 5 months and 3 days.

Then you call methods on p like so:

p.getYears();  // returns 69;
p.getMonths(); // returns 5
p.getDays(); // returns 3

This entire reply is simply to direct you to the java.time package.  Here's a link:

https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

There are MANY ways to approach this, But you're not providing enough details.

Hopefully this helps.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is very difficult to understand.  One reason is the poor formatting.  Another is illustrated in this code:

What is the purpose of this code?  What are you trying to accomplish?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!