Results 1 to 4 of 4
- 10-25-2011, 06:02 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
Manually setting a date using Calendar and Date
Greetings,
I'm trying to take a Date called endDate, and create a second Date object called startOfEndYear which holds the first date of of endDate's year. So, if endDate was "Oct 21, 2010", then I'd want startOfEndYear to be "Jan 1, 2010". My code for attempting to do so follows:
The explanation of the red underlining says "Unhandled exception type ParseException". How can I correctly code that last line?Java Code://Assume that endDate has already been set to "Oct 21, 2010" cal.setTime(endDate); int endYear = cal.get(cal.YEAR); System.out.println("end year is " + endYear); DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); Date startOfEndYear = df.parse("Jan 1, "+ endYear); //This line gets underlined in red in Eclipse
Thanks,
-ravl13
- 10-25-2011, 09:01 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 16
- Rep Power
- 0
Re: Manually setting a date using Calendar and Date
Maybe you can try something like this:
Java Code:$ Calendar cal2 = (Calendar) cal.clone(); cal2.set(Calendar.MONTH, Calendar.JANUARY); cal2.set(Calendar.DAY_OF_MONTH, 1); Date startOfEndYear = cal2.getTime();
- 10-25-2011, 09:14 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Manually setting a date using Calendar and Date
surround that line with try - catch block
- 10-25-2011, 11:12 PM #4
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
Re: Manually setting a date using Calendar and Date
OK, that basically works, thanks. I used similar code to revalue the original "cal" object; I didn't know how to set calendar values.
(by the way, for anyone just looking, I had already created a Calendar instance called cal in the above code, but I forgot to mention it)
Similar Threads
-
Calendar object? - Getting date from user
By hiei_yasha in forum New To JavaReplies: 9Last Post: 01-23-2011, 03:58 AM -
Calendar date problem
By groobelar in forum New To JavaReplies: 2Last Post: 01-17-2010, 03:47 AM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
java Date and Calendar
By valoyivd in forum New To JavaReplies: 1Last Post: 03-30-2008, 06:49 PM -
Calendar.DATE
By mew in forum New To JavaReplies: 1Last Post: 01-04-2008, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks