时间格式截取工具年月日

121 阅读1分钟

public class DateStringUtils {

private DateStringUtils(){}

/**
 * @Description: 获取今年和本月的拼接串 2021-03
 * @Author: 
 */
public static String getYearMonthString(Date statisticDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    String year = ((Integer)calendar.get(Calendar.YEAR)).toString() + "-";
    Integer month = calendar.get(Calendar.MONTH) + 1;
    if (month < 10){
        return year + "0" + month;
    }else {
        return year + month;
    }
}

/**
 * @Description: 获当前月份 integer类型 1 2 3 4 5 6 7 8 9 10 11 12
 * @Author: 
 */
public static Integer getMonthInteger(Date statisticDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    return calendar.get(Calendar.MONTH) + 1;
}

/**
 * @Description: 获当前月份字符串 01 02 03 04 05 06 07 08 09 10 11 12
 * @Author: 
 */
public static String getMonthString(Date statisticDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    Integer month = calendar.get(Calendar.MONTH) + 1;
    if (month<10){
        return "0" + month;
    }else {
        return month.toString();
    }
}

/**
 * @Description: 获取今年字符串 2021
 * @Author: 
 */
public static String getYearString(Date statisticDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    return ((Integer)calendar.get(Calendar.YEAR)).toString();
}

/**
 * @Description: 获取今年字符串 2021
 * @Author: 
 */
public static Integer getYearInteger(Date statisticDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    return calendar.get(Calendar.YEAR);
}

/**
 * @Description: 获取 本年份 减去 number 的字符串 number=1就是去年 number=2就是前年
 * @Author: 
 */
public static String getBeforeYearString(Date statisticDate, int number) {
    number = 0 - number;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    calendar.add(Calendar.YEAR, number);
    Integer year = calendar.get(Calendar.YEAR);
    return year.toString();
}

/**
 * @Description: 获取 本年份 减去 number 的整数 number=1就是去年 number=2就是前年
 * @Author: 
 */
public static Integer getBeforeYearInteger(Date statisticDate, int number) {
    number = 0 - number;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    calendar.add(Calendar.YEAR, number);
    return calendar.get(Calendar.YEAR);
}

/**
 * @Description: 获取 本月份 减去 number 的字符串 number=1就是上个月 number=2就是前2个月  01 02 03 04 05 06 07 08 09 10 11 12
 * @Author: 
 */
public static String getBeforeMonthString(Date statisticDate, int number) {
    number = 0 - number;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    calendar.add(Calendar.MONTH, number);
    Integer month = calendar.get(Calendar.MONTH) + 1;
    if (month<10){
        return "0" + month;
    }else {
        return month.toString();
    }
}

/**
 * @Description: 获取 本月份 减去 number 的整数 number=1就是上个月 number=2就是前2个月 1 2 3 4 5 6 7 8 9 10 11 12
 * @Author: 
 */
public static Integer getBeforeMonthInteger(Date statisticDate, int number) {
    number = 0 - number;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    calendar.add(Calendar.MONTH, number);
    return calendar.get(Calendar.MONTH) + 1;
}
public static String getYearMonthDayString(Date statisticDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    String year = ((Integer) calendar.get(Calendar.YEAR)).toString() + "-";
    Integer month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    if (month < 10 && day < 10) {
        return year + "0" + month + "-0" + day;

    } else if (month < 10 && day >= 10) {
        return year + "0" + month +"-"+ day;
    }else if(month>=10 && day<10){
        return year + month + "-0" +day;
    }else {
        return year + month+"-"+day;
    }
}

/**
 * 获取上一个月的月份针对于当前是一月份的特殊情况
 */
public static String getYearMonthCalendar(Date statisticDate, int number) {
    number = 0 - number;
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(statisticDate);
    calendar.add(Calendar.MONTH, number);
    int month = calendar.get(Calendar.MONTH)+1;
    if(month<10){
        return calendar.get(Calendar.YEAR)+"-"+"0"+month;
    }else {
        return calendar.get(Calendar.YEAR) + "-" + month;
    }
}

}