D3monTw
D3monTw

Reputation: 11

Alphanumeric Counter in talend to generate id

I am trying to figure out a way in talend to generate an alphanumeric counter that creates numbers in the following way: YYXXXXXXXX

Where

Every new year, 8 character ID should be reset and start again with 00000001

Sequence should appear like

00000001
00000002
00000003
...
99999999

A0000001
A0000002
A0000003
...
A9999999

B0000001
B0000002
B0000003
...
B9999999

Z0000001
Z0000002
Z0000003
...
Z9999999
...

ZA000001
ZA000002
ZA000003
...
ZA999999

......
ZZZZZZZZ

The last number should be ZZZZZZZZ. So it would 1-9 first, then A-Z after that.

And the year the last id that we can accommodate in a year will be 20ZZZZZZZZ

How to do this?

Upvotes: 1

Views: 409

Answers (1)

Basically it converts a number(long) to a radix with base 36, pads it with preceding zeros to match the format, concats the last two digits of current year and returns the result as a String.

import java.time.LocalDateTime; //for getting the current year from system clock
public static String codegen(long input) {
    String lastTwoDigitsOfYear = Integer.toString(LocalDateTime.now().getYear()).substring(2); // the "YY"
    String radixOf36Base = Long.toString(input, 36).toUpperCase();
    String radixOf36BasePadded = String.format("%8s", radixOf36Base).replace(' ', '0'); //the "XXXXXXXX"
    return lastTwoDigitsOfYear + radixOf36BasePadded; //YYXXXXXXXX
    }

Upvotes: 1

Related Questions