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

Asked

Viewed 195 times

3

I am 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 this string which may be s(segundo)/m(minuto)/d(dias) in a timestamp from the current time. I want to use the current time plus the 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? 
}
  • 1

    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.

  • Everything is following this pattern

  • 1

    And what’s the pattern?

  • 1

    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.

  • @Davidev edit the question and add to it. The comment field is not ideal for code.

  • 1

    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?

Show 1 more comment

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

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.

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.