Month is not printed from a date - Java DateFormat
Asked Answered
J

7

10

How to get month from a date in 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
Jeremiad answered 26/9, 2013 at 18:35 Comment(1)
FYI: These troublesome old date-time classes seen in the Question are now legacy, supplanted by the java.time classes built into Java 8 and later. ZonedDateTime.now().getMonthValue()Moise
T
17

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

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)

Tendance answered 26/9, 2013 at 18:37 Comment(0)
D
5

First, you used mm in 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).

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

The first month of the year in the Gregorian and Julian calendars is JANUARY which is 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
Dope answered 26/9, 2013 at 18:37 Comment(0)
A
3

If you read the SimpleDateFormat javadoc, you'll notice that mm is for minutes. You need MM for month.

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

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

Activate answered 26/9, 2013 at 18:37 Comment(0)
M
1

Month format should be MM instead of mm

 DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");
Morbilli answered 26/9, 2013 at 18:37 Comment(0)
B
1

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 SimpleDateFormat class. java.time, the modern Java date and time API, is so much nicer to work with:

    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 LocalDate class represents a date without time-of-day, exactly what you need, it matches your requirement much more precisely than the old classes Date and Calendar.

The format pattern strings used with DateTimeFormatter resemble those from SimpleDateFormat, there are a few differences. You may use uppercase MM to require two-digit month (like 09 for September) or a single M to allow the month to be written with one or two digits. Similarly dd or d for day of month. yy denotes two-digit year and is interpreted with base 2000, that is, from 2000 to 2099 inclusive (wouldn’t work for my birthday).

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

Basir answered 10/1, 2018 at 11:29 Comment(0)
G
0

Try like this using MM instead of 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

or try with:-

int month = cal.get(Calendar.MONTH) + 1;
Gambell answered 26/9, 2013 at 18:37 Comment(2)
The month printed will be 8, because the range for the month is 0-11, not 1-12.Dope
@rgettman:- Added that in my answer. I thought OP must be aware of that!!Gambell
P
0

mmis for minutes, use MM while specifying format.

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

int month = cal.get(Calendar.MONTH);// returns month value index starts from 0
Particia answered 27/9, 2013 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.