Most of the regions in the world consider Saturday and Sunday as Weekends which means Monday to Friday are considered as weekdays. However, in middle-eastern countries, Friday and Saturday are considered as Weekends whereas Sunday to Thursday are considered as weekdays.
Is there an elegant way in java to lookup a previous weekday based on the currency type and current day?
For example, Consider current day is 21-JUN-2021
For middle-eastern currency, the weekday should be 20-JUN-2021 For all other currencies, the weekday should be 18-JUN-2021.
There isn't, and not because of missing APIs, but because of fundamental issues.
Currency and weekdays have absolutely no relationship whatsoever. Clearly, asking to go from currency to the concept of weekday/weekend directly is crazy talk.
You're looking for an indirect path. But, the steps along that path don't work.
The general path you're looking for is:
Both steps are essentially impossible. At best there are maintenance nightmare hacks and handwritten lists.
The second step is hard because the concept of which days are considered weekends are not baked into the APIs - there simply is no way to ask e.g. any class in the
java.timepackage, even if you have a locale, which days are weekend days.The first step is hard as well. The step 'locale -> currency' mostly works, but it's not reversible. For example, mainland europe has a ton of locales, but only one currency. Thus, trying to go from "EURO" to "which locale is that?" is not a single answer, but would look like a sizable list.
This is your best shot:
nl-NLwas the dutch guilder. Afterwards, it was the euro.If I haven't dissuaded you yet, I'm losing my edge.
But, hey, if you wanna go on:
If that is palatable to you (an algorithm that will attempt to answer the question 'given this currency, tell me about which days are considered weekends' - but it will often answer "I do not know"), you could write it. It's non-trivial because generally speaking nobody wants this algorithm (the whole 'often it cannot tell you' part is the problem. Nobody likes it if the app just flat out doesn't work in many places, and a lot of the convenience is gone if all the algorithm can give you is a nice default for some setting at best).
How did you get at currency in the first place? If you got a currency from a locale, then take that locale and go from there, that's much, much simpler.
Make a currency to locale map
Relevant operations:
You don't actually want
Currency.getInstance, you want the reverse (currency to locale, not locale to currency). This method does not exist. You'd have to make aMap<Currency, List<Locale>>first (and this is a pricey operation, so do it once and cache the results in memory).You'll get a ton of overlap. It'll look something like:
Do a lookup for a currency
Given a currency, fetch the list of locales (use
thatMap.getOrDefault(theCurrency, List.of())), and throw it through your 'give me the weekends' algorithm, which is a convoluted mess I leave to you that optimally cannot just answer based on a locale, but needs to know the weather, have an engine to determine easter, all the other weird religious holidays I didn't manage to think of, and needs to include the date around which you wanna know, given that locales switch official holidays and currencies and the like.So, how you write that - beats me. But if you have it, throw all locales through there, and check if you get a single set of answers.
How do I determine the weekend days given a locale?
You'd have to write that yourself. Start with a list of locales. Read up on easter and eid-al-fitr. Accept it'll never be perfect.