1. 获取本周的第一天或最后一天
public static String getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst) {
Date time;
Calendar cal = Calendar.getInstance();
if (isFirst) {
cal.add(Calendar.WEEK_OF_MONTH, 0);
cal.set(Calendar.DAY_OF_WEEK, 2);
} else {
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
cal.add(Calendar.DAY_OF_WEEK, 1);
}
time = cal.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
2. 获取上一周的开始时间和结束时间(周五为每周第一天)
public static Map<String, String> getLastWeekStartOrEndDayOfWeek() {
Map<String, String> dayMap = new HashMap<>(16);
String beginTime;
String endTime;
Calendar date = Calendar.getInstance(Locale.CHINA);
date.setFirstDayOfWeek(Calendar.FRIDAY);
date.add(Calendar.WEEK_OF_MONTH, -1);
date.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
beginTime = sdf.format(date.getTime());
date.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
endTime = sdf.format(date.getTime());
dayMap.put("beginTime", beginTime);
dayMap.put("endTime", endTime);
return dayMap;
}
3. 获取本月的第一天或最后一天
public static String getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst) {
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
int length = month.length(today.isLeapYear());
if (isFirst) {
resDate = LocalDate.of(today.getYear(), month, 1);
} else {
resDate = LocalDate.of(today.getYear(), month, length);
}
return resDate.toString();
}
4. 获取上月开始日期和结束日期
public static Map<String, String> getLastStartOrEndDayOfMonth() {
Map<String, String> dayMap = new HashMap<>(16);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 1);
String firstDay = sdf.format(cal.getTime());
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH, 0);
String lastDay = sdf.format(cale.getTime());
dayMap.put("beginTime", firstDay);
dayMap.put("endTime", lastDay);
return dayMap;
}
5. 获取本季度的第一天或最后一天
public static String getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst) {
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
if (isFirst) {
resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
} else {
resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
}
return resDate.toString();
}
6. 获取上个季度开始和结束时间
public static Map<String, String> getLastStartOrEndDayOfQuarter(){
Map<String, String> dayMap = new HashMap<>(16);
String beginTime = "";
String endTime = "";
LocalDate today = LocalDate.now();
Month month = today.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
if(firstMonthOfQuarter.getValue() == 1){
LocalDate lastQuarterToday = LocalDate.of(today.getYear()-1, today.getMonth().getValue()+9, today.getDayOfMonth());
Month lastFirstMonthOfQuarter = lastQuarterToday.getMonth().firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(lastFirstMonthOfQuarter.getValue() + 2);
beginTime = LocalDate.of(today.getYear()-1, lastFirstMonthOfQuarter,1).toString();
endTime = LocalDate.of(today.getYear()-1, endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear())).toString();
}else{
LocalDate lastQuarterToday = LocalDate.of(today.getYear(), today.getMonth().getValue()-3, today.getDayOfMonth());
Month lastFirstMonthOfQuarter = lastQuarterToday.getMonth().firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(lastFirstMonthOfQuarter.getValue() + 2);
beginTime = LocalDate.of(today.getYear(), lastFirstMonthOfQuarter,1).toString();
endTime = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear())).toString();
}
dayMap.put("beginTime", beginTime);
dayMap.put("endTime", endTime);
return dayMap;
}
7. 获取本年的第一天或最后一天
public static String getStartOrEndDayOfYear(LocalDate today, Boolean isFirst) {
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
if (isFirst) {
resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
} else {
resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
}
return resDate.toString();
}
8. 获取去年的第一天或最后一天
public static Map<String, String> getLastStartOrEndDayOfYear() {
Map<String, String> map = new HashMap<>(16);
LocalDate today = LocalDate.now();
LocalDate beginTime = LocalDate.of(today.getYear()-1, Month.JANUARY, 1);
LocalDate endTime = LocalDate.of(today.getYear()-1, Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
map.put("beginTime", beginTime.toString());
map.put("endTime", endTime.toString());
return map;
}
整体代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.*;
public class DateUtils {
public static String getYestoday() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
Date time = cal.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
public static String getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst) {
Date time;
Calendar cal = Calendar.getInstance();
if (isFirst) {
cal.add(Calendar.WEEK_OF_MONTH, 0);
cal.set(Calendar.DAY_OF_WEEK, 2);
} else {
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
cal.add(Calendar.DAY_OF_WEEK, 1);
}
time = cal.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
public static Map<String, String> getLastWeekStartOrEndDayOfWeek() {
Map<String, String> dayMap = new HashMap<>(16);
String beginTime;
String endTime;
Calendar date = Calendar.getInstance(Locale.CHINA);
date.setFirstDayOfWeek(Calendar.FRIDAY);
date.add(Calendar.WEEK_OF_MONTH, -1);
date.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
beginTime = sdf.format(date.getTime());
date.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
endTime = sdf.format(date.getTime());
dayMap.put("beginTime", beginTime);
dayMap.put("endTime", endTime);
return dayMap;
}
public static String getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst) {
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
int length = month.length(today.isLeapYear());
if (isFirst) {
resDate = LocalDate.of(today.getYear(), month, 1);
} else {
resDate = LocalDate.of(today.getYear(), month, length);
}
return resDate.toString();
}
public static Map<String, String> getLastStartOrEndDayOfMonth() {
Map<String, String> dayMap = new HashMap<>(16);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 1);
String firstDay = sdf.format(cal.getTime());
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH, 0);
String lastDay = sdf.format(cale.getTime());
dayMap.put("beginTime", firstDay);
dayMap.put("endTime", lastDay);
return dayMap;
}
public static String getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst) {
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
if (isFirst) {
resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
} else {
resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
}
return resDate.toString();
}
public static Map<String, String> getLastStartOrEndDayOfQuarter(){
Map<String, String> dayMap = new HashMap<>(16);
String beginTime = "";
String endTime = "";
LocalDate today = LocalDate.now();
Month month = today.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
if(firstMonthOfQuarter.getValue() == 1){
LocalDate lastQuarterToday = LocalDate.of(today.getYear()-1, today.getMonth().getValue()+9, today.getDayOfMonth());
Month lastFirstMonthOfQuarter = lastQuarterToday.getMonth().firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(lastFirstMonthOfQuarter.getValue() + 2);
beginTime = LocalDate.of(today.getYear()-1, lastFirstMonthOfQuarter,1).toString();
endTime = LocalDate.of(today.getYear()-1, endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear())).toString();
}else{
LocalDate lastQuarterToday = LocalDate.of(today.getYear(), today.getMonth().getValue()-3, today.getDayOfMonth());
Month lastFirstMonthOfQuarter = lastQuarterToday.getMonth().firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(lastFirstMonthOfQuarter.getValue() + 2);
beginTime = LocalDate.of(today.getYear(), lastFirstMonthOfQuarter,1).toString();
endTime = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear())).toString();
}
dayMap.put("beginTime", beginTime);
dayMap.put("endTime", endTime);
return dayMap;
}
public static String getStartOrEndDayOfYear(LocalDate today, Boolean isFirst) {
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
if (isFirst) {
resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
} else {
resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
}
return resDate.toString();
}
public static Map<String, String> getLastStartOrEndDayOfYear() {
Map<String, String> map = new HashMap<>(16);
LocalDate today = LocalDate.now();
LocalDate beginTime = LocalDate.of(today.getYear()-1, Month.JANUARY, 1);
LocalDate endTime = LocalDate.of(today.getYear()-1, Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
map.put("beginTime", beginTime.toString());
map.put("endTime", endTime.toString());
return map;
}
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static final int DAY = 1;
public static final int WEEK = 2;
public static final int MONTH = 3;
public static final int QUARTER = 4;
public static String getWeekOfYear(String dateStr, int mode){
Calendar calendar = Calendar.getInstance();
if (calendar != null ) {
try {
Date date = sdf.parse(dateStr);
calendar.setTime(date);
calendar.setFirstDayOfWeek(mode);
if (calendar.get(Calendar.WEEK_OF_YEAR) > 1)
calendar.setMinimalDaysInFirstWeek(1);
return calendar.get(Calendar.WEEK_OF_YEAR)+"";
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
public static String[] dateForEach(String startDateStr, String endDateStr, Integer Flag) {
List<String> result = new ArrayList<>(12);
Date startDate = null;
try {
System.out.println("startDateStr==>" + startDateStr);
startDate = sdf.parse(startDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Date endDate = null;
try {
System.out.println("endDateStr==>" + endDateStr);
endDate = sdf.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
if (startDate != null && endDate != null) {
startCalendar.setTime(startDate);
endCalendar.setTime(endDate);
}
switch (Flag) {
case WEEK:
do {
System.out.println(startCalendar.get(Calendar.WEEK_OF_YEAR) + "周");
System.out.println(startCalendar.getTime());
startCalendar.setFirstDayOfWeek(Calendar.FRIDAY);
if (startCalendar.get(Calendar.WEEK_OF_YEAR) > 1)
startCalendar.setMinimalDaysInFirstWeek(1);
result.add(startCalendar.get(Calendar.WEEK_OF_YEAR) + "周");
startCalendar.add(Calendar.WEEK_OF_YEAR, 1);
} while (!startCalendar.equals(endCalendar) && !startCalendar.after(endCalendar));
break;
case MONTH:
do {
System.out.println(startCalendar.get(Calendar.MONTH) + 1 + "月");
result.add(startCalendar.get(Calendar.MONTH) + 1 + "月");
startCalendar.add(Calendar.MONTH, 1);
} while (!startCalendar.after(endCalendar));
break;
case QUARTER:
do {
int q = 0;
if (startCalendar.get(Calendar.MONTH) <= 2) {
q = 1;
} else if (startCalendar.get(Calendar.MONTH) >= 3 && startCalendar.get(Calendar.MONTH) <= 5) {
q = 2;
} else if (startCalendar.get(Calendar.MONTH) >= 6 && startCalendar.get(Calendar.MONTH) <= 8) {
q = 3;
} else if (startCalendar.get(Calendar.MONTH) >= 9 && startCalendar.get(Calendar.MONTH) <= 11) {
q = 4;
}
System.out.println(q);
result.add(q + "季度");
startCalendar.add(Calendar.MONTH, 3);
} while (!startCalendar.after(endCalendar));
}
return result.toArray(new String[0]);
}
public static void main(String[] args) {
System.out.println(getLastStartOrEndDayOfQuarter());
}
}