【问题标题】:Java: SimpleDateFormat not simplifying to specified pattern? [duplicate]Java:SimpleDateFormat 没有简化为指定的模式? [复制]
【发布时间】:2020-11-16 19:28:14
【问题描述】:

尝试使用 SimpleDateFormat.parse("yyyy-mm-dd") 将 yyyy-mm-dd 字符串(例如 2013-12-30)转换为日期对象。

2013 年 12 月 30 日的预期输出,2013 年 12 月 30 日星期一 00:00:00 EST 对象的接收输出。

试图找出为什么 SimpleDateFormat 返回不同的格式,但在尝试查看 java api 时不知所措。要求澄清正在发生的事情以及更好的方法。

注意:使用 java.utl.Date 卡住了。

    ...
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    dateArray = new Date[rowCount];

    try {
        for(int index = 0; index < rowCount; index++){
            dateArray[index] = simpleDateFormat.parse(fileArray[index][0]);
            System.out.println(dateArray[index].toString());
        }
    } catch(ParseException err){
        System.out.println("ERR: Data parse exception. Format is not correct.");
        err.printStackTrace();
    }

【问题讨论】:

  • 如果我可能会问,你为什么使用旧的 SimpleDateFormatDate 类?
  • 不要直接打印Date,而是使用SimpleDateFormatformat() 方法)获取相同格式的字符串。
  • 格式与Date 对象是分开的。使用simpleDateFormat.format(dateArray[index]);
  • @MCEmperor:参加过时的 Java 大学课程。课程已更新,但作业未更新。只有少数分配有一个有一个或两个使用已弃用包的规范。在这种情况下,它将是 Date 类。
  • @Kayaman:感谢您的明确解释。我对 SimpleDataFormat 的工作原理有了更好的理解,同时也减慢了拼凑我一直在阅读的内容的速度。在这种情况下,我真的应该构建一个打印方法。谢谢。

标签: java date simpledateformat


【解决方案1】:

mm 模式代表 minute,而不是 month月份,您需要使用MM

演示:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) throws ParseException {
        String strDate = "2013-12-30";
        SimpleDateFormat sdfISO8601 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdfISO8601.parse(strDate);
        System.out.println(date);

        String strDateISO8601 = sdfISO8601.format(date);
        System.out.println(strDateISO8601);

        // Some other format
        String strSomeOtherFormat = new SimpleDateFormat("EEEE MMM dd yyyy").format(date);
        System.out.println(strSomeOtherFormat);
    }
}

我也建议你查看Convert UTC String to UTC Date

请注意,java.util 的日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。我建议你应该完全停止使用它们并切换到modern date-time API

使用现代日期时间 API:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String strDate = "2013-12-30";
        LocalDate date = LocalDate.parse(strDate);
        System.out.println(date);

        String strDate8601 = date.toString();
        System.out.println(strDate8601);

        // Custom format
        String customFormat = DateTimeFormatter.ofPattern("EEEE MMM dd uuuu").format(date);
        System.out.println(customFormat);
    }
}

您的日期字符串已经是ISO 8601 格式的日期,因此在您使用现代日期时间 API 时不需要使用任何格式化程序来解析它。

通过 Trail: Date Time 了解有关现代日期时间 API 的更多信息。

如果您正在为一个 Android 项目工作,并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

【讨论】:

  • @OleV.V. - 我希望你一切都好。问题第一句Tried converting string of yyyy-mm-dd, example 2013-12-30, to date object using SimpleDateFormat.parse("yyyy-mm-dd").才是真正的罪魁祸首?。
猜你喜欢
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 2017-09-01
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 2018-11-14
相关资源
最近更新 更多