Furthermore, if your source Date is actually a string instead of a Date,
you can do this:

public static String cambiaFormatoFecha(String fechaOrigenString,  String
tipoOrigen, String tipoDestino) {
        String formatoOrigen = tipoOrigen.equals("F") ? "yyyy-MM-dd" :
tipoOrigen.equals("H") ? "hh:mm:ss" : tipoOrigen.equals("T") ? "yyyy-MM-dd
hh:mm:ss" : "";
        SimpleDateFormat formaFechaOrigen = new
SimpleDateFormat(formatoOrigen );
        Date fechaOrigen = formaFechaOrigen.parse(fechaOrigenString);
        String formatoDestino = tipoDestino.equals("F") ? "yyyy-MM-dd" :
tipoDestino.equals("H") ? "hh:mm:ss" : tipoDestino.equals("T") ?
"yyyy-MM-dd hh:mm:ss" : "";
        SimpleDateFormat formaFechaDestino = new
SimpleDateFormat(formatoDestino);
        return formaFechaDestino.format(fechaOrigen );
    }

On Sat, 26 Jun 2021 at 09:57, Javier Anton <[email protected]> wrote:

> 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/CAG90yJ37ULCJ-p5h5uEFF4Tvh4vAauh6Z0rUjTqe187Pak2Tvg%40mail.gmail.com.

Reply via email to