0

I have a Java application that allows me to customize reports. The report uses tags for things like dates and names etc.

I have this tag: <[ECHO text="dateFormat.format(currentDate)"/]>

which puts out the current date like this:

May 23, 2014

but I just want the year like this:

2014

How do I do that?

EDIT:

I tried: <[ECHO text="dateFormat.format('yyyy', currentDate)"/]>

But I get this error:

com.agilemind.commons.modules.dynatags.TagValueEvaluationException: Error happened during evaluating 'dateFormat.format('yyyy', currentDate) ' of 'text' of 'ECHO' tag
    at com.agilemind.commons.modules.dynatags.advanced.b.evaluate(b.java:12)
    at com.agilemind.commons.modules.dynatags.advanced.EchoTag.transform(EchoTag.java:6)
    at com.agilemind.commons.modules.dynatags.TagBodyElement.transform(TagBodyElement.java:9)
    at com.agilemind.commons.modules.dynatags.Body.transform(Body.java:4)
    at com.agilemind.commons.modules.dynatags.BodyTag.transform(BodyTag.java:55)
    at com.agilemind.commons.modules.dynatags.TagBodyElement.transform(TagBodyElement.java:9)
    at com.agilemind.commons.modules.dynatags.Body.transform(Body.java:4)
    at com.agilemind.commons.modules.dynatags.BodyTag.transform(BodyTag.java:55)
    at com.agilemind.commons.modules.dynatags.RootTag.transform(RootTag.java:18)
    ... 23 more

Thanks C

Cybercampbell
  • 2,486
  • 11
  • 48
  • 75

2 Answers2

4

Use SimpleDateFormat.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");

now your code:

<[ECHO text="dateFormat.format(currentDate)"/]>

should give you the correct output.

Joe Bauer
  • 572
  • 1
  • 9
  • 22
  • Thanks.. I defined an new variable in the head: <[DEFINE name="dateFormat" value="reportData.createDateFormat('MMM d, yyyy', 'en')"/]> <[DEFINE name="yearFormat" value="reportData.createDateFormat('yyyy', 'en')"/]> – Cybercampbell May 23 '14 at 20:30
0

java.time

The modern approach uses the java.time classes.

LocalDate ld = LocalDate.parse( "2018-01-23" ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu" ) ; // Use just the year of the date-time.
String output = ld.format( f ) ;

2018

Joda-Time

Using the Joda-Time library, you could use a formatter to generate a string similar to the other answer.

Another solution is to extract the year number.

int year = DateTime.now().getYear();

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154