java计算妊娠算法 [英] java calculate pregnancy algorithm

查看:230
本文介绍了java计算妊娠算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我想要计算怀孕期间剩下多少天,但我认为我的算法不正确

  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时间



在某些现实世界的日子里,报告的数字有时会被关闭,我开始认为我的算法是错误的,或毫秒的时间逐渐进一步下降,或毫秒的时间不够精确,或者grep的日历功能很奇怪。



所有的一切我不知道,任何洞察力赞赏

解决方案

我不知道你的算法,但这(基本上)是我跟踪我妻子的过程中使用的一个...书呆子...


$ b $保存你自己的很多猜工作,并抓住 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();

天d = Days.daysBetween(今天,dueDate);

int daysRemaining = d.getDays();

int daysIn = DAYS_IN_PREGNANCY - daysRemaining;

int weekValue = daysIn / 7;
int weekPart = daysIn%7;

String week = weekValue +。 +周部分

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 
剩余天数= 279
天数= 1
周= 0.1


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);
   }

I am basing it off of a 280 day term, getDueDate() is a Date object and getTime() returns millisecond unix time

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.

All in all I'm not sure, any insight appreciated

解决方案

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);

    }
}

This will output...

dueDate = 2014-02-25T14:14:31.159+11:00
Days remaining = 279
Days In = 1
Week = 0.1

这篇关于java计算妊娠算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆