格式日期从“MMM dd,yyyy HH:mm:ss a”到“MM.dd” [英] format date from "MMM dd, yyyy HH:mm:ss a" to "MM.dd
本文介绍了格式日期从“MMM dd,yyyy HH:mm:ss a”到“MM.dd”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想格式化日期从MMM dd,yyyy HH:mm:ss a到MM.dd。
我有以下代码
I want to format date from "MMM dd, yyyy HH:mm:ss a" to "MM.dd". I have following code
SimpleDateFormat ft = new SimpleDateFormat ("MMM dd, yyyy hh:mm:ss a");
t = ft.parse(date); //Date is Sep 16, 2015 10:34:23 AM and of type string.
ft.applyPattern("MM.dd");
但是我在 t = ft.parse(date)
请帮助
推荐答案
可能的解释:
- 您的默认语言环境与输入日期不兼容 - 例如它不能理解
Sep
作为月份名称 - 输入字符串有问题,或
-
t
是错误的类型(例如java.sql.Date
而不是java.util.Date
或其他一些类型)或未声明。
- your default locale is incompatible with the input date - e.g. it can't understand
Sep
as a month name - there's something wrong with the input string, or
t
is the wrong type (e.g.java.sql.Date
instead ofjava.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
这篇关于格式日期从“MMM dd,yyyy HH:mm:ss a”到“MM.dd”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文