Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tommorows date

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I know that this is simple for most of the folks..but i cant get it right...

Well I want to get the next days date (i.e, tommorows date)

And the format i want is "yyyy-MM-dd"

How can i get at the solution..I tried it with Calendar ,gregorian...

but somehow didnt materialise..

Thnks in advance,
A kumar
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Calendar:
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH)+1;
int year= cal.get(Calendar.YEAR);
int date = cal.get(Calendar.DATE)+1;
System.out.println(year+"-"+month+"-"+date);

Hope this helps...
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
month start with 0 in Calendar.So month+1;
to get tomorrows date date+1;
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ....I will try this out.....
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we do this using GregorianCalendar..?
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GregorianCalendar is a concrete subclass of Calendar.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a problem with the code.Suppose my system date is 31 march...

then the output is 2005-03-32 which is wrong i believe...

Thanks..
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by A Kumar:
There is a problem with the code.Suppose my system date is 31 march...

then the output is 2005-03-32 which is wrong i believe...

Thanks..


You can't just add 1 to the value returned as the date. You will get what you described. Try this...

 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for formatting date you can simply use this
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right..you just cannot add 1 to a date..
just slipped over my head...
sorry for that...
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciate all ur help
Thanks vryone....

I had eventually this piece of code..

Calendar now = Calendar.getInstance( );
SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-MM-dd");
System.out.println("It is now " +formatter.format(now.getTime( )));
now.add(Calendar.DATE,1);
System.out.println("Tom is " +formatter.format(now.getTime( )));


Thanx
A Kumar.
 
pradeep jaladi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, + 1);
S.O.P(calendar);
//add one day more
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic