String转Date,Date转String
源码:
Date currentTime = new Date();
System.out.println("currentTime:"+currentTime);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
System.out.println("dateString:"+dateString);
ParsePosition pos = new ParsePosition(0); // 从第一个字符开始解析
Date currentTime_2 = formatter.parse(dateString, pos);
System.out.println("currentTime_2:"+currentTime_2);
Date time3 = formatter.parse(dateString);
System.out.println("Time3:"+time3);
Date date = new Date();//new日期对象
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化当前时间
String d = dateFormat.format(date);
//获取当前时间
System.out.println("date:"+d);
结果:
currentTime:Thu Nov 23 13:08:38 CST 2023
dateString:2023-11-23 13:08:38
currentTime_2:Thu Nov 23 13:08:38 CST 2023
Time3:Thu Nov 23 13:08:38 CST 2023
date:2023-11-23 13:08:38