Java日期查询:日、周、旬、月、季度、年等时间操作

在进行频次、周期业务处理的时候,需要查询日、周、旬、月、季度、年等周期时间,为此而写的一个时间工具类。


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

/**
 * 日、星期(周)、旬、月、季度、年等时间工具类
 */
public class DateUtil {

    private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");


    /**
     * 根据要求的格式,格式化时间,返回String
     *
     * @param format 默认:yyyy-MM-dd HH:mm:ss
     * @param time   要格式化的时间
     * @return 时间字符串
     */
    public static String toStr(String format, Date time) {
        SimpleDateFormat df = null;
        if (null == format) {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        } else {
            df = new SimpleDateFormat(format);
        }
        try {
            return df.format(time);
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 字符串转时间
     *
     * @param source yyyy-MM-dd HH:mm:ss.SSS 格式的字符串
     * @return
     */
    public static Date toDate(String source) {
        String formatString = "yyyy-MM-dd hh:mm:ss";
        if (source == null || "".equals(source.trim())) {
            return null;
        }
        source = source.trim();
        if (source.matches("^\\d{4}$")) {
            formatString = "yyyy";
        } else if (source.matches("^\\d{4}-\\d{1,2}$")) {
            formatString = "yyyy-MM";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
            formatString = "yyyy-MM-dd";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}$")) {
            formatString = "yyyy-MM-dd hh";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
            formatString = "yyyy-MM-dd hh:mm";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
            formatString = "yyyy-MM-dd hh:mm:ss";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}\\.\\d{1,3}$")) {
            formatString = "yyyy-MM-dd HH:mm:ss.SSS";
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(formatString);
            Date date = sdf.parse(source);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获得本小时的开始时间
     *
     * @return
     */
    public static Date getHourStartTime(Date date) {
        Date dt = null;
        try {
            dt = longHourSdf.parse(longHourSdf.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 获得本小时的结束时间
     *
     * @return
     */
    public static Date getHourEndTime(Date date) {
        Date dt = null;
        try {
            dt = longSdf.parse(longHourSdf.format(date) + ":59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 获得本天的开始时间
     *
     * @return
     */
    public static Date getDayStartTime(Date date) {
        Date dt = null;
        try {
            dt = shortSdf.parse(shortSdf.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 获得本天的结束时间
     *
     * @return
     */
    public static Date getDayEndTime(Date date) {
        Date dt = null;
        try {
            dt = longSdf.parse(shortSdf.format(date) + " 23:59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 当前时间是星期几
     *
     * @return
     */
    public static int getWeekDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week_of_year = c.get(Calendar.DAY_OF_WEEK);
        return week_of_year - 1;
    }

    /**
     * 获得本周的第一天,周一
     *
     * @return
     */
    public static Date getWeekStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00.000"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得本周的最后一天,周日
     *
     * @return
     */
    public static Date getWeekEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 8 - weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59.999"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得本月的开始时间
     *
     * @return
     */
    public static Date getMonthStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date dt = null;
        try {
            c.set(Calendar.DATE, 1);
            dt = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 本月的结束时间
     *
     * @return
     */
    public static Date getMonthEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date dt = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            dt = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 当前年的开始时间
     *
     * @return
     */
    public static Date getYearStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date dt = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            dt = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 当前年的结束时间
     *
     * @return
     */
    public static Date getYearEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date dt = null;
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            dt = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 当前季度的开始时间
     *
     * @return
     */
    public static Date getQuarterStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date dt = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            dt = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00.000");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 当前季度的结束时间
     *
     * @return
     */
    public static Date getQuarterEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date dt = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            dt = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 获取前/后半年的开始时间
     *
     * @return
     */
    public static Date getHalfYearStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date dt = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 0);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 6);
            }
            c.set(Calendar.DATE, 1);
            dt = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00.000");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;

    }

    /**
     * 获取前/后半年的结束时间
     *
     * @return
     */
    public static Date getHalfYearEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date dt = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            dt = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 获取月旬 三旬: 上旬1-10日 中旬11-20日 下旬21-31日
     *
     * @param date
     * @return
     */
    public static int getTenDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int i = c.get(Calendar.DAY_OF_MONTH);
        if (i < 11)
            return 1;
        else if (i < 21)
            return 2;
        else
            return 3;
    }


    /**
     * 获取所属旬开始时间
     *
     * @param date
     * @return
     */
    public static Date getTenDayStartTime(Date date) {
        int ten = getTenDay(date);
        try {
            if (ten == 1) {
                return getMonthStartTime(date);
            } else if (ten == 2) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-11");
                return shortSdf.parse(df.format(date));
            } else {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-21");
                return shortSdf.parse(df.format(date));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;


    }

    /**
     * 获取所属旬结束时间
     *
     * @param date
     * @return
     */
    public static Date getTenDayEndTime(Date date) {
        int ten = getTenDay(date);
        try {
            if (ten == 1) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-10 23:59:59.999");
                return longSdf.parse(df.format(date));
            } else if (ten == 2) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-20 23:59:59.999");
                return longSdf.parse(df.format(date));
            } else {
                return getMonthEndTime(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;


    }


    /**
     * 属于本年第几天
     *
     * @return
     */
    public static int getYearDayIndex(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 属于本年第几周
     *
     * @return
     */
    public static int getYearWeekIndex(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 属于本年第几月
     *
     * @return
     */
    public static int getYearMonthIndex(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 当前属于本年第几个季度
     *
     * @return
     */
    public static int getYeartQuarterIndex(Date date) {
        int month = getYearMonthIndex(date);
        if (month <= 3)
            return 1;
        else if (month <= 6)
            return 2;
        else if (month <= 9)
            return 3;
        else
            return 4;
    }

    /**
     * 获取date所属年的所有天列表及开始/结束时间 开始时间:date[0],结束时间:date[1]
     *
     * @param date
     * @return
     */
    public static List<Date[]> yearDayList(Date date) {
        List<Date[]> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getYearStartTime(date);
        Date endtm = getYearEndTime(date);
        calendar.setTime(starttm);

        while (calendar.getTime().before(endtm)) {
            Date st = getDayStartTime(calendar.getTime());
            Date et = getDayEndTime(calendar.getTime());
            result.add(new Date[]{st, et});
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;

    }

    /**
     * 获取date所属年的所有星期列表及开始/结束时间 开始时间:date[0],结束时间:date[1]
     *
     * @param date
     * @return
     */
    public static List<Date[]> yearWeekList(Date date) {
        List<Date[]> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getYearStartTime(date);
        Date endtm = getYearEndTime(date);
        calendar.setTime(starttm);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        while (calendar.getTime().before(endtm)) {
            Date st = getWeekStartTime(calendar.getTime());
            Date et = getWeekEndTime(calendar.getTime());
            result.add(new Date[]{st, et});
            calendar.add(Calendar.WEEK_OF_YEAR, 1);
        }
        return result;

    }

    /**
     * 获取date所属年的所有月列表及开始/结束时间 开始时间:date[0],结束时间:date[1]
     *
     * @param date
     * @return
     */
    public static List<Date[]> yearMonthList(Date date) {
        List<Date[]> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getYearStartTime(date);
        Date endtm = getYearEndTime(date);
        calendar.setTime(starttm);
        while (calendar.getTime().before(endtm)) {
            Date tm = calendar.getTime();
            Date st = getMonthStartTime(tm);
            Date et = getMonthEndTime(tm);
            result.add(new Date[]{st, et});
            calendar.add(Calendar.MONTH, 1);
        }
        return result;
    }

    /**
     * 获取date所属年的所有季度列表及开始/结束时间 开始时间:date[0],结束时间:date[1]
     *
     * @param date
     * @return
     */
    public static List<Date[]> yearQuarterList(Date date) {
        List<Date[]> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getYearStartTime(date);
        Date endtm = getYearEndTime(date);
        calendar.setTime(starttm);
        while (calendar.getTime().before(endtm)) {
            Date st = getQuarterStartTime(calendar.getTime());
            Date et = getQuarterEndTime(calendar.getTime());
            result.add(new Date[]{st, et});
            calendar.add(Calendar.MONTH, 3);
        }
        return result;
    }

    /**
     * 获取date所属月份的所有旬列表及开始/结束时间 开始时间:date[0],结束时间:date[1]
     *
     * @param date
     * @return
     */
    public static List<Date[]> monthTenDayList(Date date) {
        List<Date[]> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getMonthStartTime(date);
        Date endtm = getMonthEndTime(date);
        calendar.setTime(starttm);

        while (calendar.getTime().before(endtm)) {
            Date st = getTenDayStartTime(calendar.getTime());
            Date et = getTenDayEndTime(calendar.getTime());
            result.add(new Date[]{st, et});
            calendar.add(Calendar.DAY_OF_MONTH, 11);
        }
        return result;
    }

    /**
     * 获取date所属年的所有月旬列表及开始/结束时间 开始时间:date[0],结束时间:date[1]
     *
     * @param date
     * @return
     */
    public static List<Date[]> yearTenDayList(Date date) {
        List<Date[]> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getYearStartTime(date);
        Date endtm = getYearEndTime(date);
        calendar.setTime(starttm);

        while (calendar.getTime().before(endtm)) {//
            result.addAll(monthTenDayList(calendar.getTime()));
            calendar.add(Calendar.MONTH, 1);
        }
        return result;
    }

    /**
     * 测试
     */
    private static void test() {
        Date date = new Date();
        System.out.println("当前小时开始:" + toStr(null, getHourStartTime(date)));
        System.out.println("当前小时结束:" + toStr(null, getHourEndTime(date)));
        System.out.println("当前天开始:" + toStr(null, getDayStartTime(date)));
        System.out.println("当前天时结束:" + toStr(null, getDayEndTime(date)));
        System.out.println("当前天是星期:" + getWeekDay(date));
        System.out.println("当前周开始:" + toStr(null, getWeekStartTime(date)));
        System.out.println("当前周结束:" + toStr(null, getWeekEndTime(date)));
        System.out.println("当前月开始:" + toStr(null, getMonthStartTime(date)));
        System.out.println("当前月结束:" + toStr(null, getMonthEndTime(date)));
        System.out.println("当前季度开始:" + toStr(null, getQuarterStartTime(date)));
        System.out.println("当前季度结束:" + toStr(null, getQuarterEndTime(date)));
        System.out.println("当前半年/后半年开始:" + toStr(null, getHalfYearStartTime(date)));
        System.out.println("当前半年/后半年结束:" + toStr(null, getHalfYearEndTime(date)));
        System.out.println("当前年开始:" + toStr(null, getYearStartTime(date)));
        System.out.println("当前年结束:" + toStr(null, getYearEndTime(date)));
        System.out.println("当前属于本年第:" + getYearDayIndex(date) + "天");
        System.out.println("当前属于本年第:" + getYearWeekIndex(date) + "周");
        System.out.println("当前属于本年第:" + getYearMonthIndex(date) + "月");
        System.out.println("当前属于本年第:" + getYeartQuarterIndex(date) + "季度");
        System.out.println("时间转换(yyyy): " + toStr(null, toDate("2018")));
        System.out.println("时间转换(yyyy-MM): " + toStr(null, toDate("2018-01")));
        System.out.println("时间转换(yyyy-MM-dd): " + toStr(null, toDate("2018-01-01")));
        System.out.println("时间转换(yyyy-MM-dd hh): " + toStr(null, toDate("2018-01-01 23")));
        System.out.println("时间转换(yyyy-MM-dd hh:mm): " + toStr(null, toDate("2018-01-01 23:59")));
        System.out.println("时间转换(yyyy-MM-dd hh:mm:ss): " + toStr(null, toDate("2018-01-01 23:59:59")));
        System.out.println("时间转换(yyyy-MM-dd HH:mm:ss.SSS): " + toStr(null, toDate("2018-01-01 23:59:59.999")));
    }

    /**
     * 测试:获取当年所有日期列表
     */
    private static void testYearDayList() {
        List<Date[]> datas = yearDayList(new Date());
        for (int i = 0; i < datas.size(); i++) {
            Date[] date = datas.get(i);
            System.out.println("(一年的日期列表)第" + (i + 1) + "天:" + sdf.format(date[0]) + " " + sdf.format(date[1]));
        }
    }

    /**
     * 测试:获取当年所有星期列表
     */
    private static void testYearWeekList() {
        List<Date[]> datas = yearWeekList(new Date());
        for (int i = 0; i < datas.size(); i++) {
            Date[] date = datas.get(i);
            System.out.println("(一年的周列表)第" + (i + 1) + "周:" + sdf.format(date[0]) + " " + sdf.format(date[1]));
        }
    }

    /**
     * 测试:获取当年所有季度列表
     */
    private static void testYearQuarterList() {
        List<Date[]> datas = yearQuarterList(new Date());
        for (int i = 0; i < datas.size(); i++) {
            Date[] date = datas.get(i);
            System.out.println("(一年的季度列表)第" + (i + 1) + "季度:" + sdf.format(date[0]) + " " + sdf.format(date[1]));
        }
    }


    /**
     * 测试:获取当年所有月份列表
     */
    private static void testYearMonthList() {
        List<Date[]> datas = yearMonthList(new Date());
        for (int i = 0; i < datas.size(); i++) {
            Date[] date = datas.get(i);
            System.out.println("(一年的月列表)第" + (i + 1) + "月:" + sdf.format(date[0]) + " " + sdf.format(date[1]));
        }
    }


    /**
     * 测试:获取当月所有旬列表
     */
    private static void testMonthTenDayList() {
        //Date no= DateTimeTools.toDateTime("2018-02-01 15:38:15");
        List<Date[]> datas = monthTenDayList(new Date());
        for (int i = 0; i < datas.size(); i++) {
            Date[] date = datas.get(i);
            System.out.println("(一月的旬列表)第" + (i % 3 + 1) + "旬:" + sdf.format(date[0]) + " " + sdf.format(date[1]));
        }
    }

    /**
     * 测试:获取当年所有旬列表
     */
    private static void testyearTenDayList() {
        List<Date[]> datas = yearTenDayList(new Date());
        for (int i = 0; i < datas.size(); i++) {
            Date[] date = datas.get(i);
            System.out.println("(一年的旬列表)第" + (i / 3 + 1) + "月" + (i % 3 + 1) + "旬:" + sdf.format(date[0]) + " " + sdf.format(date[1]));
        }
    }

    public static void main(String[] args) {
        test();
        testYearDayList();
        testYearWeekList();
        testYearMonthList();
        testYearQuarterList();
        testyearTenDayList();
        testMonthTenDayList();
    }


}


小礼物走一走,来简书关注我



作者:天行无迹
链接:https://www.jianshu.com/p/16694b0aa265
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用 Java 中的 Calendar 类来获取两个时间点之间的所有日期,具体步骤如下: 1. 首先,需要将两个时间点转换为 Calendar 对象,并获取它们的等信息。 2. 然后,通过循环遍历两个时间点之间的所有日期,判断每个日期是否为一,如果是一则输出该日期信息。 下面是一个示例代码: ```java import java.util.Calendar; public class Main { public static void main(String[] args) { Calendar start = Calendar.getInstance(); start.set(2022, Calendar.JANUARY, 1); // 起始日期 Calendar end = Calendar.getInstance(); end.set(2022, Calendar.DECEMBER, 31); // 结束日期 Calendar current = start; while (current.compareTo(end) <= 0) { if (current.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) { int year = current.get(Calendar.YEAR); int month = current.get(Calendar.MONTH) + 1; int day = current.get(Calendar.DAY_OF_MONTH); System.out.println(year + "-" + month + "-" + day); } current.add(Calendar.DATE, 1); } } } ``` 在这个示例中,我们首先定义了起始日期和结束日期,然后使用 Calendar.getInstance() 方法获取 Calendar 实例,并将起始日期和结束日期设置到 Calendar 实例中。接着,我们定义一个 current 变量,用于循环遍历起始日期和结束日期之间的所有日期,判断每个日期是否为一,如果是一则输出该日期信息。需要注意的是,在循环中,我们每次将 current 变量加一天,直到到达结束日期为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值