Java 实例 - 格式化时间(SimpleDateFormat)
以下实例演示了如何使用 SimpleDateFormat 类的 format(date) 方法来格式化时间
Date 转 String
public class Main{
public static void main(String[] args){
String strDateFormat= "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
String strDate = sdf.format(new Date())
}
}
String 转 Date
public class Main{
public static void main(String[] args){
String strDateFormat= "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
String strDate = "2021-01-01 10:01:01";
Date newDate = sdf.parse(strDate);
}
}
本文介绍了如何使用Java中的SimpleDateFormat类将Date对象转换为指定格式的字符串,以及如何将字符串解析回Date对象。通过两个示例代码,展示了日期格式化的具体实现。
502

被折叠的 条评论
为什么被折叠?



