问题预览
- 1)java如何获取当前日期和时间
- 2)DefaultSerializer requires a Serializable payload but received an object of type [XX]
- 3)Table ‘XXX.hibernate_sequence’ doesn’t exist
- 4)使用idea在DEBUG的时候出现Method breakpoints may dramatically slow down debugging
- 5)Field ‘Id’ doesn’t have a default value解决方法
1.java如何获取当前日期和时间
-
获取标准时间可以通过
System.currentTimeMillis()
方法获取,此方法不受时区影响,得到的结果是时间戳格式的,然后我们将其转化成我们易于理解的格式SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); Date date = new Date(System.currentTimeMillis()); System.out.println(formatter.format(date));
在Java中,获取当前日期最简单的方法之一就是直接实例化位于Java包java.util的Date类。
java.util.Date Date date = new Date();
上面获取到的日期也可以被format成我们需要的格式,例如:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); System.out.println(formatter.format(date));
Java 8提供了一个全新的API,包括:
LocalDate LocalDate只是一个日期,没有时间。 这意味着我们只能获得当前日期,但没有一天的具体时间。
LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); System.out.println(date.format(formatter));
得到的结果只有年月日
LocalTime LocalTime与LocalDate相反,它只代表一个时间,没有日期。
这意味着我们只能获得当天的当前时间,而不是实际日期:LocalTime time = LocalTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); System.out.println(time.format(formatter));
LocalDateTime
最后一个是LocalDateTime,也是Java中最常用的Date / Time类,代表前两个类的组合 - 即日期和时间的值:LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); System.out.println(dateTime.format(formatter));
2.DefaultSerializer requires a Serializable payload but received an object of type [XX]
- 要缓存的 Java 对象必须实现 Serializable 接口,因为 Spring 会将对象先序列化再存入 Redis ,将缓存实体类继承 Serializable
public class XX implements Serializable
3.Table ‘XXX.hibernate_sequence’ doesn’t exist
- 将ID生成略组改成
@GeneratedValue(strategy = GenerationType.IDENTITY)
.
4.使用idea在DEBUG的时候出现Method breakpoints may dramatically slow down debugging
-
根据语义可能是断点打在方法上面了,导致在某个断点卡住了。
打开Breakpoints面板看看,(快捷键:Ctrl - Shift -F8 ) 将前面选择框的"勾勾"去掉点击Done即可。
5.Field ‘Id’ doesn’t have a default value解决方法
- 检查数据表看看Mysql中有没有将主键设置为自增,所以在使用Mybatis时获取生成主键时出现异常。
可以在数据库中设置主键自增