gpt4 book ai didi

android - 如何使用房间更改日期格式?

转载 作者:行者123 更新时间:2023-11-30 05:09:26 25 4
gpt4 key购买 nike

我正在使用从 Firebase 实时数据库检索数据的 Room 我有包含日期​​节点的“请求”节点代码工作正常但我想将日期格式更改为“dd-mm-yyyy”我怎样才能做到这一点 ?

public class DateTypeConverter {
@TypeConverter
public static Date toDate(Long value) {
if(null == value){
return null;
}else {
Date date = new Date(value);
return date;
}
}

@TypeConverter
public static Long toLong(Date value) {
return value == null ? null : value.getTime();
}
}

编辑:我找到了解决方案 here

I think that putting the format in resources is best approach

最佳答案

  1. Room 以long 数据类型存储日期,并以Date 数据类型返回。

  2. 现在要显示此日期,您需要将其转换为格式为“dd-mm-yyyy”的 String

您可以使用 Java 中的 SimpleDateFormat 类来做到这一点。

public static String formatDate(Date date) {
SimpleDateFormat dateFormat;
// Apply desired format here
dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

Calendar c = Calendar.getInstance();
dateFormat.setTimeZone(TimeZone.getDefault());
c.setTimeInMillis(date.getTime());
return dateFormat.format(c.getTimeInMillis());
}

现在正如您所评论的那样,您正在使用数据绑定(bind),您可以在为 TextView 设置文本时直接调用此静态函数

希望对你有帮助!!!

关于android - 如何使用房间更改日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53969357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com