java中关于年,月,日的查询,整理了一个工具类
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DateUtils {
private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//输入年月,得到开始时间
public static Long getBeginTime(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate localDate = yearMonth.atDay(1);
LocalDateTime startOfDay = localDate.atStartOfDay();
ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
return zonedDateTime.toLocalDateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
}
//输入年月,得到结束时间
public static Long getEndTime(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
return zonedDateTime.toLocalDateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
}
//当前季度的开始时间
public static Long getQuarterStartTime(int year, int month) {
if (month==1){
month =1;
}
if (month==2){
month =5;
}
if (month==3){
month =8;
}
if (month==4){
month =11;
}
Calendar cal = Calendar.getInstance();
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
// 1月从0开始
cal.set(Calendar.MONTH,month);
Date date=cal.getTime();
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
Long ts = 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);
ts = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00.000").getTime();
} catch (Exception e) {
e.printStackTrace();
}
return ts;
}
//当前季度的结束时间
public static Long getQuarterEndTime(int year, int month) {
if (month==1){
month =1;
}
if (month==2){
month =5;
}
if (month==3){
month =8;
}
if (month==4){
month =11;
}
Calendar cal = Calendar.getInstance();
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
// 1月从0开始
cal.set(Calendar.MONTH,month);
Date date=cal.getTime();
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
Long ts = 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);
}
ts = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59.999").getTime();
} catch (Exception e) {
e.printStackTrace();
}
return ts;
}
//获取本年的开始时间
public static Long getBeginDayOfYear(int year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTime());
}
//获取本年的结束时间
public static Long getEndDayOfYear(int year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DATE, 31);
return getDayEndTime(cal.getTime());
}
//获取某个日期的开始时间
public static Long getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
//获取某个日期的结束时间
public static Long getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTimeInMillis();
}
//输入两个日期,得到两个日期之间的年份
public static List<String> getYears(String startTime, String endTime) {
// 返回的日期集合
List<String> days = new ArrayList<>();
DateFormat dateFormat = new SimpleDateFormat("yyyy");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
//tempStart.add(Calendar.MONTH,1);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.YEAR,1);
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
//输入两个日期,得到两个日期之间的月份
public static List<String> getMonths(String startTime, String endTime) {
// 返回的日期集合
List<String> days = new ArrayList<>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
//tempStart.add(Calendar.MONTH,1);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.MONTH,1);
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.MONTH, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
//输入两个日期,得到两个日期之间的天数
public static List<String> getDays(String startTime, String endTime) {
// 返回的日期集合
List<String> days = new ArrayList<>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
/*List lDate = new ArrayList();
lDate.add(start);*/
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, 2);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.DATE, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
//输入一个年份,查询这个年份第一个月到最后一个月的每一个月
public static List<String> findMonths(int year) {
List<String> timelist = new ArrayList<>();
for (int i = 1; i <10 ; i++) {
String str = year+"-"+"0"+i;
timelist.add(str);
}
timelist.add(year+"-"+10);
timelist.add(year+"-"+11);
timelist.add(year+"-"+12);
return timelist;
}
//输入两个日期,查询两个日期之间的天数
public static List<String> findDates(Long startTime, Long endTime) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
List<String> timelist = new ArrayList<>();
Date dBegin = new Date(startTime);
Date dEnd = new Date(endTime);
List lDate = new ArrayList();
lDate.add(dBegin);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
lDate.add(calBegin.getTime());
}
for (int i = 0; i <lDate.size() ; i++) {
String dateString = formatter.format(lDate.get(i));
timelist.add(dateString);
}
return timelist;
}
}