Avatar of cofactor
cofactor
 asked on

date format in jsp

I wan to display a date string in jsp.....display format is  yyyy-mm-dd

The date  should be  one year ahead of  today's date.

e.g.  if   current date is  say  2011-07-14   ....I wish to display 2012-07-14  in my jsp.

how do I create such a date string ?

Java EEJSPJava

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
for_yan


This is checeked and working even without compiler warning.

You can do the same thing with Calendar but it is more straightforward and understandable with Date:

   java.util.Date dd = new java.util.Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

  String dateString = sdf.format(dd);

  String yearString = dateString.substring(0,4);

  String monthString = dateString.substring(4);

  int year = new Integer(yearString) + 1;

  dateString = year + monthString;

        System.out.println(dateString);

Open in new window




2012-07-14

Open in new window

SOLUTION
rrz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
cofactor

ASKER
>>optimisation

I dont get this part. You used sql date (  is not this should be util date ? )  and  milliseconds .  ..how does this optimize ?
for_yan

java.sql.Date has toString() method which happened to format it just as you need
2012-07-14
If you use java.util.Date() the toString() method will be different and with seconds and so on, that's why CEHJ used java.sql.Date() here

You can use any of those solutions.
Calendar maybe a little bit newer and it is a standard, so
its bugs are not so well known.

The probability that you encounter bugs with leap years is much less than bugs  from hundred other sources,
and the advantage of java.util.Date is that it is much more intuitive, whereas
with Calendar you need to go to API to check how to write it, no matter how many times you use it.
At least for me it is this way.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
CEHJ

>>I dont get this part. You used sql date (  is not this should be util date ? )  and  milliseconds .  ..how does this optimize ?

The optimisation is

a. avoiding the use of DateFormat (known to be slow - so much so that other apis have been written to do it)
b. writing less code ;)

>>You used sql date

Since its toString method gives you the format you need
cofactor

ASKER
I dont have a leap year constraint.  the idea was to find a date  advance than current date. ...it need not have to be exactly 1 year though ( with leap year).

Thanks for the information.
CEHJ

?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CEHJ

Please post the code that you are now going to use, cofactor, for the benefit of future visitors to this question
rrz

In my opinion, cofactor should have accepted http:#36188279   as the solution.  
I guess he was happy with the String manipulation posted by for_yan.  Personally, I would have accepted CEHJ's http:#36189661. But, the customer is always right.
CEHJ

>>cofactor should have accepted http:#36188279   as the solution.  

He should have accepted yours, with my adjustment ;)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy