I am trying to strip every Time Unit of a String, e.g.
The String "4w10d50m39s" would return a TimeUnit of 4 weeks, a TimeUnit of 10 days, a TimeUnit of 50 minutes, and a TimeUnit of 39 seconds.
How can I achieve this?
Context: I need them to sum all the Time unit converted to millis and use it as a time stamp, it will be used inside a Minecraft server, in a command made to add a rank to a user for an specific amount of time, example: /addrank iLalox Vip 4w5d, that would set the expiration date to: System.currentMillis() + timeInMillis
.
To extract unit you can use regex
Demo
Then you can use
matcher
to extract matches from string and createTimeUnit
s. There is noWeek
constant inTimeUnit
, so i would present week asamount of weeks * 7
in days.Output: