Java 输入一个字符串格式日期,获取对应的自然月开始结束时间,对应的自然年开始结束时间
public static Map getMonthStartTimeByDate(String dateTime) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(dateTime);
long currentTime = date.getTime();
String timeZone = "GMT+8:00";
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(currentTime);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为 1 号,当前日期既为本月第一天
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long timeInMillis = calendar.getTimeInMillis();
Date resultStartDate = new Date(timeInMillis);
String resultStartTime = simpleDateFormat.format(resultStartDate);
Calendar calendar2 = Calendar.getInstance();// 获取当前日期
calendar2.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar2.setTimeInMillis(currentTime);
calendar2.add(Calendar.YEAR, 0);
calendar2.add(Calendar.MONTH, 0);
calendar2.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
calendar2.set(Calendar.HOUR_OF_DAY, 23);
calendar2.set(Calendar.MINUTE, 59);
calendar2.set(Calendar.SECOND, 59);
calendar2.set(Calendar.MILLISECOND, 999);
long timeInMillis2 = calendar2.getTimeInMillis();
Date resultEndDate = new Date(timeInMillis2);
String resultEndTime = simpleDateFormat.format(resultEndDate);
Map map = new HashMap();
map.put("startDate", resultStartTime);
map.put("endDate", resultEndTime);
return map;
}
//输入一个时间,获取对应的自然年开始时间,结束时间
public static Map getYearStartTimeByDate(String dateTime) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateForma 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 t.parse(dateTime);
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.valueOf(gc.get(1)));
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
Long dayStartTime = getDayStartTime(cal.getTime());
Date resultStartDate = new Date(dayStartTime);
String resultStartTime = simpleDateFormat.format(resultStartDate);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.YEAR, Integer.valueOf(gc.get(1)));
cal2.set(Calendar.MONTH, Calendar.DECEMBER);
cal2.set(Calendar.DATE, 31);
Long dayEndTime = getDayStartTime(cal2.getTime());
Date resultEndDate = new Date(dayEndTime);
String resultEndTime = simpleDateFormat.format(resultEndDate);
Map map = new HashMap();
map.put("startDate", resultStartTime);
map.put("endDate", resultEndTime);
return map;
}
评论