自定义日期格式(date——>String)
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class 自定义日期 { public static void main(String[] args) { //Date中自带格式 Date date = new Date(); System.out.println(date); //格式化日期 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String nowtime = dateFormat.format(date); System.out.println(nowtime); SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); String nowtime1 = dateFormat1.format(date); System.out.println(nowtime1); } }
(String——>Date)
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class 自定义日期 { public static void main(String[] args) throws ParseException { String time = "2022-4-12 17:28"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date DATETIME = dateFormat.parse(time); System.out.println(DATETIME); } }
![]()
这篇博客展示了如何在Java中将Date对象格式化为自定义的字符串表示,以及如何将字符串解析回Date对象。示例代码包括使用SimpleDateFormat进行日期格式化和解析,例如转换为'yyyy-MM-dd'和'yyyy-MM-dd HH:mm:ss SSS'格式。

457

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



