Java:将字符串日期转换为月份名称年份 (MMM yyyy)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36113530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Convert String Date to Month Name Year (MMM yyyy)
提问by NNR
I am new to Java. I am trying to convert date from string to MMM yyyy format (Mar 2016). I tried this
我是 Java 新手。我正在尝试将日期从字符串转换为 MMM yyyy 格式(2016 年 3 月)。我试过这个
Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMM yyyy");
String month_name = month_date.format(cal.getTime());
System.out.println("Month :: " + month_name); //Mar 2016
It is working fine. But when I use
它工作正常。但是当我使用
String actualDate = "2016-03-20";
It is not working. Help me, how to solve this.
它不工作。帮帮我,如何解决这个问题。
采纳答案by Yassin Hajaj
Your format must match your input
您的格式必须与您的输入相匹配
for 2016-03-20
为了 2016-03-20
the format should be (just use a second SimpleDateFormat
object)
格式应该是(只需使用第二个SimpleDateFormat
对象)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Full answer
完整答案
SimpleDateFormat month_date = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String actualDate = "2016-03-20";
Date date = sdf.parse(actualDate);
String month_name = month_date.format(date);
System.out.println("Month :" + month_name); //Mar 2016
Using java.time
java-8
使用java.time
java-8
String actualDate = "2016-03-20";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MMM yyyy", Locale.ENGLISH);
LocalDate ld = LocalDate.parse(actualDate, dtf);
String month_name = dtf2.format(ld);
System.out.println(month_name); // Mar 2016
回答by Moin
Try this code:
试试这个代码:
DateFormat df = new SimpleDateFormat("dd-MMMM HH:mm a");
Date date = new Date(System.currentTimeMillis());
String infi = df.format(date);
And the output should be:
输出应该是:
11-May 4:47 PM
回答by Basil Bourque
tl;dr
tl;博士
YearMonth.from(
LocalDate.parse( "2016-03-20" )
)
.format(
DateTimeFormatter.ofPattern(
"MMM uuuu" ,
Locale.US
)
)
Mar 2016
2016 年 3 月
java.time
时间
Avoid the troublesome old date-time classes. Entirely supplanted by the java.timeclasses.
避免麻烦的旧日期时间类。完全被java.time类取代。
YearMonth
YearMonth
Java has a class to represent a year and month. Oddly named, YearMonth
.
Java 有一个类来表示年和月。奇怪的名字,YearMonth
。
LocalDate ld = LocalDate.parse( "2016-03-20" ) ;
YearMonth ym = YearMonth.from( ld ) ;
Define a formatting pattern for your desired output.
为所需的输出定义格式模式。
Specify a Locale
. A Locale
determines (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
指定一个Locale
. ALocale
确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号、分隔符等问题的文化规范。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM uuuu" , Locale.US ) ;
String output = ym.format( f ) ;
Mar 2016
2016 年 3 月
You could use that formatter on the LocalDate
. But I assume you may have further need of the year-month as a concept, and so the YearMonth
class may be useful.
您可以在LocalDate
. 但是我假设您可能还需要将年月作为一个概念,因此该YearMonth
课程可能很有用。
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*
classes.
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9、Java SE 10及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。