且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

格式日期从“MMM dd,yyyy HH:mm:ss a”到“MM.dd”

更新时间:2023-02-26 20:07:56

可能的解释:


  1. 您的默认语言环境与输入日期不兼容 - 例如它不能理解 Sep 作为月份名称

  2. 输入字符串有问题,或

  3. t 是错误的类型(例如 java.sql.Date 而不是 java.util.Date 或其他一些类型)或未声明。

  1. your default locale is incompatible with the input date - e.g. it can't understand Sep as a month name
  2. there's something wrong with the input string, or
  3. t is the wrong type (e.g. java.sql.Date instead of java.util.Date, or some other type altogether), or is not declared.

您应该包括你的问题的异常的细节,以弄清楚它是什么,但这里是一个工作示例,使用基本上你自己的代码,添加了一个特定的 Locale

You should include details of the exception in your question to figure out which it is, but here's a working example using basically your own code, with the addition of a specific Locale.

SimpleDateFormat ft = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
java.util.Date t=ft.parse("Sep 16, 2015 10:34:23 AM");
ft.applyPattern("MM.dd");
System.out.println(ft.format(t));

输出:

09.16