This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

add 1 year to current date in one statement  RSS feed

 
Gregory Smithson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

this handy post http://www.coderanch.com/t/392347/java/java/add-year-today-date-java shows me how to add 1 year to the current date using a few statements.

Is it possible to do it in one statement though?

For example, I can now set the current date (in ISO format) in an XML tag with this:
<startDt>${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date())}</startDt>

which gave me 2012-12-24.

I want to now set an <endDt> tag, which is to be the start date (set above) plus 1 year.

I've been experimenting with this: add(Calendar.YEAR,1)
but haven't managed to get the syntax correct when I try to add it to the above structure.

For the record I'm actually a COBOL programmer trying to get a bit of Java knowledge, so please go easy on the responses!

many thanks, Greg.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do it using the normal Java API - not sure is Joda Time will allow it.

But it seems to me like you are doing way too much and trying to cram it into some markup. This is poor design - it will make reading/changing the markup and reading/changing the Java code much harder (since they are interleaved). Especially 6 months from now when you come back to the code after not looking at it for a while.

You should create a utility method to do the work for you. One method to get the current date. Another method to format a date the way you want it. And a third date to add a year (or some period of time) to a date. Then you can make the utility methods as simple to call as necessary for your markup.

You won't need to format the endDt like that - it is one line, but forum software needs to break it up.

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:not sure is Joda Time will allow it.

Yes, in Joda you have DateTime objects which have plusXXX methods, so to create a date one year on from now you would do ie

But like Steve has already said you are better off not having a mass of code on one line.
 
Gregory Smithson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve for your reply.
It is good advice to keep things simple - easier to read and understand for the novice and thus easier to debug if things don't work as expected.

I should have mentioned in my initial post that the reason I wanted a single command in the XML tag was that it was part of a SoapUI request message, where I "think" statements have to be single, for want of a better description.
Had I have done so, it would have given a different slant to my question. A lesson for next time...

In any case, thanks to Tony as well for his reply, and for pointing me in the direction of Joda.
I managed to use it and got the following definitions working:

<startDt>${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new org.joda.time.DateTime().toDate())}</startDt>
<endDt>${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new org.joda.time.DateTime().plusYears(1).toDate())}</endDt>

which in my current time zone gives me 2012-12-27 & 2013-12-27 respectively.

Once again, many thanks chaps for taking the time to respond to my question. cheers, Greg.

Many thanks your your advice to my question.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Joda you could also use Joda's formatter ie

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!