Java的SimpleDateFormat显示不正确。

huangapple 未分类评论47阅读模式
英文:

Java SimpleDataFormat not displaying correctly

问题

我正在尝试将一个字符串数组转换为日期 ArrayList,格式为 - "dd/MM/yyyy"。我已经在下面包含了我的方法。当我打印 ArrayList 时,日期的格式不正确,显示为:"Thu Mar 05 00:00:00 GMT 2020"。有人能想出这是为什么吗?

private void convertDates()
{
    SimpleDateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy");

    for (String dateString : date) {
        try {
            dateList.add(formatter1.parse(dateString));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    displayDates.setText(Arrays.toString(dateList.toArray()));
}
英文:

I am trying to convert an array of strings to a Date ArrayList with the following format - "dd/MM/yyyy" I have included my method below. When I print the ArrayList the dates are not formatted correctly and are displayed as: "Thu Mar 05 00:00:00 GMT 2020" Can anyone think why this is happening?

private void convertDates()
        {
            SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");
    
            for (String dateString : date) {
                try {
                    dateList.add(formatter1.parse(dateString));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            displayDates.setText(Arrays.toString(dateList.toArray()));
        }

答案1

得分: 0

你有一个Date对象的列表,当你将它们格式化为字符串时,它们将使用它们的默认格式,不管你用什么格式来解析它们。如果你想以那种格式显示它们,你需要明确地这样做。例如:

displayDates.setText(
    dateList.stream().map(formatter1::format).collect(Collectors.joining(","));
英文:

You have a list of Date objects, and when you format them to a string, they'll use their default formatting, regardless of the format you used to parse them. If you want to display them in that format, you'll have to do so explicitly. E.g.:

displayDates.setText(
    dateList.stream().map(formatter1::format).collect(Collectors.joining(","));

huangapple
  • 本文由 发表于 2020年4月10日 21:09:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/61140979.html
匿名

发表评论

匿名网友

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

确定