I am getting a date in the format January,2007(or February 2006 etc) from a form dynamically. I would like to get the beginning and end dates of the month (and the year) by using DateFormat methods . I tried to write the code like
The code was not working and was throwing exception. Could anybody help me in this regard?
ps I got the result in another way by converting the month and year in to Strings and comparing this with patterns of every month . But I am not hapy with that code)
There's something wrong here. The method format(...) converts a Date object into a String. But you are not passing it a Date object - you are passing it a String "January,2007". You probably got the following error message:
This is also wrong. Now you are creating a new SimpleDateFormat object, but you are passing it the string "January,2007" and not a format pattern, which is what it expects.
4: Calendar cdr = fullMonthFormat.getCalendar();
You are getting the Calendar object associated with the SimpleDateFormat object here. That doesn't have anything to do with the date value you are trying to parse. This will just return a Calendar object set to the current date and time (and not to January, 2007).
There's also a mistake here. MONTH and YEAR in class Calendar are constants to indicate the month and year fields of the Calendar. They are not the month and year to which the Calendar object is set. Running this will always print this, regardless of what the current month and year of the calendar are:
month is=====>2 year is=====>1
Those constants are meant to be passed to the method Calendar.get(...), so these lines should look like this:
System.out.println("month is=====>" + cdr.get(Calendar.MONTH)); System.out.println("year is=====>" + cdr.get(Calendar.YEAR)); [ April 06, 2007: Message edited by: Jesper Young ]
Thanks for the reply. Though I changed the first fel lines as follows, I couldn't complete the program since I couldn't locate a class which accepts the Date object created as an argument. Could you help me in suggesting a class name/method name to complete the program.
I got the Date object here. But which method or class can I use to access year/month.
Months and dates in Java can be quite tricky. My advice is to make sure you have the java docs or source code attached to your ide. Make a habit of looking at the API when deciding what classes and methods to use it is the fastest way of improving your code.. I have updated your example, I hope this helps.
String stringDate = "March,2007";
SimpleDateFormat fullMonthFormat = new SimpleDateFormat("MMMMM,yyyy"); //parse the date to create a date. Look at the api for Date Date date = fullMonthFormat.parse(stringDate);
System.out.println(date.toString()); //you can pass a Locale if needed Calendar cdr = new GregorianCalendar(); cdr.setTime(date); // System.out.println("month is=====>"+cdr.MONTH); //With this line all you are doing is printing the static variable from //Calendar //public final static int MONTH = 2; //It is convention to access static variables as Class.static variable and //not instance.varialbe // Calendar.MONTH = ok cdr.MONTH = wrong.. int month = cdr.get(Calendar.MONTH); System.out.println("month is=====>" + month);
int year = cdr.get(Calendar.YEAR); System.out.println("year is=====>" + year);
Joined: Nov 27, 2006
Thanks for the reply of Mr.Ulf Dittmer and Mr.Shawn Vader . I didn't want to use deprecated methods. That is the reason I didn't use methods in Date class.
The suggestion given by Mr.Shawn Vader is working fine,and I added a one to get the current year.Though I looked in APIs, I couldn't understand it fully.That is why I posted the question.
Thanks once again for the replies and a special thanks to javaranch.
Joined: Mar 22, 2005
I didn't want to use deprecated methods.
Ah, OK. But there's nothing wrong with using those methods. There're not broken (like other deprecated methods), just superseded by the Calendar class which can do a number things that Date can't, like timezones.
Joined: Nov 27, 2006
Dear Mr.Ulf Dittmer ,
Thanks for that information. I was not aware of that. When I looked at API,I saw the methods as deprecated and I felt that these methods would not be supported in future.