Issue
I need to get a new serial in my android app. I need a serial number with a specific pattern every time when a user click a button. The serial number pattern is:
[date]_1 ... ... ... [date]_n
for example, today is 21-09-2021. so first serial number for today will be 210921_1, the next one for today will be 210921_2. And for tomorrow the first serial number will be 220921_1, the next one will be 220921_2. Like this it will continue. How can I do it? please forgive if I make any mistake.
Solution
The first step with solving a problem such as this is to break it down into the separate logic elements.
- Extract Date Today.
- Check 'number' of previous dates stored.
- Convert data to DDMMYY format.
- Append _
- Append 'number' + 1
- Increment 'number' counter.
Those are the steps. You therefore need a variable to start with that will contain the current value of the number of serials stored that day, this will start at 0.
Next you now need to understand how to get the 'date' and how to format it. Look at the APIs for Calendar and SimpleDateFormat.
That's about it.
I could post code but, there's a lot of examples for those and, really, with that logic, you should now be able to do this yourself.
Good luck!
Answered By - greysqrl
Answer Checked By - Senaida (JavaFixing Volunteer)