在java中从MM/DD/YYYY到DD-MMM-YYYY
Java 中是否有一种方法可以将 MM/DD/YYYY
转换为 DD-MMM-YYYY
?
例如:05/01/1999
到 01-MAY-99
Is there a method in Java that I can use to convert MM/DD/YYYY
to DD-MMM-YYYY
?
For example: 05/01/1999
to 01-MAY-99
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 SimpleDateFormat 解析日期,然后使用具有所需格式的 SimpleDateFormat 打印出来。
这是一些代码:
输出:
Use a SimpleDateFormat to parse the date and then print it out with a SimpleDateFormat withe the desired format.
Here's some code:
Output:
java.time
您应该在 Java 8 及更高版本中使用 java.time 类。要使用java.time,请添加:
下面是一个如何格式化日期的示例。
java.time
You should use java.time classes with Java 8 and later. To use java.time, add:
Below is an example, how you can format date.
试试这个,
Try this,
Java 8 本地日期
Java 8 LocalDate
试试这个
Try this
java.time
java.util
Date-Time API 及其格式化 APISimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*。日期时间解析/格式化类型是
区域设置
敏感的日期时间解析/格式化类型(例如
DateTimeFormatter
)是区域设置
敏感的,即相同的字母将在不同的Locale
中生成文本。例如MMM
用于月份名称的三个字母缩写,并且在不同的中可以是不同的单词区域设置。 如果没有
Locale
参数,它将使用JVM的Locale
。因此,永远不要忘记使用不带Locale
参数的日期时间解析/格式化类型。通过切勿在没有区域设置的情况下使用 SimpleDateFormat 或 DateTimeFormatter 了解更多信息。您需要
DateTimeFormatter
的两个实例 - 一个用于解析输入字符串,另一个用于根据所需模式格式化输出字符串。演示:
输出:
在线演示
其他一些重要说明:
Y
(week-based-year),您需要使用y
(year-of-era)而不是D
(年份),您需要使用d
(月份)。查看文档了解更多信息关于它。y
而不是u
,但我更喜欢u
到y
。了解有关现代日期时间 API* 来自 跟踪:日期时间< /a>。
* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API 和 如何在Android项目中使用ThreeTenABP。
java.time
The
java.util
Date-Time API and their formatting API,SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.A Date-Time parsing/formatting type is
Locale
-sensitiveA Date-Time parsing/formatting type (e.g.
DateTimeFormatter
) isLocale
-sensitive i.e. the same letters will produce the text in differentLocale
s .e.g.MMM
is used for the three-letter abbreviation of month name and it can be different words in differentLocale
s. In the absence of theLocale
parameter, it will use the JVM'sLocale
. Therefore, never forget to use a Date-Time parsing/formatting type without theLocale
parameter. Learn more about it from Never use SimpleDateFormat or DateTimeFormatter without a Locale.You need two instances of
DateTimeFormatter
- one to parse the input string and another to format the output string, as per required patterns.Demo:
Output:
ONLINE DEMO
Some other important notes:
Y
(week-based-year), you need to usey
(year-of-era) and instead ofD
(day-of-year), you need to used
(day-of-month). Check the documentation to learn more about it.y
instead ofu
but I preferu
toy
.Learn more about the modern Date-Time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
下面应该可以工作。
Below should work.