定义一个日期工具类(DateUtils),包含两个方法,
(1)把日期转换为指定格式的字符串;
(2)把字符串解析为指定格式的日期;
然后定义一个测试类(DateDemo),测试日期工具类的方法
(1) 定义日期工具类(DateUtils)
(2) 定义一个方法dateToString,用于把日期转换为指定格式的字符串
*返回值类型:String
参数: Date date,String format
(3) 定义一个方法stringToDate,用于字符串解析为指定格式日期
(4) 定义测试类DateDemo,调用日期工具类中的方法
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
private DateUtils() { }
public static String dateToString(Date date,String format){
SimpleDateFormat sdf=new SimpleDateFormat(format);
String resault=sdf.format(date);
return resault;
}
public static Date stringToDate(String date,String format) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat(format);
Date resualt = sdf.parse(date);
return resualt;
}
}
测试类
public class DateDemo {
public static void main(String[] args) throws ParseException {
Date d=new Date();
String s1 =DateUtils.dateToString(d,"yyyy年MM月dd日");
System.out.println(s1);
//时间为 东八区 与格林威治时间不是完全相同
Date d2=DateUtils.stringToDate("2024--09--09","yyyy--MM--dd");
System.out.println(d2);
}
}
推荐阅读:
vue.js:634 [Vue warn]: Property or method "userName" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See:
状态码:415,发送HTTP请求返回415状态码的解决办法
SpringBoot 整合 webapp时 访问404的解决办法
Vue 打开页面时就加载方法,例如查询
SpringCloud怎么调用多个服务的信息
SpringCloud 加入 thymleaf前端页面的方法
Spring整合Mybatis
Controller之间的跳转
Spring AOP的实现原理
This may be the result of an unspecified view, due to default view name generation
行动消除疑虑