0

I have a date string "2012-01-26" which I get from JSON.

I have to now check if the month and year is as current year than display data it in ListView.

SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-DD");
  try {
      Calendar c = Calendar.getInstance(); 
      c.setTime(dateFormat.parse(event_start_date));
      int month = c.get(Calendar.MONTH);
      System.out.println(c.get(Calendar.YEAR));
      System.out.println("month is"+month);
 }

Year value is always correct, but the Month is always zero.What am I missing guys?

Renjith
  • 5,783
  • 9
  • 31
  • 42
Asmi
  • 301
  • 3
  • 13
  • Note: I don't want the current year or date, but I want the date and year from the String for e.g"2012-12-28". Right now from the above code I get Year=2012; Month=0; Date=not checked yet. – Asmi Jan 08 '13 at 09:39
  • 1
    Update: The terrible date-time classes you are using have been supplanted by the *java.time* classes defined in JSR 310. – Basil Bourque Jun 21 '21 at 08:30

6 Answers6

2

You are fetching month using Calendar's instance.

int month = c.get(Calendar.MONTH);

So you will get current month (i.e. January) And it means 0. So the result is correct.

Check this.

month - The month that was set (0-11) for compatibility with Calendar.

If you want the month of event_start_date, then you won't have to use calendar's instance at all.

You can fetch it using

int month = event_start_date.getMonth();
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Yes, I think you are right is there any way I can get "2012-01-26" month and year from the this String – Asmi Jan 08 '13 at 09:29
  • @Asmi, why do you parse event_start_date? You can use event_start_date.getMonth(). – MysticMagicϡ Jan 08 '13 at 09:31
  • But the event_start_date is String , how can I get .getMonth() from it? I m getting this String form JSON, not as a Date but as a String – Asmi Jan 08 '13 at 09:32
  • @Asmi, ok. If the event_start_date string is in YYYY-MM-DD format, you can add these lines: String[] event_start_d = event_start_date.split("-"); int month = Integer.parseInt(event_start_d[1]); – MysticMagicϡ Jan 08 '13 at 09:43
  • it works:) Splitting it seems very good option.:) Thanks alot – Asmi Jan 08 '13 at 09:47
  • @Asmi, But I just tried your code. It seems that it will work perfectly if you replace "yyyy-MM-DD" with "yyyy-MM-dd" – MysticMagicϡ Jan 08 '13 at 09:48
2

java.time.YearMonth

The modern solution uses java.time classes defined in JSR 310.

If you are focused on the year and month without the day, use YearMonth class.

LocalDate ld = LocalDate.parse( "2012-01-26" ) ;
YearMonth ym = YearMonth.from( ld ) ;
int y = ym.getYear() ;
Month m = ym.getMonth() ;
String monthName = m.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    Thanks for posting the modern answer. Please also add the links for backporting `java.time` API and also for using `java.time` API in Android via desugaring. – Arvind Kumar Avinash Jun 21 '21 at 09:14
1

monthes start from 0 - which means youre getting JANUARY. from Calendar.java:

public final static int JANUARY = 0;
radai
  • 23,949
  • 10
  • 71
  • 115
1

The month is zero based (January = 0) so you have to add one.

Henry
  • 42,982
  • 7
  • 68
  • 84
1

read the java doc ;)

for MONTH it says:

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year is JANUARY which is 0; the last depends on the number of months in a year.
duffy356
  • 3,678
  • 3
  • 32
  • 47
0

That's because month in Calendar start from 0 (for January) and go on till 11 (for December)

Ranjan
  • 328
  • 3
  • 10