Android Open Source - vitdroid-android Util






From Project

Back to project page vitdroid-android.

License

The source code is released under:

Apache License

If you think the Android project vitdroid-android 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.googlecode.android.widgets.DateSlider.labeler;
//w  w  w . ja  v a  2 s.  c om
import java.util.Calendar;

import com.googlecode.android.widgets.DateSlider.TimeObject;

/**
 * A bunch of static helpers for manipulating dates and times. There are two
 * types of methods -- add*() methods that add a number of units to a time
 * and return the result as a Calendar, and get*() objects that take a
 * Calendar object and a format string and produce the appropriate TimeObject.
 */
class Util {
    public static Calendar addYears(long time, int years) {
        return add(time, years, Calendar.YEAR);
    }

    public static Calendar addMonths(long time, int months) {
        return add(time, months, Calendar.MONTH);
    }

    public static Calendar addWeeks(long time, int days) {
        return add(time, days, Calendar.WEEK_OF_YEAR);
    }

    public static Calendar addDays(long time, int days) {
        return add(time, days, Calendar.DAY_OF_MONTH);
    }

    public static Calendar addHours(long time, int hours) {
        return add(time, hours, Calendar.HOUR_OF_DAY);
    }

    public static Calendar addMinutes(long time, int minutes) {
        return add(time, minutes, Calendar.MINUTE);
    }
    
    public static Calendar addMinutes(long time, int minutes, int minInterval) {
        return add(time, minutes*minInterval, Calendar.MINUTE);
    }

    public static TimeObject getYear(Calendar c, String formatString) {
        int year = c.get(Calendar.YEAR);
        // set calendar to first millisecond of the year
        c.set(year, 0, 1, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        long startTime = c.getTimeInMillis();
        // set calendar to last millisecond of the year
        c.set(year, 11, 31, 23, 59, 59);
        c.set(Calendar.MILLISECOND, 999);
        long endTime = c.getTimeInMillis();
        return new TimeObject(String.format(formatString, c,c), startTime, endTime);
    }

    public static TimeObject getMonth(Calendar c, String formatString) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        // set calendar to first millisecond of the month
        c.set(year, month, 1, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        long startTime = c.getTimeInMillis();
        // set calendar to last millisecond of the month
        c.set(year, month, c.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
        c.set(Calendar.MILLISECOND, 999);
        long endTime = c.getTimeInMillis();
        return new TimeObject(String.format(formatString,c,c), startTime, endTime);
    }

    public static TimeObject getDay(Calendar c, String formatString) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        // set calendar to first millisecond of the day
        c.set(year, month, day, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        long startTime = c.getTimeInMillis();
        // set calendar to last millisecond of the day
        c.set(year, month, day, 23, 59, 59);
        c.set(Calendar.MILLISECOND, 999);
        long endTime = c.getTimeInMillis();
        return new TimeObject(String.format(formatString,c,c), startTime, endTime);
    }

    public static TimeObject getHour(Calendar c, String formatString) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        // get the first millisecond of that hour
        c.set(year, month, day, hour, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        long startTime = c.getTimeInMillis();
        // get the last millisecond of that hour
        c.set(year, month, day, hour, 59, 59);
        c.set(Calendar.MILLISECOND, 999);
        long endTime = c.getTimeInMillis();
        return new TimeObject(String.format(formatString, c,c), startTime, endTime);
    }

    public static TimeObject getMinute(Calendar c, String formatString) {
      return getMinute(c, formatString, 1);
    }
    
    public static TimeObject getMinute(Calendar c, String formatString, int minInterval) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        // get the last millisecond of that minute interval (cap it at 59)
        c.set(year, month, day, hour, Math.min(59,minute+minInterval-1), 59);
        c.set(Calendar.MILLISECOND, 999);
        long endTime = c.getTimeInMillis();
        // get the first millisecond of that minute interval
        c.set(year, month, day, hour, minute, 0);
        c.set(Calendar.MILLISECOND, 0);
        long startTime = c.getTimeInMillis();
        return new TimeObject(String.format(formatString, c,c), startTime, endTime);
    }

    public static TimeObject getTime(Calendar c, String formatString, int minuteInterval) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE)/minuteInterval*minuteInterval;
        // get the last millisecond of that 15 minute block
        c.set(year, month, day, hour, minute+minuteInterval-1, 59);
        c.set(Calendar.MILLISECOND, 999);
        long endTime = c.getTimeInMillis();
        // get the first millisecond of that 15 minute block
        c.set(year, month, day, hour, minute, 0);
        c.set(Calendar.MILLISECOND, 0);
        long startTime = c.getTimeInMillis();
        return new TimeObject(String.format(formatString, c,c), startTime, endTime);
    }

    private static Calendar add(long time, int val, int field) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.add(field, val);
        return c;
    }
}




Java Source Code List

com.googlecode.android.widgets.DateSlider.AlternativeDateSlider.java
com.googlecode.android.widgets.DateSlider.Attendance.java
com.googlecode.android.widgets.DateSlider.Attendanceboth.java
com.googlecode.android.widgets.DateSlider.BackGroundDialogs.java
com.googlecode.android.widgets.DateSlider.Base64.java
com.googlecode.android.widgets.DateSlider.CustomDateSlider.java
com.googlecode.android.widgets.DateSlider.DBAdapter2.java
com.googlecode.android.widgets.DateSlider.DBAdapter3.java
com.googlecode.android.widgets.DateSlider.DBAdapter5.java
com.googlecode.android.widgets.DateSlider.DBAdapter6.java
com.googlecode.android.widgets.DateSlider.DBAdapter.java
com.googlecode.android.widgets.DateSlider.DBAdaptergrades.java
com.googlecode.android.widgets.DateSlider.DBAdapterlinks.java
com.googlecode.android.widgets.DateSlider.DateSlider.java
com.googlecode.android.widgets.DateSlider.DateTimeSlider.java
com.googlecode.android.widgets.DateSlider.DefaultDateSlider.java
com.googlecode.android.widgets.DateSlider.Demo.java
com.googlecode.android.widgets.DateSlider.FacultyCabin.java
com.googlecode.android.widgets.DateSlider.Faculty.java
com.googlecode.android.widgets.DateSlider.GAttendance.java
com.googlecode.android.widgets.DateSlider.GDBAdapter5.java
com.googlecode.android.widgets.DateSlider.GDBAdapter6.java
com.googlecode.android.widgets.DateSlider.GDBAdapter.java
com.googlecode.android.widgets.DateSlider.GFaculty.java
com.googlecode.android.widgets.DateSlider.GMarks.java
com.googlecode.android.widgets.DateSlider.GStudent.java
com.googlecode.android.widgets.DateSlider.GuestLogin.java
com.googlecode.android.widgets.DateSlider.GuestMainscreen.java
com.googlecode.android.widgets.DateSlider.Login.java
com.googlecode.android.widgets.DateSlider.Mainscreen.java
com.googlecode.android.widgets.DateSlider.MarkAttendance.java
com.googlecode.android.widgets.DateSlider.Marks.java
com.googlecode.android.widgets.DateSlider.MinimalDemo.java
com.googlecode.android.widgets.DateSlider.MonthYearDateSlider.java
com.googlecode.android.widgets.DateSlider.MyService2.java
com.googlecode.android.widgets.DateSlider.MyService4.java
com.googlecode.android.widgets.DateSlider.MyService5.java
com.googlecode.android.widgets.DateSlider.MyService.java
com.googlecode.android.widgets.DateSlider.ObservableScrollView2.java
com.googlecode.android.widgets.DateSlider.ObservableScrollView.java
com.googlecode.android.widgets.DateSlider.QuizRem3.java
com.googlecode.android.widgets.DateSlider.QuizRem4.java
com.googlecode.android.widgets.DateSlider.Quiz.java
com.googlecode.android.widgets.DateSlider.Refresh.java
com.googlecode.android.widgets.DateSlider.SMainScreen.java
com.googlecode.android.widgets.DateSlider.ScrollLayout.java
com.googlecode.android.widgets.DateSlider.ScrollViewListener2.java
com.googlecode.android.widgets.DateSlider.ScrollViewListener.java
com.googlecode.android.widgets.DateSlider.ServiceManager.java
com.googlecode.android.widgets.DateSlider.Settings.java
com.googlecode.android.widgets.DateSlider.SimpleGestureFilter.java
com.googlecode.android.widgets.DateSlider.SliderContainer.java
com.googlecode.android.widgets.DateSlider.Student.java
com.googlecode.android.widgets.DateSlider.TimeObject.java
com.googlecode.android.widgets.DateSlider.TimeSlider.java
com.googlecode.android.widgets.DateSlider.ViewflipActivity.java
com.googlecode.android.widgets.DateSlider.labeler.DayDateLabeler.java
com.googlecode.android.widgets.DateSlider.labeler.DayLabeler.java
com.googlecode.android.widgets.DateSlider.labeler.HourLabeler.java
com.googlecode.android.widgets.DateSlider.labeler.Labeler.java
com.googlecode.android.widgets.DateSlider.labeler.MinuteLabeler.java
com.googlecode.android.widgets.DateSlider.labeler.MonthLabeler.java
com.googlecode.android.widgets.DateSlider.labeler.MonthYearLabeler.java
com.googlecode.android.widgets.DateSlider.labeler.TimeLabeler.java
com.googlecode.android.widgets.DateSlider.labeler.Util.java
com.googlecode.android.widgets.DateSlider.labeler.WeekLabeler.java
com.googlecode.android.widgets.DateSlider.labeler.YearLabeler.java
com.googlecode.android.widgets.DateSlider.timeview.DayTimeLayoutView.java
com.googlecode.android.widgets.DateSlider.timeview.TimeLayoutView.java
com.googlecode.android.widgets.DateSlider.timeview.TimeTextView.java
com.googlecode.android.widgets.DateSlider.timeview.TimeView.java
com.googlecode.android.widgets.DateSlider.global.java
com.googlecode.android.widgets.DateSlider.grades.java
com.googlecode.android.widgets.DateSlider.notif2.java
com.googlecode.android.widgets.DateSlider.notif.java
com.googlecode.android.widgets.DateSlider.pbl.java
com.googlecode.android.widgets.DateSlider.sample.java
in.ac.vit.vitdroid.DBAdaptergrades.java
in.ac.vit.vitdroid.DBAdapterlinks.java