将模型中的日期格式从适配器中转换。

huangapple go评论75阅读模式
英文:

transfer the date format from model in adapter

问题

我正在努力处理一个模型,以检索名为“review_time”的字符串,其格式如下:
Sun May 03 21:41:46 PDT 2020

已成功将review_time存储在模型中。我想将字符串设置为“May 03, 2020”。

以下是我在适配器的onBindViewHolder中的代码:

//递归所有字符串,例如 Sun May 03 21:41:46 PDT 2020
String oldstring = model.getReview_time(); 
DateTimeFormatterBuilder  df = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("MMM dd, yyyy"); //May 03, 2020
Log.d("comment_adapter", df.toString());

如何将所有字符串递归到格式("MMM dd, yyyy")。非常感谢。

英文:

I am working on the model to retrieve the string called "review_time" with this format:
Sun May 03 21:41:46 PDT 2020

The review_time has been stored in model successfully. I want to set the string to "May 03 ,2020"

here is what I have in the adapter onBindViewHolder:

//recursive all string, ex. Sun May 03 21:41:46 PDT 2020
String oldstring = model.getReview_time(); 
DateTimeFormatterBuilder  df = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("MMM DD, YYYY"); //May 03, 2020
Log.d("comment_adapter", df.toString());

How can I recursive all string to the format ("MMM DD, YYYY"). Thank you in advance.

答案1

得分: 0

你可以在设置新的日期格式化值时尝试以下类似的方法:

String js = "Sun May 03 21:41:46 PDT 2020"; //model.getReview_time()
Date dt = new Date(js);
LocalDateTime localDateTime = dt.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
obj.setNewDateValue(localDateTime.format(DateTimeFormatter.ofPattern("MMM dd, yyyy")));
英文:

you can try something like below while you set your new date formatted value

String js="Sun May 03 21:41:46 PDT 2020";//model.getReview_time()
Date dt=new Date(js);
LocalDateTime localDateTime=dt.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
obj.setNewDateValue(localDateTime.format(DateTimeFormatter.ofPattern("MMM dd, yyyy")));

huangapple
  • 本文由 发表于 2020年5月4日 13:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61585756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定