wood burning stoves 2.0*
The moose likes Java in General and the fly likes Formatting date having month and year Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Formatting date having month and year" Watch "Formatting date having month and year" New topic

Formatting date having month and year

veena sasikumar

Joined: Nov 27, 2006
Posts: 29
Hi all,

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)

Thanks in advance,

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

Let's analyze what your code is doing.

1: SimpleDateFormat fullMonthFormat = new SimpleDateFormat("MMMM,yyyy");

You are creating a new SimpleDateFormat object with the pattern "MMMM,yyyy". That's OK.

2: String date1 = fullMonthFormat.format("January,2007");

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:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date

To convert a String to a Date object, you must call the method parse(...), not the method format(...).

3: fullMonthFormat = new SimpleDateFormat(date1);

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).

5: System.out.println("month is=====>"+cdr.MONTH);
6: System.out.println("year is=====>"+cdr.YEAR);

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
veena sasikumar

Joined: Nov 27, 2006
Posts: 29
Dear Mr 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.

Thanks in advance,

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41120
Once you have a Date object, you can use its getMonth and getYear methods. But that seems so obvious that I think you may be asking something else ... ?

Ping & DNS - my free Android networking tools app
Shawn Vader

Joined: Mar 31, 2007
Posts: 13
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);

//you can pass a Locale if needed
Calendar cdr = new GregorianCalendar();
// 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);
veena sasikumar

Joined: Nov 27, 2006
Posts: 29
Dear all,

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.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41120
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.
veena sasikumar

Joined: Nov 27, 2006
Posts: 29
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.

I agree. Here's the link: http://aspose.com/file-tools
subject: Formatting date having month and year
Similar Threads
how to convert Julian Date into Calendar Date
How to Compare two Calendar Objects.
JTable dates
Custom Date class and Appointment class