英文:
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")));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论