DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format = dateTimeFormatter.format(LocalDateTime.now());
System.out.println(format);
TemporalAccessor parse = dateTimeFormatter.parse("2021-07-20 08:56:55");
System.out.println(parse);
long l = System.currentTimeMillis();
Date date = new Date();
System.out.println(date);
System.out.println(date.getTime());
Date date1 = new Date(1626323787777L);
System.out.println(date1);
java.sql.Date date2 = new java.sql.Date(1626323787777L);
System.out.println(date2);
SimpleDateFormat 使用
SimpleDateFormat sdf = new SimpleDateFormat();
String format = sdf.format(new Date());
System.out.println(format);
Date parse = sdf.parse("21-7-15 下午12:45");
System.out.println(parse);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf1.format(new Date()));
Date date2 = sdf1.parse("2021-07-15 03:48:00");
long time = date2.getTime();
System.out.println(time);
练习题
- 字符串"2021-01-02"转换为java.sql.Date
String str = "2021-01-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date parse = sdf.parse(str);
java.sql.Date date = new java.sql.Date(parse.getTime());
System.out.println(date);