Android Open Source - droidling Date Distribution






From Project

Back to project page droidling.

License

The source code is released under:

Copyright (c) 2012 Keith Trnka Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softwa...

If you think the Android project droidling 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 com.github.ktrnka.droidling;
/*from www.  j av  a2s .c om*/
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * A collection of Dates, which we can use to compute various stats such as date
 * range, day-of-week histogram, time-of-day histogram, etc.
 * 
 * @author keith.trnka
 */
public class DateDistribution {
    private ArrayList<Date> dates;

    public DateDistribution() {
        dates = new ArrayList<Date>();
    }

    public void add(Date date) {
        dates.add(date);
    }

    public Date findMin() {
        Date min = null;
        for (Date date : dates)
            if (min == null || min.compareTo(date) > 0)
                min = date;

        return min;
    }

    public Date findMax() {
        Date max = null;
        for (Date date : dates)
            if (max == null || max.compareTo(date) < 0)
                max = date;

        return max;
    }

    /**
     * This is working under the assumption of approximately 30-day months.
     * 
     * @return The average number of texts per 30 days between your first and
     *         last text. If you have only one text or less than 30 days' worth,
     *         it'll return the total so far.
     */
    public double computeTextsPerMonth() {
        Date min = findMin();
        Date max = findMax();

        long maxTime = max.getTime();
        long minTime = min.getTime();

        double monthMs = 1000L * 60 * 60 * 24 * 30;

        double months = (maxTime - minTime) / monthMs;

        // safety from crazy numbers
        if (months < 1)
            return dates.size();

        return dates.size() / months;
    }

    /**
     * Builds a day-of-week histogram, with Sunday as index 0 and Saturday as
     * index 6.
     */
    public int[] computeDayOfWeekHistogram() {
        int[] days = new int[7];
        for (int i = 0; i < days.length; i++)
            days[i] = 0;

        Calendar calendar = Calendar.getInstance();

        for (Date date : dates) {
            calendar.setTime(date);
            days[calendar.get(Calendar.DAY_OF_WEEK) - 1]++;
        }

        return days;
    }

    /**
     * Compute a histogram mapping the hour to the number of texts in that hour.
     * Uses 24-hour format, starting from 1 (return array is size 25). TODO:
     * There's a bug in this function cause Java returns a 0-23 value and I
     * didn't realize it.
     */
    public int[] computeHourHistogram() {
        int[] hours = new int[25];
        for (int i = 0; i < hours.length; i++)
            hours[i] = 0;

        Calendar calendar = Calendar.getInstance();

        for (Date date : dates) {
            calendar.setTime(date);
            hours[calendar.get(Calendar.HOUR_OF_DAY)]++;
        }

        return hours;
    }
}




Java Source Code List

com.github.ktrnka.droidling.AboutActivity.java
com.github.ktrnka.droidling.AboutInterpersonalActivity.java
com.github.ktrnka.droidling.AboutLangIDActivity.java
com.github.ktrnka.droidling.AboutPersonalActivity.java
com.github.ktrnka.droidling.CorpusStats.java
com.github.ktrnka.droidling.DateDistribution.java
com.github.ktrnka.droidling.DiagnosticActivity.java
com.github.ktrnka.droidling.ExtendedApplication.java
com.github.ktrnka.droidling.GraphCard.java
com.github.ktrnka.droidling.ImageAdapter.java
com.github.ktrnka.droidling.InterpersonalActivity.java
com.github.ktrnka.droidling.InterpersonalCard.java
com.github.ktrnka.droidling.InterpersonalSingleStats.java
com.github.ktrnka.droidling.InterpersonalStats.java
com.github.ktrnka.droidling.LIDStats.java
com.github.ktrnka.droidling.LanguageIdentificationActivity.java
com.github.ktrnka.droidling.LanguageIdentifier.java
com.github.ktrnka.droidling.MainActivity.java
com.github.ktrnka.droidling.PersonalActivity.java
com.github.ktrnka.droidling.PersonalStats.java
com.github.ktrnka.droidling.RefreshableActivity.java
com.github.ktrnka.droidling.ShareableCard.java
com.github.ktrnka.droidling.Sms.java
com.github.ktrnka.droidling.Tokenizer.java
com.github.ktrnka.droidling.WordDistribution.java
com.github.ktrnka.droidling.helpers.AsyncDrawable.java
com.github.ktrnka.droidling.helpers.BitmapLoaderTask.java
com.github.ktrnka.droidling.helpers.Util.java