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

3

I am making a test code 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 this 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 the time that is in 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? 
}
    
asked by anonymous 10.12.2016 / 16:25

2 answers

5
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);

You do not convert a time period to a point in time, which is to take a point in time (the timestamp ) and add an amount of thousandths according to the < in> string displayed. This is the right concept.

I added time too, it's not in the question, but it does not make sense not to. If it is not to have, just take that part. If you have other units, how do month and year do. I did not do it, because it's in the title, but it's not in the body of the question. I would have to see what the letter used for the month would be to avoid conflicting with the minute. And decide what the time will be, because it depends on each month. For the year has the complication of the leap year.

I emphasize that this is a naive implementation and everything has to be correct to work. It may have other requirements, but the question does not make anything clear about this.

Because I do not have a bigger context of the problem I can not say much, but it seems to me that there may be a better solution.

    
10.12.2016 / 18:20
1

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

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);
    }


}

}

You have to evolve logic and improve code.

    
10.12.2016 / 19:38