Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
problem in incrementing date
harsimran kochar
Greenhorn
Posts: 14
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
here is the code
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; public class PracDate { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy"); GregorianCalendar gc = new GregorianCalendar(); java.util.Date d = sdf.parse("12-11-2003"); gc.setTime(d); System.out.println("Issue Date = " + sdf.format(d)); int dayBefore = gc.get(Calendar.DAY_OF_YEAR); gc.roll(Calendar.DAY_OF_YEAR, +42); int dayAfter = gc.get(Calendar.DAY_OF_YEAR); System.out.println("No Of Days =" +dayAfter); //if (dayAfter>30){ gc.roll(Calendar.MONTH, +1); //} int n= gc.getActualMaximum(Calendar.DAY_OF_MONTH); //if(gc.MONTH==12) gc.add(Calendar.YEAR,+1); // gc.get(Calendar.DATE); java.util.Date nextDate = gc.getTime(); System.out.println("maturity Date = " + sdf.format(nextDate)); System.out.println(n); } } and here is the output it is giving Issue Date = 12-11-2003 No Of Days =54 maturity Date = 23-11-2004 31
the problem is that it is not incrementing month
Jesper de Jong
Java Cowboy
Posts: 16084
88
I like...
posted 12 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
The format
string
in line 9 that you are using to create a SimpleDateFormat is wrong. It should be "dd-MM-yyyy". Note that "mm" means minutes; "MM" means months.
Jesper's Blog
-
Pluralsight Author Page
Martin Vashko
Sheriff
Posts: 3837
66
I like...
posted 12 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
The other issue is that you probably want to use
add
instead of
roll
. See javadoc for the difference between the two.
harsimran kochar
Greenhorn
Posts: 14
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
setting calendar to an arbitrary date
weird java dates!!!!
Date; Previous date, Calendar; Gregorian
Date difference
date arithmetic
More...