Android Open Source - clinicalguide Date Helper






From Project

Back to project page clinicalguide.

License

The source code is released under:

Apache License

If you think the Android project clinicalguide listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.get.oxicam.clinicalguide.xml;
// ww  w. j a  v  a 2s .  c  o m
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.get.oxicam.clinicalguide.xml.forms.FormDuration;

public class DateHelper {
    private int duration;
    private String details;
    private Calendar calendar;

    /**
     * Constructs a datehelper to calculate dates.
     * Start Day of the week for this object depends on the FormDuration object.
     * If the details of the form is empty, Start of the week will be monday.
     * @param fd A FormDuration object that will hold the duraton (Form.DURATION_WEEK, etc..) and the start day of the week.
     */
    public DateHelper(FormDuration fd) {
  duration = fd.type;
  details = fd.detail;
  calendar = Calendar.getInstance();
  if (duration == FormDuration.DURATION_WEEK && !details.isEmpty()) {
      calendar.setFirstDayOfWeek(dayToCalendarDayOfWeek(details));
  } else {
      calendar.setFirstDayOfWeek(Calendar.MONDAY);
  }
    }

    /**
     * Construct a DateHelper with a specified duration and specified adjustment.
     * Start Day of the week in this object will be Sunday.
     * @param duration duration of the timespan (Form.DURATION_WEEK, etc)
     * @param adjust Specific adjustment (ie. if duration is Form.DURATION_WEEK and adjust is 2, that will be the week two weeks ago)
     */
    public DateHelper(int duration, int adjust) {
  this.duration = duration;
  details = "";
  calendar = Calendar.getInstance();
  calendar.setFirstDayOfWeek(Calendar.SUNDAY);
  switch (duration) {
  case FormDuration.DURATION_YEAR:
      if (adjust != 0) {
    GregorianCalendar gc = new GregorianCalendar(calendar.get(Calendar.YEAR), Calendar.JANUARY, 1);
    //calendar.setTimeInMillis(gc.getTimeInMillis());
    for (int i = 1; i < adjust; i++) {
        gc.add(Calendar.YEAR, -1);
    }
    calendar.setTimeInMillis(gc.getTimeInMillis() - 1);
      }
      break;
  case FormDuration.DURATION_MONTH:
      if (adjust != 0) {
    GregorianCalendar gc = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
    //calendar.setTimeInMillis(gc.getTimeInMillis());
    for (int i = 1; i < adjust; i++) {
        gc.add(Calendar.MONTH, -1);
    }
    calendar.setTimeInMillis(gc.getTimeInMillis() - 1);
      }
      break;
  case FormDuration.DURATION_WEEK:
      if (adjust != 0) {
    int diff = getDayDifference(calendar.getFirstDayOfWeek(),
      calendar.get(Calendar.DAY_OF_WEEK));
    calendar.add(Calendar.DAY_OF_YEAR, diff * -1);
    GregorianCalendar gc = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    
    
    for (int i = 1; i < adjust; i++) {
        gc.add(Calendar.DAY_OF_YEAR, -7);
    }
    calendar.setTimeInMillis(gc.getTimeInMillis() - 1);
      }
      break;
  case FormDuration.DURATION_DAY:
      if(adjust != 0){
    calendar = getMidnight(calendar);
    calendar.setTimeInMillis(calendar.getTimeInMillis() - 1);
    for(int i = 1; i < adjust; i++){
        calendar.add(Calendar.DAY_OF_YEAR, -1);
    }
      }
      break;
  case FormDuration.DURATION_DEFINED:
      break;
  }
    }

    public static long changeTextDateToLong(String s) {
  long retVal = 0;
  int yearL, monthL, dayL;
  String temp[] = s.split("-");
  if (temp.length == 3) {
      yearL = Integer.valueOf(temp[0]);
      monthL = Integer.valueOf(temp[1]) - 1;
      dayL = Integer.valueOf(temp[2]);

      final GregorianCalendar cal = new GregorianCalendar(yearL, monthL,
        dayL + 1);

      retVal = cal.getTimeInMillis() - 1;

  }

  return retVal;
    }
    /**
     * Creates a new calendar that will have the date of the calendar passed in parameter but at midnight.
     * This does not affect the original state of the calendar that is passed in the parameter.
     * @param c Calendar to get the midnight date from
     * @return a new GregorianCalendar with the same date but at midnight.
     */
    public static Calendar getMidnight(Calendar c){
  Calendar retVal = new GregorianCalendar(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
  return retVal;
    }
    
    /**
     * Calculates the age based on the milliseconds
     * @param birthdate The milliseconds representing the date of the birthday
     * @return The year difference from todays date compare to the date that is passed in parameter.
     */
    public static int calculateAge(long birthdate) {
  int retVal = -1;
  Calendar c1 = Calendar.getInstance();
  c1.setTimeInMillis(birthdate);
  Calendar c2 = Calendar.getInstance();
  c1 = getMidnight(c1);
  c2 = getMidnight(c2);
  do{
      retVal++;
      c1.add(Calendar.YEAR, 1);
      
  } while (c1.getTimeInMillis() <= c2.getTimeInMillis());

  return retVal;
    }

    /**
     * Gets the start day depending on the end date of the calendar that is inside this object.
     * This will call getStartDay(endDate).
     * Useful when the constructor DateHelper(type, adjust) was used when
     * creating the object. If the constructor DateHelper(formduration) was
     * used, endDate will be the current time.
     * 
     * @return A long representing milliseconds for the date
     */
    public long getStartDay() {
  return getStartDay(calendar.getTimeInMillis());
    }
    
    /**
     * Gets the end day depending on the calendar that is inside this object.
     * Useful when the constructor DateHelper(type, adjust) was used when
     * creating the object. If the constructor DateHelper(formduration) was
     * used, endDate will be the current time.
     * 
     * @return A long representing milliseconds for the date
     */
    public long getEndDay(){
  return calendar.getTimeInMillis();
    }

    /**
     * Gets the start day of the form depending on its duration.
     * 
     * @return A long representing milliseconds which can be transformed to
     *         date.
     */
    public long getStartDay(long endDate) {
  long retVal = -1;
  calendar.setTimeInMillis(endDate);
  switch (duration) {
  case FormDuration.DURATION_YEAR:
      retVal = getFirstOfYear();
      break;
  case FormDuration.DURATION_MONTH:
      retVal = getFirstOfMonth();
      break;
  case FormDuration.DURATION_WEEK:
      retVal = getFirstOfWeek();
      break;
  case FormDuration.DURATION_DEFINED:
      retVal = getFirstOfDefined();
      break;
  case FormDuration.DURATION_DAY:
      retVal = getMidnight(calendar).getTimeInMillis();
      break;
  }

  return retVal;
    }

    private long getFirstOfYear() {
  Calendar c = Calendar.getInstance();
  Date d = new Date();
  int year = calendar.get(Calendar.YEAR);
  GregorianCalendar gc = new GregorianCalendar(year, Calendar.JANUARY, 1);

  long retVal = gc.getTimeInMillis();

  return retVal;
    }

    private long getFirstOfMonth() {
  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH);
  GregorianCalendar gc = new GregorianCalendar(year, month, 1);

  long retVal = gc.getTimeInMillis();

  return retVal;
    }

    private long getFirstOfWeek() {

  int firstDay = calendar.getFirstDayOfWeek();
  int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
  int diff = getDayDifference(firstDay, currentDay);

  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH);
  int day = calendar.get(Calendar.DAY_OF_MONTH);

  GregorianCalendar gc = new GregorianCalendar(year, month, day);

  gc.add(Calendar.DAY_OF_YEAR, (diff * -1));

  return gc.getTimeInMillis();
    }

    private long getFirstOfDefined() {
  long retVal = 0;
  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH);
  int day = calendar.get(Calendar.DAY_OF_MONTH);

  int num = Integer.parseInt(details);

  GregorianCalendar gc = new GregorianCalendar(year, month, day);
  gc.add(Calendar.DAY_OF_YEAR, (num - 1) * -1);
  retVal = gc.getTimeInMillis();

  return retVal;
    }

    private int getDayDifference(int firstDay, int currentDay) {
  int retVal = 0;

  while (currentDay != firstDay) {
      retVal++;
      currentDay--;
      if (currentDay == 0) {
    currentDay = 7;
      }
  }

  return retVal;
    }

    /**
     * Turns a weekday string to the specific Calendar.DAY_OF_WEEK or -1 if the
     * day is invalid.
     * 
     * @param day
     *            The specific day to turn to int. eg. "sunday"
     * @return Returns Calendar.Monday for "monday", etc.. or -1 if the day is
     *         not a valid day
     */
    public int dayToCalendarDayOfWeek(String day) {
  int retVal = -1;
  if (day.equalsIgnoreCase("monday")) {
      retVal = Calendar.MONDAY;
  } else if (day.equalsIgnoreCase("tuesday")) {
      retVal = Calendar.TUESDAY;

  } else if (day.equalsIgnoreCase("wednesday")) {
      retVal = Calendar.WEDNESDAY;

  } else if (day.equalsIgnoreCase("thursday")) {
      retVal = Calendar.THURSDAY;

  } else if (day.equalsIgnoreCase("friday")) {
      retVal = Calendar.FRIDAY;

  } else if (day.equalsIgnoreCase("saturday")) {
      retVal = Calendar.SATURDAY;

  } else if (day.equalsIgnoreCase("sunday")) {
      retVal = Calendar.SUNDAY;
  }
  return retVal;
    }

}




Java Source Code List

.PatientDetailsDataSource.java
org.get.oxicam.clinicalguide.ClinicalGuideActivity.java
org.get.oxicam.clinicalguide.FileUtils.java
org.get.oxicam.clinicalguide.LoginActivity.java
org.get.oxicam.clinicalguide.db.DatabaseHelper.java
org.get.oxicam.clinicalguide.db.Database.java
org.get.oxicam.clinicalguide.db.FollowupDataSource.java
org.get.oxicam.clinicalguide.db.FollowupDetails.java
org.get.oxicam.clinicalguide.db.FollowupSQLHelper.java
org.get.oxicam.clinicalguide.db.HistoryDetailsDataSource.java
org.get.oxicam.clinicalguide.db.HistoryDetailsSQLHelper.java
org.get.oxicam.clinicalguide.db.HistoryDetails.java
org.get.oxicam.clinicalguide.db.PatientDetailsSQLHelper.java
org.get.oxicam.clinicalguide.db.PatientDetails.java
org.get.oxicam.clinicalguide.db.RegistrationValidator.java
org.get.oxicam.clinicalguide.db.Validator.java
org.get.oxicam.clinicalguide.encryption.Encryption.java
org.get.oxicam.clinicalguide.ui.AnswersReviewFragment.java
org.get.oxicam.clinicalguide.ui.AssessmentDetailFragment.java
org.get.oxicam.clinicalguide.ui.ClassificationFragment.java
org.get.oxicam.clinicalguide.ui.ClassificationListItem.java
org.get.oxicam.clinicalguide.ui.DatePickerFragment.java
org.get.oxicam.clinicalguide.ui.ExportFragment.java
org.get.oxicam.clinicalguide.ui.ExportscreenListItem.java
org.get.oxicam.clinicalguide.ui.FollowUpFragment.java
org.get.oxicam.clinicalguide.ui.FormScreenFragment.java
org.get.oxicam.clinicalguide.ui.HomescreenFragment.java
org.get.oxicam.clinicalguide.ui.HomescreenListItem.java
org.get.oxicam.clinicalguide.ui.ListItemOnClickListener.java
org.get.oxicam.clinicalguide.ui.MainSymptomFragment.java
org.get.oxicam.clinicalguide.ui.MainSymptomListItem.java
org.get.oxicam.clinicalguide.ui.NumberTickerValueChangeListener.java
org.get.oxicam.clinicalguide.ui.NumberTicker.java
org.get.oxicam.clinicalguide.ui.PatientDetailsFragment.java
org.get.oxicam.clinicalguide.ui.PatientHistoryFragment.java
org.get.oxicam.clinicalguide.ui.PatientsFragment.java
org.get.oxicam.clinicalguide.ui.QuestionListItem.java
org.get.oxicam.clinicalguide.ui.QuestionnaireFragment.java
org.get.oxicam.clinicalguide.ui.StatScreenFragment.java
org.get.oxicam.clinicalguide.ui.SummaryScreenFragment.java
org.get.oxicam.clinicalguide.ui.TextViewCustomFont.java
org.get.oxicam.clinicalguide.ui.TreatmentConfirmationDialog.java
org.get.oxicam.clinicalguide.ui.TreatmentFragment.java
org.get.oxicam.clinicalguide.ui.TreatmentListItem.java
org.get.oxicam.clinicalguide.ui.ViewDetailScreenFragment.java
org.get.oxicam.clinicalguide.xml.CGFormParser.java
org.get.oxicam.clinicalguide.xml.CGParser.java
org.get.oxicam.clinicalguide.xml.CGStatsParser.java
org.get.oxicam.clinicalguide.xml.DateHelper.java
org.get.oxicam.clinicalguide.xml.FormGenerator.java
org.get.oxicam.clinicalguide.xml.ParserHelper.java
org.get.oxicam.clinicalguide.xml.StatsGenerator.java
org.get.oxicam.clinicalguide.xml.XMLHandler.java
org.get.oxicam.clinicalguide.xml.data.AbstractAnswer.java
org.get.oxicam.clinicalguide.xml.data.Annotation.java
org.get.oxicam.clinicalguide.xml.data.AnswerValidator.java
org.get.oxicam.clinicalguide.xml.data.Answer.java
org.get.oxicam.clinicalguide.xml.data.Assessment.java
org.get.oxicam.clinicalguide.xml.data.CombinedAnswer.java
org.get.oxicam.clinicalguide.xml.data.FollowUp.java
org.get.oxicam.clinicalguide.xml.data.FormQuery.java
org.get.oxicam.clinicalguide.xml.data.Info.java
org.get.oxicam.clinicalguide.xml.data.Option.java
org.get.oxicam.clinicalguide.xml.data.PatientAttribute.java
org.get.oxicam.clinicalguide.xml.data.Question.java
org.get.oxicam.clinicalguide.xml.data.Questionnaire.java
org.get.oxicam.clinicalguide.xml.data.SimpleAnswer.java
org.get.oxicam.clinicalguide.xml.data.Symptom.java
org.get.oxicam.clinicalguide.xml.data.TreatmentAction.java
org.get.oxicam.clinicalguide.xml.data.Treatment.java
org.get.oxicam.clinicalguide.xml.data.User.java
org.get.oxicam.clinicalguide.xml.forms.FormCell.java
org.get.oxicam.clinicalguide.xml.forms.FormColumn.java
org.get.oxicam.clinicalguide.xml.forms.FormDuration.java
org.get.oxicam.clinicalguide.xml.forms.Form.java
org.get.oxicam.clinicalguide.xml.query.QueryHelper.java
org.get.oxicam.clinicalguide.xml.query.QueryResultCell.java
org.get.oxicam.clinicalguide.xml.query.QueryResultRow.java
org.get.oxicam.clinicalguide.xml.query.QueryResultTable.java
org.get.oxicam.clinicalguide.xml.stats.AbstractStatsQuestion.java
org.get.oxicam.clinicalguide.xml.stats.StatsAnswerHolder.java
org.get.oxicam.clinicalguide.xml.stats.StatsColumnCompare.java
org.get.oxicam.clinicalguide.xml.stats.StatsComparatorOperator.java
org.get.oxicam.clinicalguide.xml.stats.StatsCompareConstraint.java
org.get.oxicam.clinicalguide.xml.stats.StatsConstraint.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionAverage.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionCount.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionExtrema.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionFactory.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionList.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionPercentage.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionRatio.java
org.get.oxicam.clinicalguide.xml.stats.StatsSubject.java
org.get.oxicam.clinicalguide.xml.stats.StatsTimespan.java
org.get.oxicam.clinicalguide.xml.stats.Stats.java