java calculate pregnancy algorithm -
this question has answer here:
hello trying calculate how many days left in pregnancy term think algorithm 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 basing off of 280 day term, getduedate()
date object , gettime()
returns millisecond unix time
on real world days number reported off one, sometimes, , starting think algorithm wrong, or millisecond time gradually further , further off, or millisecond time not precise enough, or gregorian calendar function rounds weird.
all in i'm not sure, insight appreciated
i don't know algorithm, (is basically) 1 used while tracking wife's pregency...nerds...
save lot of "guess" work , 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 output...
duedate = 2014-02-25t14:14:31.159+11:00 days remaining = 279 days in = 1 week = 0.1
Comments
Post a Comment