java - SimpleDateFormat 无法解析的日期 : "9-Jul" when using parse method

标签 java date datetime apache-poi simpledateformat

我想使用SimpleDateFormat.parse方法来设置日期:

 private static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM");
 ...
  Calendar calendar = Calendar.getInstance();
  int year = calendar.get(Calendar.YEAR);
  calendar.setTime(fmt.parse("9-Jul"));
  calendar.set(Calendar.YEAR, year);

但我得到:

Exception in thread "main" java.text.ParseException: Unparseable date: "9-Jul"
    at java.text.DateFormat.parse(Unknown Source)
    at BusinessPlan.main(BusinessPlan.java:116)

代码来自 Apache 示例站点,发布号为 this bug .

最佳答案

对我来说效果很好

尝试设置区域设置

 SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM", Locale.US);

关于java - SimpleDateFormat 无法解析的日期 : "9-Jul" when using parse method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22266576/

相关文章:

java - Spring LDAP 空指针异常

Java 在使用 getClass().getResource() 加载图像时抛出 NullPointerException

javascript - 在 JavaScript 计数器中使用主机日期

PHP如何从mysql转换日期?

mysql - excel vba - 根据包含日期和时间的相邻单元格添加带有日期的表格列

javascript - .isAfter 甚至返回 false

java - 可选,@NotBlank 给出错误,而 @Size 不给出使用 Javax 验证的不可变类

java - 使用 Spring AOP 捕获 Spring 运行时异常

php - 解析 Twitter API created_at

java - 如何将 🎌 Japanese Era Date 字符串值解析为 LocalDate 和 LocalDateTime