java的计算pregnancy算法 [英] java calculate pregnancy algorithm
问题描述
您好我想计算多少天都留在pregnancy词,但我认为我的算法是不正确
hello I am trying to calculate how many days are left in a pregnancy term but I think my algorithm is incorrect
public int getDaysPregnantRemainder_new() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long diffDays = 280 - ((getDueDate().getTime() - calendar.getTime()
.getTime()) / (24 * 60 * 60 * 1000));
return (int) Math.abs((diffDays) % 7);
}
我立足其关闭一个280天项, getDueDate()
是一个Date对象和的getTime()
返回毫秒UNIX时间
I am basing it off of a 280 day term, getDueDate()
is a Date object and getTime()
returns millisecond unix time
在一些真实世界日内报告数是关闭的,有时,我开始觉得我的算法是错误的,或者毫秒的时间得到已渐行渐远了,还是毫秒的时间并不precise不够,或者阳历功能舍奇怪。
On some real world days the number reported is off by one, sometimes, and I am starting to think my algorithm is just wrong, or the millisecond time get gradually further and further off, or the millisecond time is not precise enough, or the gregorian calendar function rounds weird.
总之我不知道,任何见解AP preciated
All in all I'm not sure, any insight appreciated
推荐答案
我不知道你的算法,但是这(基本上)我用,同时跟踪我妻子的pregency ......书呆子一个..
I don't know about your algorithm, but this (is basically) the one I used while tracking my wife's pregency...nerds...
保存自己很多的猜测的工作,弄个乔达时
Save yourself a lot of "guess" work and get hold of Joda-Time
public class TestDueDate {
public static final int WEEKS_IN_PREGNANCY = 40;
public static final int DAYS_IN_PREGNANCY = WEEKS_IN_PREGNANCY * 7;
public static void main(String[] args) {
DateTime dueDate = new DateTime();
dueDate = dueDate.plusDays(DAYS_IN_PREGNANCY);
System.out.println("dueDate = " + dueDate);
DateTime today = DateTime.now();
Days d = Days.daysBetween(today, dueDate);
int daysRemaining = d.getDays();
int daysIn = DAYS_IN_PREGNANCY - daysRemaining;
int weekValue = daysIn / 7;
int weekPart = daysIn % 7;
String week = weekValue + "." + weekPart;
System.out.println("Days remaining = " + daysRemaining);
System.out.println("Days In = " + daysIn);
System.out.println("Week = " + week);
}
}
这将输出...
dueDate = 2014-02-25T14:14:31.159+11:00
Days remaining = 279
Days In = 1
Week = 0.1
这篇关于java的计算pregnancy算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!