Hey,

Your method that you shared makes very little sense.

You pass new Date.toString() as a parameter but this is not the proper way
to create a string from a date

The proper way is to first create a SimpleDateFormat with your desired
format and then crate the string by doing .format(date)

Your method would make more sense like this:

 public static String cambiaFormatoFecha(Date fecha, String tipo) {
        String formato = tipo.equals("F") ? "yyyy-MM-dd" : tipo.equals("H")
? "hh:mm:ss" : tipo.equals("T") ? "yyyy-MM-dd hh:mm:ss" : "";
        SimpleDateFormat formaFecha = new SimpleDateFormat(formato);
        return formaFecha.format(fecha);
    }


On Fri, 25 Jun 2021 at 04:30, Shai Almog <[email protected]> wrote:

> Hi,
> I don't understand the problem. We have SimpleDateFormat in Codename One
> it's just under the codename one packages in the import.
>
> On Thursday, June 24, 2021 at 11:35:56 AM UTC+3 [email protected] wrote:
>
>> Hi,
>>
>> I created a new method in CodenameOne, but it is sending me an error.
>> To test:
>>   fecha = new Date (). ToString ()
>>   tipo = "T"
>>
>>     public static String cambiaFormatoFecha(String fecha, String tipo) {
>>         String formato = tipo.equals("F") ? "yyyy-MM-dd" :
>> tipo.equals("H") ? "hh:mm:ss" : tipo.equals("T") ? "yyyy-MM-dd hh:mm:ss" :
>> "";
>>         SimpleDateFormat formaFecha = new SimpleDateFormat(formato);
>>         Date d = null;
>>         try {
>>             d = formaFecha.parse(fecha);
>>         } catch (ParseException ex) {
>>             Dialog.show("Error", "Error formato de fecha", "Continuar",
>> null);
>>         }
>>         return formaFecha.format(d);
>>     }
>>
>> El Wednesday, June 23, 2021 a la(s) 11:17:52 PM UTC-5, [email protected]
>> escribió:
>>
>>> Hi,
>>>
>>> In java you could have the following method:
>>>
>>> public static Date ParseFecha(String fecha) {
>>>      SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
>>>      Date fechaDate = null;
>>>      try {
>>>            fechaDate = formato.parse(fecha);
>>>       }
>>>            catch (ParseException ex) {
>>>                System.out.println(ex);
>>>       }
>>>      return fechaDate;
>>>  }
>>>
>>> How can I get this to work in CodenameOne?
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "CodenameOne Discussions" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/codenameone-discussions/908e2e0c-ede8-4864-aebe-525fdc011492n%40googlegroups.com
> <https://groups.google.com/d/msgid/codenameone-discussions/908e2e0c-ede8-4864-aebe-525fdc011492n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/codenameone-discussions/CAG90yJ34TCPx-9VZW9H6_77mJni7C1TZc3BeJX-v1gEjmqwiAA%40mail.gmail.com.

Reply via email to