Converting a String to Localdate

Asked

Viewed 1,946 times

-3

I was taking a date in a jLabel in the format dd/MM/yyyy and trying to convert it to Localdate and then use it with Localdate methods, but I ran into some errors and found nothing on the internet that could solve.

Below is the correct way to convert string to Localdate.


        /*
Convertendo a string em LocalDate no formato padrão (yyyy-MM-dd). O método ofPattern deve ter a mascara que esta sendo utilizado no source (jLabelDia), então, ao converter em LocalDate, o formato automaticamente passa a ser iso (padrão do LocalDate yyyy-MM-dd), então vc pode trabalhar com os métodos da LocalDate.
*/

        String data = jLabelDia.getText();
        LocalDate ld = LocalDate.parse(data, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        String dia = String.valueOf(ld.minusDays(1));
        jLabelDia.setText(dia);
        
        

  • The format that localDate works by default is yyyy-MM-dd. The mask you use in the method of.Pattern is the mask that is being used in the souce, in this case, the jLabelDia that has the dd/MM/yyyy mask as informed at the beginning of the question.

  • corrected title.

  • It should work, see: https://ideone.com/xbeMQx - my guess is jLabelDia.getText() is returning something else (maybe with a space or another "invisible" character at the beginning)

2 answers

0

The code is not wrong, if the entry is 15/08/2020 it will work as expected. By doing String.valueOf(ld.minusDays(1)); the value returned will be in ISO format: yyyy-MM-dd. By so assigning in jLabelDia the next execution will fail as it waits for the date on dd/MM/yyyy.

This can help:

String data = jLabelDia.getText();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate ld = LocalDate.parse(data, formatter);
String dia = ld.minusDays(1).format(formatter); 
jLabelDia.setText(dia);

0

The goal of the post was to show how to convert a string to Localdate and the example given with Ld.minusDay was an example of the operation working.

But to have a sequence in the next run just use the Datatimeformatter to convert the string to the default mask again and set.

LocalDate ld1 = LocalDate.parse(dia, DateTimeFormatter.ISO_DATE);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        jLabelDia.setText(ld1.format(formatter));

Source: https://docs.oracle.com/javase/tutorial/datetime/iso/format.html

  • In the question you say you are receiving an Exception, but your code with the input 15/08/2020 works as expected, without errors. You could fix the title and describe better what the problem was and how it was fixed.

  • In fact, my post has no errors, it was more to leave an example here because I did not find any post referring to string conversion to Localdate that was deployed in java8 if I am not mistaken. I just learned how to use Localdate by reading the Oracle documentation, but as I said, I ran into it when it came to parsing.... I left the correct example to be useful for anyone looking for a solution to this Parseexcepttion problem. Maybe other people fall for the same mistake.

  • If the idea was this, it would be better to put a code that gives the error, for example (and eliminate what does not interfere, as the question of jlabel, since it is irrelevant where the string comes from). Anyway, in a quick search for "localdate parse" I found that, that and much more :-)

  • @hkotsubo, do any of these solutions convert a string to Localdate as I reported? That is, in the correct way as the documentation of the oracle informs? Alias, until simple after we learn.... My intention was to help, but if you’re in the way, you can delete.... If you had bothered to research how I researched you would see that you don’t have.... criticizing is very easy but doing something good is difficult... if you don’t want to help, don’t disturb.

  • I’m sorry if it doesn’t seem like it, but I’m trying to help you. Understand that the format of the site is that the questions are useful to any future visitor, and the way the question is, it got confused because the title says it’s a mistake but the code of the question - apparently - does not give this error. I just suggested how to edit the question to make it more suitable for the site format. Please take it as constructive criticism, I didn’t mean to "oblige". And please visit the links I have indicated and read all the answers, which many show how to make the conversion of String for LocalDate.

  • When parsing from a string to Localdate, it automatically converts the format you specified in the expression to iso. The goal is to perform date operations using the standard iso format. So, if you find a post that shows the correct way to convert a string in the dd/MM/yyyy format to Localdate in the iso format, I delete mine. If you have a suggestion for my title, I am all ears.... I tried to put in the title the very common mistake when doing the parse.... I leave it to the moderators to resolve.

  • In fact LocalDate has no format (read the links indicated that the answers speak of this too - mainly that one). What happens is that String.valueOf calls the method toString, and this converts the date to ISO format. Anyway, the title could be something along these lines: when you put a Localdate on the jlabel the date is no longer in the same format or something, and in the question you describe this problem step by step. So I think it would look much better :-)

  • It is precisely this misconception that has hindered me and that is exactly what I am trying to show in this post. According to the Oracle documentation Localdate has an Iso standard format when performing parse. I left the correct solution on the link you posted. The one-argument parse(Charsequence) method in the Localdate class uses the ISO_LOCAL_DATE Formatter. Source: https://docs.oracle.com/javase/tutorial/datetime/iso/format.html

  • @hkotsubo, I see that you are more experienced than I am in java, if you can understand and revise what I have shown, I thank you very much, because I am making a system where you have a schedule to consult and the only solution that worked was the one that I posted, when it comes to converting string to Localdate... so far it is working correctly. ....

  • If I understand you correctly, I think Damião Martins' answer already solves your problem. Now I’m on my cell, tomorrow or later if no one else answers I see if it can complement something

  • @hkotsubo, I wanted to post the resolution of the problem, just for not finding any correct, excuse me but including yours that you linked here too. My difficulty was taking a string date, moving to Localdate and performing operations with the dates. The example I posted was just didactic.

Show 6 more comments

Browser other questions tagged

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