java实现根据指定日期获取今年 去年 本季度 上个季度 本月 上个月的值

日期工具类记录:

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 处理时间的工具类
 */
public class DateUtil {
    /**
     * 获取日期里面的今年
     *
     * @return
     */
    public static String thisYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        return String.valueOf(year);
    }

    /**
     * 去年
     */
    public static String lastYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, -1);
        int year = c.get(Calendar.YEAR);
        return String.valueOf(year);
    }

    /**
     * 当前季度
     */
    public static String thisQuarter(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int quarter = month % 3 == 0 ? month / 3 : (month / 3) + 1;
        return year + "Q" + quarter;
    }

    /**
     * 上个季度
     */
    public static String lastQuarter(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int quarter = month % 3 == 0 ? month / 3 : (month / 3) + 1;
        if (quarter == 1) {
            return (year - 1) + "Q4";
        }
        return year + "Q" + (quarter - 1);
    }

    /**
     * 当前月份
     */
    public static String thisMonth(Date date) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return sd.format(c.getTime());
    }

    /**
     * 上个月
     */
    public static String lastMonth(Date date) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        return sd.format(c.getTime());
    }

    /**
     * 上上个月
     */
    public static String lastTwoMonth(Date date) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -2);
        return sd.format(c.getTime());
    }

    /**
     * 根据传进来的日期计算周期
     */
    public static List<String> dateList(String dateStr) {
        //设定指定日期时使用
        Date date = new Date();
        List<String> dateList = new ArrayList<>();

        if (!dateStr.isEmpty()) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
                date = sdf.parse(dateStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        dateList.add(lastYear(date));
        dateList.add(thisYear(date));
        dateList.add(lastQuarter(date));
        dateList.add(thisQuarter(date));
        dateList.add(lastTwoMonth(date));
        dateList.add(lastMonth(date));
        dateList.add(thisMonth(date));
        return dateList;
    }
}

上面这个写死了,又整了一个优化后的版本,根据偏移值来
优化后的版本

public class DateUtil {
    /**
     * 获取年份
     * 根据偏移值计算 0 不动
     */
    public static String getYear(Date date,int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, offset);
        int year = c.get(Calendar.YEAR);
        return String.valueOf(year);
    }

    /**
     * 获取季度
     */
    public static String getQuarter(Date date,int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int quarter = month % 3 == 0 ? month / 3 : (month / 3) + 1;
        //加入偏移值
        quarter = quarter+offset;
        if (quarter <= 0) {
            return (year - 1) + "Q"+ (4 + quarter );
        }
        return year + "Q" + quarter;
    }

    /**
     * 获取月份
     */
    public static String getMonth(Date date,int offset) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, offset);
        return sd.format(c.getTime());
    }

    /**
     * 根据传进来的日期计算周期
     */
    public static List<String> dataList(String dateStr) {
        //设定指定日期时使用
        Date date = new Date();
        List<String> dateList = new ArrayList<>();

        if (!dateStr.isEmpty()) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
                date = sdf.parse(dateStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        dateList.add(getYear(date,-1));
        dateList.add(getYear(date,0));
        dateList.add(getQuarter(date,-1));
        dateList.add(getQuarter(date,0));
        dateList.add(getMonth(date,-2));
        dateList.add(getMonth(date,-1));
        dateList.add(getMonth(date,0));
        return dateList;
    }

    public static void main(String[] args) {
        System.out.println(dataList(""));
    }

你可能感兴趣的:(小技巧,java)