How to convert a string with minutes / month / year to TimeStamp?

I'm making a code for testing only. In it, I have the following String:

String tempo = "1s"; //1 segundo

This string is modified all the time:

String tempo = "30d"; //30 dias

So, I want to convert that string which can be s(segundo)/m(minuto)/d(dias) into a timestamp from the current time. I want to use the current time plus time q is in the string . How to do?

String tempo = Métodos.getStringTempo();
int tempoSemLetra = Integer.valueOf(tempo.replace("m", "").replace("s", "").replace("d", ""));
long added = 0L;
if (tempo.endsWith("m")) {
   added = (tempoSemLetra*60)*1000; 
} else if (tempo.endsWith("s")) {
   added = (tempoSemLetra)*1000; 
else if (tempo.endsWith("d")) { 
   //Como converto os dias? 
}
Author: Maniero, 2016-12-10

2 answers

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 on GitHub for future reference.

You don't convert a period of time to a point in time, what you do is take a point in time (the current timestamp) and add up an amount of thousandths according to the pattern of the presented string. That is the correct concept.

I added time too, it is not in the question, but it makes no sense not to have. If it's not to have, just take that part. If you have other units, such as month and year gives do. I didn't, because it's in the title, but it's not in the body of the question. Would have to see what will be the letter used for month to not conflict with minute. And decide what will be the time taken, because it depends on each month. For year has the complication of leap year.

I stress that this is a naive implementation and everything has to be correct to work. It is possible that you have other requirements, but the question makes nothing clear about this.

For not having a context major problem I can not talk much, but it seems to me there may be better solution.

 5
Author: Maniero, 2020-03-27 12:52:06

Hello, using JodaTime, you can do more or less like that.

public class TesteData {

public static void main(String[] args) {
    String tempo = "10s"; //1 segundo
    String tempo2 = "30d"; //30 dias

    if(tempo.contains("s")){
        Integer tempoEmSegundos = Integer.parseInt(tempo.replace("s", ""));
        DateTime time = new DateTime();
        DateTime newDate =  time.plusSeconds(tempoEmSegundos);

        Date dataAtualComSegundos = newDate.toDate();
        System.out.println(dataAtualComSegundos);
    }

    if (tempo2.contains("d")){
        Integer tempoEmDias = Integer.parseInt(tempo2.replace("d", ""));
        DateTime time = new DateTime();
        DateTime newDate = time.plusDays(tempoEmDias);

        Date dataAtualComDias = newDate.toDate();
        System.out.println(dataAtualComDias);
    }


}

}

Has to evolve the logic and improve the code.

 1
Author: Bruno Spy, 2016-12-10 18:38:46