时间工具类

该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 TimeUtils {

    // 第1个方法:计算两个时间相隔的天数
    public static long getDaysBetween(String startTimeStr, String endTimeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = sdf.parse(startTimeStr);
            Date endDate = sdf.parse(endTimeStr);

            long diff = endDate.getTime() - startDate.getTime();
            return diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

    // 第2个方法:获取指定时间的当前月的第一天和最后一天
    public static String[] getFirstAndLastDayOfMonth(String timeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(timeStr);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1; // Month is 0-based in Calendar, so we add 1

            // Get the first day of the month
            String firstDayOfMonth = year + "-" + String.format("%02d", month) + "-01 00:00:00";

            // Get the last day of the month
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            String lastDayOfMonth = sdf.format(calendar.getTime());

            return new String[]{firstDayOfMonth, lastDayOfMonth};
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 第3个方法:计算两个时间间隔的所有月份
    public static List<String> getAllMonthsBetween(String startTimeStr, String endTimeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> monthsList = new ArrayList<>();

        try {
            Date startDate = sdf.parse(startTimeStr);
            Date endDate = sdf.parse(endTimeStr);

            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            startCalendar.set(Calendar.DAY_OF_MONTH, 1); // Set to the first day of the month

            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            endCalendar.set(Calendar.DAY_OF_MONTH, 1); // Set to the first day of the month

            while (startCalendar.getTimeInMillis() <= endCalendar.getTimeInMillis()) {
                int year = startCalendar.get(Calendar.YEAR);
                int month = startCalendar.get(Calendar.MONTH) + 1; // Month is 0-based in Calendar, so we add 1
                String monthStr = year + "-" + String.format("%02d", month);
                monthsList.add(monthStr);

                startCalendar.add(Calendar.MONTH, 1); // Move to the next month
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return monthsList;
    }

    // Test the methods
    public static void main(String[] args) {
        String startTimeStr = "2023-07-15 12:00:00";
        String endTimeStr = "2024-02-20 18:30:00";
        long daysBetween = getDaysBetween(startTimeStr, endTimeStr);
        System.out.println("Days between: " + daysBetween);

        String timeStr = "2023-07-23 14:25:30";
        String[] firstAndLastDay = getFirstAndLastDayOfMonth(timeStr);
        if (firstAndLastDay != null) {
            System.out.println("First day of the month: " + firstAndLastDay[0]);
            System.out.println("Last day of the month: " + firstAndLastDay[1]);
        }

        List<String> allMonths = getAllMonthsBetween(startTimeStr, endTimeStr);
        System.out.println("All months between:");
        for (String month : allMonths) {
            System.out.println(month);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值