public class CommonUtils {
//获取当前系统时间,格式:yyyy-MM-dd HH:mm:ss
public static String nowTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String nowTime = df.format(new Date());
System.out.println(nowTime);// new Date()为获取当前系统时间
return nowTime;
}
//获取当前系统日期,格式:yyyy-MM-dd
public static String nowDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String nowDate = df.format(new Date());
return nowDate;
}
//将指定时间转换为yyyy-MM-dd HH:mm:ss格式
public static String goDateTime(Date datetime){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String thisDateTime = df.format(datetime);
return thisDateTime;
}
//将指定时间转换为yyyy-MM-dd格式
public static String goDate(Date date){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String thisDate = df.format(date);
return thisDate;
}
}
Java日期时间快速处理(封装)
于 2022-04-21 12:17:22 首次发布
这是一个Java工具类,包含获取当前系统时间与日期的方法,以及将指定日期时间转换为特定格式的功能。方法如nowTime()返回当前时间(yyyy-MM-dd HH:mm:ss),nowDate()返回当前日期(yyyy-MM-dd)。此外,还提供了将日期时间转化为指定格式的goDateTime()和goDate()方法。
摘要由CSDN通过智能技术生成