1. 方法1: 如果是 java8, 可以使用 LocalDate 获取
// 获取当前日期前一天的日期
LocalDate localDate = ().minusDays(1);//1代表提前多少天
// 获取时间字符串如: 2023-03-01
(());
// 获取当前年如 2023
(());
// 获取当前月如 3
(());
// 获取当前天如 1
(());
//获取前一天的日期
String date = (("yyyy-MM-dd"));//转为String类型
LocalDate转化为指定格式的字符串
方法1
LocalDate localDate = ("2022-02-02");
String date = (("yyyy-MM-dd"));
(date); //2022-02-02
方法2
LocalDate localDate = ("2022-02-02");
DateTimeFormatter dtf = ("yyyy-MM-dd");
String date = (localDate);
(date); //2022-02-02
2. 方法2: 使用 Calendar 类
//获取当前日期
Date date = new Date();
//将时间格式化成yyyy-MM-dd HH:mm:ss的格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//创建Calendar实例
Calendar cal = ();
//设置当前时间
(date);
//在当前时间基础上减一年
(, -1);
((()));
//在当前时间基础上减一月
(,-1);
((()));
//同理增加一天的方法:
(, 1);
((()));
3. 获取指定时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//定义日期显示格式
String now = (new Date());//当前日期
String nowMonthLateDay = getNextDays(now, "-1");//当前日期的前一天
Calendar datees = ();
String years = (()) + "-11-15";//获取当前年的11-15
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//定义日期显示格式
Calendar c = ();
(, -1);//当前月减去一个月
String before = (());//上个月的当前日期
String now = (new Date());//当前日期
String nowMonthLateDay = getNextDay(now, "-1");//当前日期的前一天
String lateMonthLateDay = getNextDay(before, "-1");//上个月的当前日期的前一天
Calendar cal = ();
// int m=()+1;//获取当前月份
// ("当前月份:"+m);
String nowMonthFirstDay = getFirstDayOfMonth(1);//获取当前月份的第一天
(, -1);
(Calendar.DAY_OF_MONTH, 1); //重写当前日期
String lateMonthFirstDay = (());//获取上个月的第一天
4. 获取当前年度,如果超过某个时间,就减去一年或者加上一年
Date date = new Date();//获取日期
SimpleDateFormat dateFormats= new SimpleDateFormat("yyyy");//日期格式
String year=(date);//获取当前年度
DateTimeFormatter dtf = ("yyyy-MM-dd");
//把String转为LocalDate
LocalDate localTime=(year+"-06-30",dtf);
Integer years=(year);
if (!().isAfter(localTime)){//判断时间是否超过
years=(year)-1;
}
(years);
}
5. 获取时间的工具类
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = (strDate, pos);
return strtodate;
}
/**
* 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
*/
public static String getNextDay(String nowdate, String delay) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String mdate = "";
Date d = strToDate(nowdate);
long myTime = (() / 1000) + (delay) * 24 * 60 * 60;
(myTime * 1000);
mdate = (d);
return mdate;
} catch (Exception e) {
return "";
}
}
/**
* 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
*/
public static String getNextDays(String nowdate, String delay) {
try {
SimpleDateFormat format = new SimpleDateFormat("-MM-dd");
String mdate = "";
Date d = strToDate(nowdate);
long myTime = (() / 1000) + (delay) * 24 * 60 * 60;
(myTime * 1000);
mdate = (d);
return mdate;
} catch (Exception e) {
return "";
}
}
/**
* 获取当前小时
*
* @return
*/
public static String getTimeShort() {
SimpleDateFormat formatter = new SimpleDateFormat(" HH");
Date currentTime = new Date();
String dateString = (currentTime);
return dateString;
}
/**
* 获取当前月第一天
*
* @param month
* @return
*/
public static String getFirstDayOfMonth(int month) {
Calendar calendar = ();
// 设置月份
(, month - 1);
// 获取某月最小天数
int firstDay = (Calendar.DAY_OF_MONTH);
// 设置日历中月份的最小天数
(Calendar.DAY_OF_MONTH, firstDay);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String firstDays = (());
return firstDays;
}
//g过去年度
public static String gerLateYear(Integer num){
SimpleDateFormat format = new SimpleDateFormat("yyyy");
Calendar c = ();
//过去一年
(new Date());
(, num);
Date y = ();
String year = (y);
return year;
}
//判断是否超过指定时间
public static boolean afterDate(String date) {
DateTimeFormatter dtf = ("yyyy-MM-dd");
//把String转为LocalDate
LocalDate localTime = (date, dtf);
//判断当前日期是否大于指定日期
return ().isAfter(localTime);
}