可能有错,若看到请指正,谢谢 MySQL里的datetime类型默认是精确到秒的 用getTimestamp将结果放入java.sql.Timestamp类中,是会包含微秒的,因为MySQL的datetime精度不够,所以默认为0 e.g. MySQL:datetime start=2023-11-22 10:34:58,则getTimestamp后,调用toString()(Timestamp类重写了toStirng方法的)后打印 System.out.println(this.start.toString()); 2023-11-22 10:34:58.0 Timestamp/java.util.Date类的getTime()得到对应的时间戳,为long类型 Date date =new Date(Timestamp);,其中Timestamp为时间戳,long类型 SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat的format的参数类型为java.util.Date/java.sql.Timestamp -> String 格式化为字符串 SimpleDateFormat的parse的参数类型为String-> java.util.Date Date date = simpleDateFormat.parse("2023-03-15 12:30:00"); Timestamp(long time) 构造Timestamp类的对象 SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd"),得到日期; simpleDateFormat1得到的时间戳,就会使小时分钟秒均零 00:00:00
MySQL的Datetime在java中的对应类
于 2023-11-22 17:12:28 首次发布