public long weekdays() { if (weekdays == -1) { weekdays = 0; NSTimeZone tz = NSTimeZone.timeZoneWithName("America/Los_Angeles", true); // NSTimestamp currDatePacific = new NSTimestamp(currDate.getTime(), tz); NSTimestamp startDate = new NSTimestamp(day1.getTime(), tz); GregorianCalendar cal = new GregorianCalendar(); int num = (int) calendardays(); for (int i = 0; i < num; i++) { // will check the start date, all days in-between and the end-date NSTimestamp tempDate = startDate.timestampByAddingGregorianUnits(0, 0, i, 0, 0, 0); cal.setTime(tempDate); // The day-of-week is an integer value where 1 is Sunday, 2 is Monday, ..., and 7 is // Saturday int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if ((dayOfWeek > 1) && (dayOfWeek < 7)) { weekdays++; } } } return weekdays; }
/** * TODO traiter le cas d'une periode unique a 1 jour qui tombe le 31 ... * * <p>Retourne la duree d'une periode sur la base de 1 mois = 30 jours 1 an = 12 mois = 360 jours * * @param dateDebut * @param dateDebut * @return le nombre de jours de cette periode sur cette base */ public static int getTotalJours360(NSTimestamp dateDebut, NSTimestamp dateFin) { int dureePeriode = 0; NSTimestamp dateDebutMoisEnCours = DateCtrlConges.dateToDebutMois(dateDebut); NSTimestamp dateFinMoisEnCours = DateCtrlConges.dateToFinMois(dateDebut); while (DateCtrlConges.isBeforeEq(dateDebutMoisEnCours, dateFin)) { boolean moisComplet = true; // recadrage du debut et de la fin sur la periode if (DateCtrlConges.isBefore(dateDebutMoisEnCours, dateDebut)) { dateDebutMoisEnCours = dateDebut; moisComplet = false; } if (DateCtrlConges.isAfter(dateFinMoisEnCours, dateFin)) { dateFinMoisEnCours = dateFin; moisComplet = false; } // si le mois est complet, on compte 30 jours if (moisComplet) { dureePeriode += 30; } else { // +1 car de j à j+i, il y a i+1 jours complets dureePeriode += (dateFinMoisEnCours.getTime() - dateDebutMoisEnCours.getTime()) / (1000 * 60 * 60 * 24) + 1; } // avance au mois suivant dateDebutMoisEnCours = DateCtrlConges.dateToDebutMois(dateDebutMoisEnCours) .timestampByAddingGregorianUnits(0, 1, 0, 0, 0, 0); dateFinMoisEnCours = dateDebutMoisEnCours.timestampByAddingGregorianUnits(0, 1, -1, 0, 0, 0); } return dureePeriode; }
public static String timeFrom(NSTimestamp time, NSTimestamp baseTime) { StringBuilder results = new StringBuilder(); StringHelper helper = new StringHelper(); int years = DateUtils.difference(time, baseTime, Calendar.YEAR) - 1; int months; int days; int hours; int minutes; if (years > 1) { String year_label = helper.pluralize("year", years); results.append(years + " " + year_label); } else { months = DateUtils.difference(time, baseTime, Calendar.MONTH) - 1; if (months > 11) { results.append("about 1 year ago"); } else if (months > 1) { String month_label = helper.pluralize("month", months); results.append(months + " " + month_label); } else { days = DateUtils.difference(time, baseTime, Calendar.DAY_OF_YEAR) - 1; if (days > 25) { results.append("about 1 month"); } else if (days > 7) { int weeks = days / 7; String weeks_label = helper.pluralize("week", weeks); results.append(weeks + " " + weeks_label); } else if (days > 1) { if (days <= 2) { hours = DateUtils.difference(time, baseTime, Calendar.HOUR_OF_DAY) - 1; if (hours < 30) { results.append("about 1 day"); } else { String days_label = helper.pluralize("day", days); results.append(days + " " + days_label); } } else { String days_label = helper.pluralize("day", days); results.append(days + " " + days_label); } } else { hours = DateUtils.difference(time, baseTime, Calendar.HOUR_OF_DAY) - 1; if (hours >= 20) { results.append("about 1 day"); } else if (hours > 1) { String hours_label = helper.pluralize("hour", hours); results.append(hours + " " + hours_label); } else { minutes = DateUtils.difference(time, baseTime, Calendar.MINUTE) - 1; if (minutes >= 45) { results.append("about an hour"); } else if (minutes > 20 && minutes < 45) { results.append("about half an hour"); } else if (minutes > 1) { String minutes_label = helper.pluralize("minute", minutes); results.append(minutes + " " + minutes_label); } else { return "this very minute"; } } } } } if (baseTime.after(time)) { results.append(" ago"); } else if (baseTime.before(time)) { results.append(" from now"); } else { results.append("now"); } return results.toString(); }