-3

this is what i do to get date in java :

Date date = (new GregorianCalendar(year,month - 1, i)).getTime(); // year,month,day
            SimpleDateFormat f = new SimpleDateFormat("EEEE");
            nameofday = f.format(date);

when i print the date Object it gives me the answer like follows :

Sun Apr 01 00:00:00 IST 2012
Mon Apr 02 00:00:00 IST 2012
Tue Apr 03 00:00:00 IST 2012
Wed Apr 04 00:00:00 IST 2012
Thu Apr 05 00:00:00 IST 2012
Fri Apr 06 00:00:00 IST 2012
Sat Apr 07 00:00:00 IST 2012

from this i want to get only the day ex: 01,02,03,04,05,etc.

How to do this in java?

Regards Tony

Joe
  • 4,460
  • 19
  • 60
  • 106
  • Do you mean format the output (as a string), or into a new object? –  May 30 '12 at 07:21
  • Please, have a look at other questions before asking, like this one: http://stackoverflow.com/questions/2619691/extract-day-from-date – Carlo May 30 '12 at 07:24

2 Answers2

5

If you want the day as a number, use:

int dayOfMonth = gregorianCalendarInstance.get(Calendar.DATE);

If you want a string like "05", change your date format to dd, that is:

SimpleDateFormat f = new SimpleDateFormat("dd");
Joni
  • 108,737
  • 14
  • 143
  • 193
3

Your output is not the nameofday. If you printed nameofday, it would print "saturday" or "friday". If you want the day in the month on two characters, as indicated in the javadoc of SimpleDateFormat, you must use "dd" for the pattern:

Date date = ...
DateFormat df = new SimpleDateFormat("dd");
System.out.println(df.format(date));

You should really learn to read documentation.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255