cancel
Showing results for 
Search instead for 
Did you mean: 

calculate age in mapping ?

former_member270261
Participant
0 Kudos

Hello experts,

how to achive the below mapping loggic  for calculating age,please give me any suggestion.

Thanks

Narendra

Accepted Solutions (1)

Accepted Solutions (1)

former_member213558
Active Participant
0 Kudos

try this way.

only thing is you need to find the sub string of the 'Year'.

former_member270261
Participant
0 Kudos

Hi Ramesh,

i have tried the same way it is executing but they are asking me to send the month and time information also.i appreciate if you giving any suggestions

former_member213558
Active Participant
0 Kudos

what is your expected output?

former_member270261
Participant
0 Kudos

Hi Ramesh,

sachin is 42 years old 3months and 20 days and 8hours.like that output expacting.

Thanks

Narendra

former_member213558
Active Participant
0 Kudos

you can try with based on the sub string for both month/date, it can be achieve. else let see any one have UDF for the same.

former_member182412
Active Contributor
0 Kudos

Hi Narendran,

What is your source field value? is it date of birth or what?

Regards,

Praveen.

former_member270261
Participant
0 Kudos

HI Praveen,

1408198616.30 date field is comming like this way.

Thanks

Narendra

former_member182412
Active Contributor
0 Kudos

Hi Narendran,

You can use below UDF, you need to download jodatime library from this url Maven Repository: joda-time » joda-time » 1.6 and import it as imported archive and add it to mapping under Archives Used tab.


public String getAge(String name, String time, Container container) throws StreamTransformationException {

  LocalDate birthdate = new LocalDate((long) Double.parseDouble(time));

  LocalDate now = new LocalDate();

  Period period = new Period(birthdate, now, PeriodType.yearMonthDayTime());

  return name + " is " + period.getYears() + " years old and " + period.getMonths() + " months and " + period.getDays()

  + " days and " + period.getHours() + " hours";

  }

Regards,

Praveen.

former_member270261
Participant
0 Kudos

Hi Praveen,

Thanks for your great support,but still am getting the below error.could you please give any suggestion for below code.

Thanks

Narendra

former_member270261
Participant
0 Kudos

Hi Praveen,

attached jar file and below the screen shots.

former_member182412
Active Contributor
0 Kudos

Hi Narendran,

Remove the blank lines in the imports section.

Regards,

Praveen.

former_member270261
Participant
0 Kudos

Hi Praveen,

removed blank still am getiing below error

former_member182412
Active Contributor
0 Kudos

Hi Narendran,

Did you install the right version of joda time jar into repository?

Regards,

Praveen.

former_member270261
Participant
0 Kudos

HI praveen,

i have installed and tested but receiving same error .please help me any suggestion.

Thanks

Narendra

former_member182412
Active Contributor
0 Kudos

HI Narendra,

You need to import the .jar not .zip, check my screen shot above. You need to unzip first and import the jar file

Regards,

Praveen.

former_member270261
Participant
0 Kudos

Hi Praveen,

finally achived almost but 10 years showing is extra.my age 1986 but 10 years above is showing.

Thanks

Narendra

former_member182412
Active Contributor
0 Kudos

Hi Narendra,

You got the wrong time based on 1986, it should be 504914400000.

Regards,

Praveen.

former_member270261
Participant
0 Kudos

Hi Praveen,

sorry to asking below question.

504914400000 in this one which one is the year and month.

please

Thanks

Narendra

former_member182412
Active Contributor
0 Kudos

Hi Narendra,

If you check my screen shot it is showing 1986-01-01, i just took this example.

Regards,

Praveen.

former_member270261
Participant
0 Kudos

Hi Praveen,

sorry for asking many questions,1986-01-01(504914400000),if want chage the year like 1970-01-01 what should be the giving in the date time.

i have no clue so that am asking many question. 

Thanks

Narendra

former_member182412
Active Contributor
0 Kudos

Hi Narendra,

Check the test program which i shown above. It should be -7200000.

Do close the thread if your query is resolved as per this blog

Regards,

Praveen.

Answers (2)

Answers (2)

iaki_vila
Active Contributor
0 Kudos

Hi Narendran,

You can do it an UDF as well using a pattern as this thread shows http://stackoverflow.com/questions/12636102/how-to-find-string-pattern-in-java

Regards.

vishnu_pallamreddy
Contributor
0 Kudos

Hi Narendran,

Could you please paste your source and target data types?