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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:28:09  来源:igfitidea点击:

Java: Convert String Date to Month Name Year (MMM yyyy)

javadate

提问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 SimpleDateFormatobject)

格式应该是(只需使用第二个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.timejava-8

使用java.timejava-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 Localedetermines (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 YearMonthclass 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 类?

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 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多