File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calender class doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calender class doubt " Watch "Calender class doubt " New topic
Author

Calender class doubt

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi,
Can any one explain me following Calender example what
cal.roll(Calendar.DAY_OF_YEAR, true);
will return and whats the use of that.
why second println give IllegalArgumentException.

Thanks in advance


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
Can any one explain me following Calender example what
cal.roll(Calendar.DAY_OF_YEAR, true);
will return and whats the use of that.


this is straight from Java specification about Calendar class::


roll
public abstract void roll(int field,boolean up)::
Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields. For example, to roll the current date up by one day, you can achieve it by calling:

roll(Calendar.DATE, true).

When rolling on the year or Calendar.YEAR field, it will roll the year value in the
range between 1 and the value returned by calling getMaximum(Calendar.YEAR). When rolling on the month or Calendar.MONTH field,
other fields like date might conflict and, need to be changed.
For instance, rolling the month on the date 01/31/96 will result in 02/29/96. When rolling on the hour-in-day or Calendar.HOUR_OF_DAY field, it will roll the hour value in the range between 0 and 23, which is zero-based.




why second println give IllegalArgumentException.
System.out.println(new SimpleDateFormat("DD-MM-YYYY").format(cal.getTime()))


pattern should be dd-mm yy and not dd-mm-yyyy

for more click on this link::

http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html


"History would be kind to me, for I intend to write it."
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
The SimpleDateFormat(String pattern) throws IllegalArgumentException if the given pattern is invalid, In your example
new SimpleDateFormat("DD-MM-YYYY") The pattern is invalid because it has "Y"

Also SimpleDateFormat("Y") will throw IllegalArgumentException .
I am not sure how and where are the valid pattenrs defined?

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String)

Thanks
Deepak


SCJP, SCWCD, SCBCD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Deepak, please check your private messages. You can see them by clicking on "My Profile" at the top right of the page.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calender class doubt
 
Similar Threads
problem in incrementing date
Date; Previous date, Calendar; Gregorian
No exception when parsing date from string
Calendar shows incorrect date
The Date of Easter