Android 日期 Calendar 关于时间段的操作

根据一个传递过来的日期,得到指定类型的时间段:例如,传递过来一个日期,并指定一个类型(周、月份、季度、半年、年),返回一个Timespan,包含开始日期和结束日期.


类型枚举:


public enum TimeTypeEnum {
    Year(1),
    HalfYear(2),
    Quarter(3),
    Month(4),
    Week(5),
    Day(6);
    
    private int mValue;

    TimeTypeEnum(int value) {
        mValue = value;
    }

    public int getValue() {
        return mValue;
    }
}


然后是主方法的返回类型:


public class TimeSpan {
    public Date startDate;
    public Date endDate;
    @Override
    public String toString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return String.format("%s - %s",  format.format(startDate),format.format(endDate));
    }
}

最后是主类:

package com.example.terry;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 *
* Created by 程腾光 on 2016/10/26
 */

public class JetDateUtil {
    static Calendar calendar = Calendar.getInstance(Locale.CHINESE);

    public static TimeSpan getTimeSpan(Date givenDate, TimeTypeEnum timeTypeEnum) {
        switch (timeTypeEnum) {
            case Week:
                return getWeekTimeSpan(givenDate);
            case Month:
                return getMonthTimeSpan(givenDate);
            case Quarter:
                return getQuarterTimeSpan(givenDate);
            case HalfYear:
                return getHalfYearTimeSpan(givenDate);
            case Year:
                return getYearTimeSpan(givenDate);
            default:
                return getYearTimeSpan(givenDate);
        }
    }

    /**
     * 得到指定日期的所在星期的时间周期
     *
     * @param givenDate
     * @return
     */
    public static TimeSpan getWeekTimeSpan(Date givenDate) {
        calendar.setTime(givenDate);
        TimeSpan timespan = new TimeSpan();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        timespan.startDate = calendar.getTime();
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        timespan.endDate = calendar.getTime();
        return timespan;
    }

    /**
     * 得到指定日期的所在月份的时间周期
     *
     * @param givenDate
     * @return
     */
    public static TimeSpan getMonthTimeSpan(Date givenDate) {
        calendar.setTime(givenDate);
        TimeSpan timespan = new TimeSpan();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        timespan.startDate = calendar.getTime();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        timespan.endDate = calendar.getTime();
        return timespan;
    }
    
    /**
     * 得到指定日期的所在季度的时间周期
     *
     * @param givenDate
     * @return
     */
    public static TimeSpan getQuarterTimeSpan(Date givenDate) {
        calendar.setTime(givenDate);
        TimeSpan timeSpan = new TimeSpan();
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month >= 1 && month <= 3) { // given date is belong the first half year.
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            timeSpan.startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, Calendar.MARCH);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            timeSpan.endDate = calendar.getTime();

        } else if (month >= 4 && month <= 6) {// given date is belong the last half year.
            calendar.set(Calendar.MONTH, Calendar.APRIL);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            timeSpan.startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, Calendar.JUNE);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            timeSpan.endDate = calendar.getTime();
        } else if (month >= 7 && month <= 9) {// given date is belong the last half year.
            calendar.set(Calendar.MONTH, Calendar.JULY);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            timeSpan.startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, Calendar.SEPTEMBER);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            timeSpan.endDate = calendar.getTime();
        } else if (month >= 10 && month <= 12) {// given date is belong the last half year.
            calendar.set(Calendar.MONTH, Calendar.OCTOBER);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            timeSpan.startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, Calendar.DECEMBER);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            timeSpan.endDate = calendar.getTime();
        }
        return timeSpan;
    }

    /**
     * 得到指定日期的所在半年的时间周期
     *
     * @param givenDate
     * @return
     */
    public static TimeSpan getHalfYearTimeSpan(Date givenDate) {
        calendar.setTime(givenDate);
        TimeSpan timeSpan = new TimeSpan();
        int month = calendar.get(Calendar.MONTH) + 1; //得到的月份是从0开始的.
        if (month >= 1 && month <= 6) { // given date is belong the first half year.
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            timeSpan.startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, Calendar.JUNE);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            timeSpan.endDate = calendar.getTime();
        } else {// given date is belong the last half year.
            calendar.set(Calendar.MONTH, Calendar.JULY);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            timeSpan.startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, Calendar.DECEMBER);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            timeSpan.endDate = calendar.getTime();
        }
        return timeSpan;
    }

    /**
     * 得到指定日期的所在年的时间周期
     *
     * @param givenDate
     * @return
     */
    public static TimeSpan getYearTimeSpan(Date givenDate) {
        calendar.setTime(givenDate);
        TimeSpan timeSpan = new TimeSpan();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_YEAR));
        timeSpan.startDate = calendar.getTime();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        timeSpan.endDate = calendar.getTime();
        return timeSpan;
    }
}







你可能感兴趣的:(Android学习记录)