Java当前时间的实现
import java.text.SimpleDateFormat;
import java.util.Date;
public class Datetest {
public static void main(String[] args) throws Exception {
Date now =new Date();
System.out.println("JAVA默认时间格式"+now);
//修改时间格式
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String nowTime = sdf.format(now);
System.out.println("当前时间是"+nowTime);
//假设现在有一个日期字符串,应该怎么转Date类型
String time = "2008-08-08 18:32:49 032";
//SimpleDateFormat sdf2 = new SimpleDateFormat("格式要和所给字符串格式一致");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Date date_08 = sdf2.parse(time);
//这里有异常需要在main方法后面加 throws Exception 即可运行
System.out.println(date_08);
}
}
运行结果如下
但是我又发现,如果修改时间格式中,如果不传格式进去
即
SimpleDateFormat sdf =new SimpleDateFormat( );
此时的运行结果就是
发现这里不传格式进去应该是输出电脑自带格式(个人猜想)
但是在sdf2(日期字符串转Date类型)中的格式必须写。