1、每月1号取前一个月所在月份
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
return LocalDate.now().getDayOfMonth() == 1 ?
//获取当前月份-1
formatter.format(LocalDate.now().minusMonths(1)) :
formatter.format(LocalDate.now());
2、获取当前日期所在月
Calendar now = Calendar.getInstance();
String year = now.get(Calendar.YEAR) + "";
int mon = (now.get(Calendar.MONTH) + 1);
String month = mon + "";
if (mon < 10) {
month = "0" + mon;
}
return year + month;
3、获取指定日之前所在日(yyyy-MM-dd)
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, 0);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String mDateTime = formatter.format(c.getTime());
return mDateTime;
4、查询T-1日是周几
public Map<String, String> getTimeByLatitude() {
Date date = new Date();
SimpleDateFormat simdf = new SimpleDateFormat("YYYY-MM-dd");
Map<String, String> map = new HashMap<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
//获取要查询的T-1日
map.put("lastDate", simdf.format(calendar.getTime()));
//获取要查询的T-1日对应的月份
map.put("month", simdf.format(calendar.getTime()).substring(0, 7));
int day = calendar.get(Calendar.DATE);
int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (day_of_week == 0) {
day_of_week = 7;
}
calendar.add(Calendar.DATE, -day_of_week + 1);
//获取要查询的T-1日对应的周一
map.put("weekHand", simdf.format(calendar.getTime()));
return map;
}
5、获取指定月份以及格式
private String getLastMonth(int i, int style) {
SimpleDateFormat format;
if (style == 1) {
format = new SimpleDateFormat("yyyyM");
} else {
format = new SimpleDateFormat("yyyyMM");
}
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - i);
date = calendar.getTime();
String accDate = format.format(date);
return accDate;
}