I'm writing a Java program that automatically generates a certain amount of "units" for the user to do (Actual purpose not important in this question).
我正在编写一个Java程序,可以自动为用户生成一定数量的“单位”(实际目的在这个问题中并不重要)。
I'm struggling to find a good way to determine how many of these "units" to give the user for the next week.
我很难找到一个好方法来确定下周有多少这些“单位”给用户。
Intentions of the calculation :
计算的意图:
- Gives "units" of a specific week
- Calculates units for the week based on the actual day of the week (Mon-Fri)
- Accounts for all of the units that month (Ex. If it is the last week of the month, give all possible units that entire week)
- If estimating, should over-estimate (Due to the program's nature, under-estimating would be far worse.)
- Units cannot be decimals (Hence the cast to int)
给出特定周的“单位”
根据实际星期几(周一至周五)计算一周的单位
该月所有单位的帐户(例如,如果是该月的最后一周,则给出整周的所有单位)
如果估算,应该高估(由于程序的性质,低估会更糟糕。)
单位不能是小数(因此转换为int)
Variables I have :
我有变量:
- The amount of "units" to do that month
那个月要做的“单位”数量
I have tried to do this in a few different ways, and so far, the best way I have is this :
我试图以几种不同的方式做到这一点,到目前为止,我最好的方法是:
public static int getRemainingUnitsThisWeek() {
return (int) Math.round(((double) getUnitsThisMonth() / (((30 - (double) DateTime.now().getDayOfMonth()+1) / 7) < 1 ? 1 :
((30 - (double) DateTime.now().getDayOfMonth()+1) / 7))) / (double)DateTime.now().getDayOfWeek());
}
The problem that I end up (specifically, I'm still not happy with how it does it) with is that in the "units" given on the last week of the month is still divided by the day of the week (Which in turn gives you a much lower number each day - even though the user needs to receive all the "units" by the end of the month).
我最终的问题(具体来说,我仍然不满意它是如何做的)是在本月最后一周给出的“单位”仍然除以星期几(反过来)每天为您提供更低的数字 - 即使用户需要在月底之前收到所有“单位”)。
Any questions you might have, just ask!
您可能有任何问题,请问!
PS. The library I am using in the DateTime.now() function is joda-time.
PS。我在DateTime.now()函数中使用的库是joda-time。
2 个解决方案
#1
0
Your description is confusing, so I'm not sure I can answer it directly. Instead, I will answer a different question, which may help:
你的描述很混乱,所以我不确定我是否可以直接回答。相反,我会回答一个不同的问题,这可能会有所帮助:
I have a bunch of units to do, and the rest of the month to do them. How many should I do today?
我有一堆单位要做,其余的月份要做。我今天要做多少?
I don't know about Joda, but using the standard Date
API, you can use this answer to get a Date
representing the end of the month, a similar technique to get a Date
representing the end of today, and new Date()
to get the current time. Then subtract their getTime()
s. You can then apportion your units now that you know how many milliseconds to the end of the day and the end of the month.
我不知道Joda,但是使用标准的Date API,您可以使用此答案来获取表示月末的日期,类似的技术可以获得表示今天结束的日期,以及新的Date()得到当前时间。然后减去他们的getTime()s。然后,您可以分配您的单位,因为您知道当天结束和月末的时间。
units_today = total_units_to_do * milliseconds_to_end_of_today /
milliseconds_to_end_of_month;
#2
0
I don't know java, so I won't attempt to write code, but I suggest you start by calculating daysLeftInMonth
and daysLeftInWeek
. Once you get those working perfectly, you have two simple cases: either the month will end before the week does, or else it won't.
我不知道java,所以我不会尝试编写代码,但我建议你先从计算daysLeftInMonth和daysLeftInWeek开始。一旦你完成了那些工作,你有两个简单的案例:月份将在一周之前结束,否则它不会。
In the first case the rest of the algorithm is trivial. In the second, calculate how many units there should be per day (rounding up), then multiply by the days left in the week.
在第一种情况下,算法的其余部分是微不足道的。在第二个中,计算每天应该有多少单位(四舍五入),然后乘以一周剩下的天数。
#1
0
Your description is confusing, so I'm not sure I can answer it directly. Instead, I will answer a different question, which may help:
你的描述很混乱,所以我不确定我是否可以直接回答。相反,我会回答一个不同的问题,这可能会有所帮助:
I have a bunch of units to do, and the rest of the month to do them. How many should I do today?
我有一堆单位要做,其余的月份要做。我今天要做多少?
I don't know about Joda, but using the standard Date
API, you can use this answer to get a Date
representing the end of the month, a similar technique to get a Date
representing the end of today, and new Date()
to get the current time. Then subtract their getTime()
s. You can then apportion your units now that you know how many milliseconds to the end of the day and the end of the month.
我不知道Joda,但是使用标准的Date API,您可以使用此答案来获取表示月末的日期,类似的技术可以获得表示今天结束的日期,以及新的Date()得到当前时间。然后减去他们的getTime()s。然后,您可以分配您的单位,因为您知道当天结束和月末的时间。
units_today = total_units_to_do * milliseconds_to_end_of_today /
milliseconds_to_end_of_month;
#2
0
I don't know java, so I won't attempt to write code, but I suggest you start by calculating daysLeftInMonth
and daysLeftInWeek
. Once you get those working perfectly, you have two simple cases: either the month will end before the week does, or else it won't.
我不知道java,所以我不会尝试编写代码,但我建议你先从计算daysLeftInMonth和daysLeftInWeek开始。一旦你完成了那些工作,你有两个简单的案例:月份将在一周之前结束,否则它不会。
In the first case the rest of the algorithm is trivial. In the second, calculate how many units there should be per day (rounding up), then multiply by the days left in the week.
在第一种情况下,算法的其余部分是微不足道的。在第二个中,计算每天应该有多少单位(四舍五入),然后乘以一周剩下的天数。