月份不是从日期打印的 - Java DateFormat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19036033/
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-12 13:35:02  来源:igfitidea点击:

Month is not printed from a date - Java DateFormat

javadatecalendardate-format

提问by bigData

How to get month from a date in java :

如何从java中的日期获取月份:

        DateFormat inputDF  = new SimpleDateFormat("mm/dd/yy");
        Date date1 = inputDF.parse("9/30/11");

        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);

        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int year = cal.get(Calendar.YEAR);

        System.out.println(month+" - "+day+" - "+year);

This code return day and year but not month.

此代码返回日期和年份,但不返回月份。

output :

输出 :

0 - 30 - 2011

采纳答案by dasblinkenlight

This is because your format is incorrect: you need "MM/dd/yy"for the month, because "mm"is for minutes:

这是因为您的格式不正确:您需要"MM/dd/yy"月份,因为"mm"是分钟:

DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");
Date date1 = inputDF.parse("9/30/11");

Calendar cal = Calendar.getInstance();
cal.setTime(date1);

int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int year = cal.get(Calendar.YEAR);

System.out.println(month+" - "+day+" - "+year);

Prints 8 - 30 - 2011(because months are zero-based; demo)

打印8 - 30 - 2011(因为月份是从零开始的;演示

回答by Sotirios Delimanolis

If you read the SimpleDateFormatjavadoc, you'll notice that mmis for minutes. You need MMfor month.

如果您阅读SimpleDateFormatjavadoc,您会注意到那mm是几分钟。你需要MM一个月。

DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");

Otherwise the format doesn't read a monthfield and assumes a value of 0.

否则格式不会读取month字段并假定值为0.

回答by rgettman

First, you used mmin your date format, which is "minutes" according to the Javadocs. You set the minutes to 9, not the month. It looks like the month defaults to 0 (January).

首先,您使用mm了日期格式,根据 Javadocs 是“分钟”。您将分钟设置为9,而不是月份。看起来月份默认为 0(一月)。

Use MM(capital 'M's) to parse the month. Then, you will see 8, because in Calendarmonths start with 0, not 1. Add 1to get back the desired 9.

使用MM(大写'M's)来解析月份。然后,您将看到8,因为Calendar几个月中以 0 开始,而不是 1。添加1以取回所需的9.

The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0

公历和儒略历中的第一个月是 JANUARY,即 0

// MM is month, mm is minutes
DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");  

and later

然后

int month = cal.get(Calendar.MONTH) + 1; // To shift range from 0-11 to 1-12

回答by Prabhaker A

Month format should be MMinstead of mm

月份格式应MM改为mm

 DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");

回答by Rahul Tripathi

Try like this using MMinstead of mm:-

尝试这样使用MM而不是mm:-

    DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");
    Date date1 = inputDF.parse("9/30/11");

    Calendar cal = Calendar.getInstance();
    cal.setTime(date1);

    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int year = cal.get(Calendar.YEAR);

    System.out.println(month+" - "+day+" - "+year);

The month printed will be 8 as index starts from 0

打印的月份将为 8,因为索引从 0 开始

or try with:-

或尝试:-

int month = cal.get(Calendar.MONTH) + 1;

回答by Dax Joshi

mmis for minutes, use MMwhile specifying format.

mm为分钟,MM在指定格式时使用。

Calendar cal = Calendar.getInstance();
cal.setTime(date1);

int month = cal.get(Calendar.MONTH);// returns month value index starts from 0

回答by Ole V.V.

Time for someone to provide the modern answer. The other answers were good answers when the question was asked in 2013 and are still correct. Today there is no reason why you should struggle with the old, outdated and simultaneously notoriously troublesome SimpleDateFormatclass. java.time, the modern Java date and time API, is so much nicer to work with:

是时候有人提供现代答案了。其他答案在 2013 年提出问题时都是很好的答案,并且仍然是正确的。今天,你没有理由要与旧的、过时的、同时臭名昭著的麻烦SimpleDateFormat阶级斗争。java.time,现代 Java 日期和时间 API,使用起来要好得多:

    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("M/d/yy");
    LocalDate date1 = LocalDate.parse("9/30/11", inputFormatter);
    System.out.println(date1);

This prints

这打印

2011-09-30

The LocalDateclass represents a date without time-of-day, exactly what you need, it matches your requirement much more precisely than the old classes Dateand Calendar.

LocalDate级表示没有时间的日的日期,正是你需要的,它符合你的要求比更精确得多的老班DateCalendar

The format pattern strings used with DateTimeFormatterresemble those from SimpleDateFormat, there are a few differences. You may use uppercase MMto require two-digit month (like 09 for September) or a single Mto allow the month to be written with one or two digits. Similarly ddor dfor day of month. yydenotes two-digit year and is interpreted with base 2000, that is, from 2000 to 2099 inclusive (wouldn't work for my birthday).

使用的格式模式字符串与 中的格式模式字符串DateTimeFormatter类似SimpleDateFormat,但有一些区别。您可以使用大写字母MM来要求两位数的月份(如 09 代表九月),或者使用单个字母来M允许使用一位或两位数来书写月份。类似地ddd对于一个月中的某一天。yy表示两位数的年份,并以 2000 为基数进行解释,即从 2000 年到 2099 年(含)(不适用于我的生日)。

Link: Oracle tutorial Trail: Date Timeexplaining how to use java.time.

链接Oracle 教程路径:解释如何使用java.time.