• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting date having month and year

 
veena sasikumar
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

regards
Veena
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
veena sasikumar
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

Sasi
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ?
 
Shawn Vader
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
veena sasikumar
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

regards
Veena
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

regards
Veena
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic