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);
}
}
}
时间工具类
最新推荐文章于 2024-10-03 08:33:02 发布
该Java代码实现了一个时间工具类,包含计算两个日期之间相隔的天数、获取指定日期所在月份的第一天和最后一天,以及获取两个日期之间所有月份的方法。示例展示了如何使用这些功能。

1172

被折叠的 条评论
为什么被折叠?



