1

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

Where

  • YY is calendar year
  • XXXXXXXX is id which is alphanumeric counter (starting sequence number 00000001 to ZZZZZZZZ every year)

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?

James Z
  • 12,209
  • 10
  • 24
  • 44
D3monTw
  • 11
  • 1
  • Hi and welcome to stackoverflow. What have you tried thus far? What are you getting stuck on? Stackoverflow is not a 'homework/write my code for me' service. You'll be more likely to get people help you, if you show to have put in some effort yourself beforehand. – D Kramer Nov 03 '20 at 15:15
  • That description looks like a base 36 number system. All you have to do is make a function that takes an int and return it in base 36. Then it's just a matter of concatenating year. – Sathimantha Malalasekera Nov 03 '20 at 15:45
  • Can you pls suggeat with example – D3monTw Nov 04 '20 at 18:04

1 Answers1

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
    }