如何更改 List<Date> 的日期格式

我有一个列表,当我打印/显示其内容时,我得到了特定格式的日期。比方说我想以不同的格式获取它们,我如何选择我希望打印列表的格式?


public List<Date> TimeHistory = new ArrayList<Date>();

ArrayAdapter<Date> adapter = new ArrayAdapter<Date>(getActivity(), R.layout.listview_timehistory, R.id.tvTime,TimeHistory);

例如,这段代码按照我的意愿给了我列表,但我想以不同的格式获取它。


顺便说一句,当我将项目添加到列表中时,我使用 simpleDateFormat 和我想要的格式。但是当列表视图中显示项目时,它们似乎不使用这种格式。


if(Info.get("TIME")!=null)

                    {

                        SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

                        try {

                            Date date = format.parse(Info.get("TIME"));

                            message.TimeHistory.add(date);

                        }

                        catch (Exception e){


                        }

                    }


阿波罗的战车
浏览 311回答 3
3回答

温温酱

博士format.parse( Info.get("TIME") )&nbsp; &nbsp; // Get a legacy `java.util.Date` object..toInstant()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Convert from legacy class to modern class..atOffset(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Convert from the basic `Instant` class to the more flexible `OffsetDateTime` class.&nbsp; &nbsp; ZoneOffset.UTC&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Specify the offset-from-UTC in which you want to view the date and time-of-day.&nbsp;)&nbsp;&nbsp;.format(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Generate text representing the value of our `OffsetDateTime` object.&nbsp; &nbsp; DateTimeFormatter.ofPattern( "dd/MM/yyyy HH:mm:ss" )&nbsp; // Specify your custom formatting pattern. Better to cache this object, in real work.&nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Returns a `StringDate几年前被Instant. 该Instant::toString方法使用了一种更好的格式,一种现代标准格式。Instant.now().toString()&nbsp;2019-06-04T20:11:18.607231Z转换您的Date对象,myDate.toInstant().细节该Object::toString方法并不意味着灵活。其目的是在调试或记录时提供对象的简单视图。但是,如您所见,java.util.Date::toString实现很糟糕。首先,将 JVM 当前的默认时区应用于存储在Date对象中的时刻。那一刻实际上是在UTC中。这种误报会造成时区实际上不在对象中的错觉。其次,该Date::toString方法使用了糟糕的格式,只有英文,人类难以阅读,机器也难以解析。这个Date班还有很多其他问题。您根本不应再使用此类。随着 JSR 310 的采用,java.time.Instant类取代了它。您应该尽可能替换Date为Instant。在你不能的地方,转换。调用添加到旧类的新方法。Instant instant = myJavaUtilDate.toInstant() ;幸运的是,上的toString方法Instant设计得更好。它告诉你真相,UTC中的一个时刻。它使用标准的ISO 8601格式。发明该标准的目的是为了以一种既易于机器解析又易于跨文化的人类阅读的方式将日期时间值作为文本进行通信。String output = instant.toString() ;2019-06-04T20:11:18.607231Z因此Instant对象列表将如下所示。Instant now = Instant.now();List < Instant > instants = List.of( now.minus( 1L , ChronoUnit.HOURS ) , now , now.plus( 20L , ChronoUnit.MINUTES ) );String output = instants.toString();[2019-06-04T19:41:51.210465Z, 2019-06-04T20:41:51.210465Z, 2019-06-04T21:01:51.210465Z]你的代码片段至于您的代码片段,请转换为一个java.time.OffsetDateTime对象,并使用自定义格式模式生成文本。DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/yyyy HH:mm:ss" ) ;&nbsp;…&nbsp; &nbsp;if(Info.get("TIME")!=null){&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Date date = format.parse( Info.get("TIME") ) ;&nbsp; &nbsp; &nbsp; &nbsp; Instant instant = date.toInstant() ;&nbsp; &nbsp; &nbsp; &nbsp; OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;&nbsp; &nbsp; &nbsp; &nbsp; String output = odt.format( f ) ;&nbsp; &nbsp; &nbsp; &nbsp; message.TimeHistory.add(date);&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception e){&nbsp; &nbsp; }}

守候你守候我

如果你做了类似的事情,System.out.println(TimeHistory)或者如果你在调试时“只”看你的日期,就会调用java.util.DatestoString()方法。System.out.println(TimeHistory)调用java.util.AbstractCollectionstoString()方法,它执行对每个项目toString()方法的调用。如果你想改变这种行为,你应该扩展java.util.Date并覆盖toString()-method

侃侃无极

最好的方法是使用 simpleDateFormat 类,您可以在其中使用字符串指定格式:&nbsp;https ://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html但是 Date 对象是旧的,你可以使用 Java8 日期类来获取时间编辑后:显然,现在很明显您想更改 ArrayAdapter 的行为,因为默认情况下它使用 OBJECT.toString() 方法来显示数据,因此它使用 java.util.Date.toString()。你想改变这种行为,这就是你想要的:在 ArrayAdapter 中显示自定义对象 - 简单的方法?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java