0

My aim to extract date and month from a date object. I cannot use getDate() and getMonth() , as they are deprecated.

I used the Calendar API, however I am not able to get the desired result. Would you please suggest...

My aim is to extract the month and date as integers.

 Scanner input = new Scanner(System.in);
          String a;
          SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
          System.out.println("Please type in your name...");
          a = input.nextLine();
          System.out.println("You entered the name " + a);

          //To print the current date

          Date DOB;
          Date presentdate = new Date();           
          System.out.println("Today is " + presentdate);

          //To get a birthdate

          System.out.println("Enter yout DOB in dd/mm/yyyy");
          String b1 = input.nextLine();
          DOB = formatter.parse(b1);

          System.out.println("Your Date of Birth is" + DOB);


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

          System.out.println(cal);
          int month = cal.get(Calendar.MONTH);
          int date1  = cal.get(Calendar.DATE);          


          System.out.println(month);
          System.out.println(date1);
Anwitha
  • 1
  • 3
  • 1
    Please describe what's your expected outcome and what's the current output. – John Sep 02 '14 at 18:23
  • Enter yout DOB in dd/mm/yyyy 4/11/1991 Your Date of Birth isFri Jan 04 00:11:00 IST 1991 java.util.GregorianCalendar[time=662928060000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1991,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=4,DAY_OF_YEAR=4,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=11,SECOND=0,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0] 0 4 – Anwitha Sep 02 '14 at 18:23
  • Also show how you're creating `formatter`. Oh, and tell us which time zone you're interested in. Basically you haven't given nearly enough information at the moment. – Jon Skeet Sep 02 '14 at 18:24
  • 2
    @Anwitha: No, put the information in your *question*, rather than in comments. – Jon Skeet Sep 02 '14 at 18:24
  • Is your formatter using "dd/mm/yyyy"??, month should be MM, otherwise it will be confused with minutes. – Paco Lagunas Sep 02 '14 at 18:26
  • possible duplicate of [Java: Get month Integer from Date](http://stackoverflow.com/questions/7182996/java-get-month-integer-from-date) – Basil Bourque Sep 03 '14 at 07:23

3 Answers3

3

In Java calendar:-

Calendar.JANURAY=0;
Calendar.FEBRUARY=1;
 .. ..
Calendar.DECEMBER=11;

So change your code to get month like this:-

     Calendar cal = Calendar.getInstance(); ;
     cal.setTime(DOB);
     int month = cal.get(Calendar.MONTH)+1;
     int date1  = cal.get(Calendar.DATE); 
Sunil Singh Bora
  • 771
  • 9
  • 24
1

This is fine, but two things to bear in mind

  1. This works if your formatter is using the format ""dd/MM/yyyy" <- not the capitals for month
  2. Months are zero-indexed, so June although expressed as "06" would come out as 5.
tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • Thanks to MrWiggles, Paco and Sunil..The code works fine when I changed the format to ""dd/MM/yyyy" and considered the zero-indexed months. – Anwitha Sep 11 '14 at 04:25
1

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

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

Then extract the information:

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

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

Months will start with 0, that's the reason of the +1.

Paco Lagunas
  • 320
  • 1
  • 13