String tempo = Métodos.getStringTempo();
int tempoSemLetra = Integer.valueOf(tempo.replace("s", "").replace("m", "")
.replace("h", "").replace("d", ""));
long added = 0L; //não sei porque precisa de long
if (tempo.endsWith("s")) {
added = tempoSemLetra * 1000;
} else if (tempo.endsWith("m")) {
added = tempoSemLetra * 60 * 1000;
} else if (tempo.endsWith("m")) {
added = tempoSemLetra * 60 * 60 * 1000;
else if (tempo.endsWith("d")) {
added = tempoSemLetra * 24 * 60 * 60 * 1000;
}
Timestamp timestamp = new Timestamp(System.currentTimeMillis() + added);
I put in the Github for future reference.
You don’t convert a time period into a point in time, what you do is you take a point in time (the timestamp) and add a number of thousandths according to the standard of the string presented. This is the correct concept.
I added time too, it is not in the question, but it makes no sense not to have. If it is not to have, just take that part. If you have other units, like month and year you can do it. I didn’t do it because it’s in the title, but it’s not in the body of the question. You would have to see which letter will be used for month to not conflict with minute. And decide which time will be adopted, because it depends on each month. Next year there is the complication of the leap year.
I stress that this is a naive implementation and everything has to be right to work. You may have other requirements, but the question makes nothing clear about this.
For not having a larger context of the problem I can not talk much, but it seems to me there can be better solution.
If you have the amount of seconds it’s easy, but having to do Parsing of string, see if everything is ok, already complicates a little and is probably too wide to answer. I know someone will fall for the temptation to do a superficial analysis that works only if everything is perfect, but it breaks if something comes out of pattern.
– Maniero
Everything is following this pattern
– DaviDEV
And what’s the pattern?
– user28595
I don’t even know if you have everything you need. It is common for requirements not to be right, it needs to have clear, complete and unambiguous indication of what you want. To tell you the truth I don’t even know if you should do this, since you’re looking to convert a period of time into a point in time (timestamp), which are concepts that seem to be the same, but very different, so this conversion doesn’t even make sense unless you can justify it, which I doubt. Anyway it would be good to put what has already done for us to see where is your difficulty.
– Maniero
@Davidev edit the question and add to it. The comment field is not ideal for code.
– user28595
This code is not enough to understand your problem unless you just don’t know how to find the time of day. Even if everything was still clear, how and why do you want to convert a period of time into a point in time? Do you understand these concepts? You understand you’re trying to turn an orange into an apple?
– Maniero