[英]How to change the date format of 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){
}
}
如果您执行类似System.out.println(TimeHistory)
或者仅在调试时注意日期,则将调用java.util.Date
的toString()
方法。 System.out.println(TimeHistory)
调用java.util.AbstractCollection
的toString()
方法,该方法将对每个项目的toString()
方法执行一个调用。
如果要更改此行为,则应扩展java.util.Date
并覆盖toString()
-方法
format.parse( Info.get("TIME") ) // Get a legacy `java.util.Date` object.
.toInstant() // Convert from legacy class to modern class.
.atOffset( // Convert from the basic `Instant` class to the more flexible `OffsetDateTime` class.
ZoneOffset.UTC // Specify the offset-from-UTC in which you want to view the date and time-of-day.
)
.format( // Generate text representing the value of our `OffsetDateTime` object.
DateTimeFormatter.ofPattern( "dd/MM/yyyy HH:mm:ss" ) // Specify your custom formatting pattern. Better to cache this object, in real work.
) // Returns a `String
Date
在几年前被Instant
取代。 Instant::toString
方法使用一种更好的格式,一种现代的标准格式。
Instant.now().toString()
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() ;
幸运的是, Instant
上的toString
方法设计得更好。 它告诉您真相,在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" ) ;
…
if(Info.get("TIME")!=null)
{
try {
Date date = format.parse( Info.get("TIME") ) ;
Instant instant = date.toInstant() ;
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
String output = odt.format( f ) ;
message.TimeHistory.add(date);
}
catch (Exception e){
}
}
最好的方法是使用simpleDateFormat类,在其中使用字符串指定格式: https ://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
但是Date对象很旧,您可以使用Java8日期类作为时间
编辑后:显然现在您想更改ArrayAdapter的行为,因为默认情况下它使用OBJECT.toString()方法显示数据,因此它使用java.util.Date.toString()。 您想要更改此行为,这是您想要的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.