获取今天、昨天、本月、上月、近一周等的工具类实现

public static String getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
// 设置年份
cal.set(Calendar.YEAR, year);
// 设置月份
cal.set(Calendar.MONTH, month - 1);
// 获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
// 设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());

return lastDayOfMonth;
}

// 获取今天年月日
public static String getDateToday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}

// 获取昨天年月日
public static String getYesterdayDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();

c.setTime(new Date());
c.add(Calendar.DATE, -1);
Date date = c.getTime();
return format.format(date);
}

// 获取明天年月日
public static String getTomorrowDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();

c.setTime(new Date());
c.add(Calendar.DATE, 1);
Date date = c.getTime();
return format.format(date);
}

// 获取当前年月
public static String getDateYM() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
return sdf.format(new Date());
}

// 获取上月年月
public static String getProMonthYM() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();

c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date date = c.getTime();
return format.format(date);
}

// 通过年月获取当前月的最开始时间
public static String getDateMin(String str) {
return str.trim() + "-01 00:00:00";
}

// 通过年月获取当下一月的最开始时间
public static String getDateMax(String str) {
String s[] = str.split("-");
int y = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
if (m < 12) {
m = m + 1;
} else {
y = y + 1;
m = 1;
}
return y + "-" + getCoverageNum(m + "") + "-01 00:00:00";
}

public static String getCoverageNum(String alarmType) {
if (Integer.parseInt(alarmType) < 10) {
return "0" + alarmType;
}
return alarmType;
}

// 获取当前日期前一个星期的日期
public static String getProWeekDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();

c.setTime(new Date());
c.add(Calendar.DATE, -7);
Date date = c.getTime();
return format.format(date);
}

// 获取当前包括当前日期前6天的日期
public static String getProWeekDateNew() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();

c.setTime(new Date());
c.add(Calendar.DATE, -6);
Date date = c.getTime();
return format.format(date);
}

// 获取当前日期前一个月的日期
public static String getProMonthDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();

c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date date = c.getTime();
return format.format(date);
}

posted on   黑子菜园  阅读(180)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示