Reputation: 71
According to the requirement i want to get a unique reference according to following format.
Reference should be = 15 Chars Date Format(yyyy-MM--dd) + ZERO`s + Id ; ex 1 = 201911070000181 ex 2 = 201911070000090
In the code sample i have shown date as string and Id as string can anyone help me here? Thanks.
Date today = Utils.getCurrentDateByTimeZone(environment.getProperty(TIME_ZONE));
String pattern = "yyyyMMdd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(today);
String merchantId = String.valueOf(transactionData.getMerchant().getMerchantId());
Upvotes: 1
Views: 59
Reputation: 301
You can use String format to do this.
String.format("%s%07d", date, id);
// if date="20191107" and id=181, this will give 201911070000181
What this code does is, it appends 0's before id until its length is 7. (The first part is date and I assume that date is always 8 characters long.) It won't work if id is string so I suggest casting it to integer.
Upvotes: 2
Reputation: 393841
You can always use a StringBuilder
:
StringBuilder sb = new StringBuilder(15);
sb.append(date);
for (int i = date.length() ; i < 15 - merchantId.length(); i++) {
sb.append('0');
}
sb.append (merchantId);
String reference = sb.toString();
Upvotes: 1