• 日期操作,方法收藏


    package com;


    import java.math.BigDecimal;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;

    //import org.apache.log4j.Logger;
    import org.springframework.jdbc.core.JdbcTemplate;

    import com.hotent.core.util.AppUtil;
    import com.runqianapp.group.domain.newGroupSheetLoader;

    public class HyReportUtil {
    // private Logger log = Logger.getLogger(HyReportUtil.class);
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

    /**
    * 获取年的第几天,getDAY_OF_YEAR
    */
    public static int getDAY_OF_YEAR(String rq) throws Exception {
    int num =0;
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(rq));
    num=c.get(Calendar.DAY_OF_YEAR);
    return num;
    }
    /**
    * 获取月的第几天,getDAY_OF_MONTH
    */
    public static int getDAY_OF_MONTH(String rq) throws Exception {
    int num =0;
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(rq));
    num=c.get(Calendar.DAY_OF_MONTH);
    return num;
    }

    /**
    * 字符串的日期加上一天,在返回字符串
    */
    public static String addOneDay(String countdate) throws Exception {
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(countdate));
    c.add(Calendar.DATE, 1);
    String rtn = sdf.format(c.getTime());
    return rtn;
    }

    /**
    * 字符串的日期减去一天,在返回字符串
    */
    public static String subOneDay(String countdate) throws Exception {
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(countdate));
    c.add(Calendar.DATE, -1);
    String rtn = sdf.format(c.getTime());
    return rtn;
    }

    /**
    * @param rq 字符串的日期 格式 yyyy-MM-dd
    * @param num
    * @return 添加或减去它的天数,在返回字符串
    * @throws Exception
    * String
    *
    */
    public static String addOrSubRq(String rq,int num) throws Exception {
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(rq));
    c.add(Calendar.DATE, num);
    String rtn = sdf.format(c.getTime());
    return rtn;
    }

    /**
    * @param tjrq
    * @return yyyy-MM-dd 格式化为 yyyyMMdd
    */
    public static String formatDateForyyyyMMdd(String tjrq) throws Exception {
    Date date = sdf.parse(tjrq);
    tjrq = new SimpleDateFormat("yyyyMMdd").format(date);
    return tjrq;
    }

    /**
    * @param tjrq
    * @return yyyy-MM-dd 格式化为 yyyy
    */
    public static String formatDateForYYYY(String tjrq) throws Exception {
    Date date = sdfYear.parse(tjrq);
    tjrq = new SimpleDateFormat("yyyy").format(date);
    return tjrq;
    }
    /**
    * @param tjrq
    * @return yyyy-MM-dd 格式化为 yyyyMM
    */
    public static String formatDateForyyyyMM(String tjrq) throws Exception {
    Date date = sdf.parse(tjrq);
    tjrq = new SimpleDateFormat("yyyyMM").format(date);
    return tjrq;
    }

    /**
    * @param tjrq
    * @return 将字符串日期减去一年
    * @throws Exception
    * String
    *
    */
    public static String subOneYearForDate(String tjrq) throws Exception {
    Date date = sdf.parse(tjrq);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.YEAR, -1);
    tjrq = sdf.format(calendar.getTime());

    return tjrq;
    }

    /**
    * @param tjrq
    * @return 将字符串日期减去一个月
    * @throws Exception
    * String
    *
    */
    public static String subOneMonthForDate(String tjrq) throws Exception {
    Date date = sdf.parse(tjrq);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, -1);
    tjrq = sdf.format(calendar.getTime());
    return tjrq;
    }

    /**
    * @param tjrq 参数格式为yyyy-MM-dd
    * @param type 参数 只能为month 或year
    * @throws Exception
    * 循环遍历,当月或当年日期,并返回list集合
    *
    */
    public static List<String> traversalMonthOrYear(String tjrq,String type) throws Exception {
    List<String> list= new ArrayList<String>();
    Calendar c = Calendar.getInstance();
    int dayNum = 0;
    if ("month".equals(type)) {
    dayNum = HyReportUtil.getDAY_OF_MONTH(sdf.format(c.getTime()));
    }else if("year".equals(type)){
    dayNum = HyReportUtil.getDAY_OF_YEAR(sdf.format(c.getTime()));
    }
    for (int i = 1; i < dayNum; i++) {
    c.add(Calendar.DATE, -1);
    list.add(sdf.format(c.getTime()));
    }
    return list;

    }


    /**
    * @param args
    * @throws Exception
    * void
    *
    */
    public static void main(String[] args) throws Exception {

    // traversalMonthOrYear("2020-03-08","month");

    System.out.println(traversalMonthOrYear("2020-03-08","year"));

    // System.out.println(getDAY_OF_MONTH("2020-03-08"));
    // System.out.println(getDAY_OF_YEAR("2020-03-08"));
    // System.out.println(getDAY_OF_YEAR("2019-12-25"));

    }
    }

  • 相关阅读:
    python 去除字符串两端字符串
    python 找到列表中满足某些条件的元素
    python join函数
    Ambiguous mapping. Cannot map "***Controller" been method解决办法
    uflo2安装与配置
    uflo2概述
    Mybatis-plus中的常用注解
    Spring Cloud Eureka配置文件详解 (还没细看)
    idea安装lombok
    PowerDesigner最基础的使用方法入门学习(一)
  • 原文地址:https://www.cnblogs.com/rdchen/p/9621173.html
Copyright © 2020-2023  润新知