我需要能够使用 Room TypeConverters 在我的 sqlite 数据库上存储和检索 LocalDateTime 属性。
经过一番研究,我实现了以下转换器。但是,此转换器似乎仅存储默认日期时间 (1970-01-01)。所以没有正确转换有人有工作的 LocalDateTime 转换器吗?或者对此有任何改进?
public class LocalDateTimeConverter {
@TypeConverter
public static LocalDateTime toDate(Long timestamp) {
LocalDateTime ldt;
if (timestamp == null){
return null;
}else{
ldt = Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
return ldt;
}
@TypeConverter
public static Long toTimestamp(LocalDateTime date) {
if (date == null){
return null;
}else {
return date.getLong(ChronoField.CLOCK_HOUR_OF_DAY);
}
}}
Run Code Online (Sandbox Code Playgroud) iv一直在建立一个网站,在测试时我发现如果我放了 <em>bob</em>
或者在我的注册/ udate页面上的文本字段中类似的东西,它们在输入'<em>bob</em>'时存储在数据库中, 但当回到网站时它们以斜体显示
有没有办法阻止我的文本输入中的HTML代码?或者从数据库回显到页面上时,它只读作html?大多数人只是想知道这里发生了什么?以斜体显示的名称不是主要问题,但似乎是用户无法控制的东西?
ps我可以提供代码,如果需要,但不认为这对这个问题有多大帮助?