How to convert a string to date

46 views
Skip to first unread message

rdvg...@gmail.com

unread,
Jun 24, 2021, 12:17:52 AM6/24/21
to CodenameOne Discussions
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?

rdvg...@gmail.com

unread,
Jun 24, 2021, 4:35:56 AM6/24/21
to CodenameOne Discussions
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);
    }

Shai Almog

unread,
Jun 24, 2021, 11:30:41 PM6/24/21
to CodenameOne Discussions
Hi,
I don't understand the problem. We have SimpleDateFormat in Codename One it's just under the codename one packages in the import.

Dave Dyer

unread,
Jun 25, 2021, 5:28:11 PM6/25/21
to CodenameOne Discussions
The problem with SimpleDateFormat is that it can't cope with a lot of the junk that passes for "date" out there
in the real world.  I gave up and wrote my own parser, which I have extended to handle the dates that I actually
encounter.

rdvg...@gmail.com

unread,
Jun 25, 2021, 8:04:48 PM6/25/21
to CodenameOne Discussions
Hi,

Thanks for the comments, I wish you could do the test of the method that I included with the test data and validate the problem that I am detecting.

Shai Almog

unread,
Jun 26, 2021, 1:43:57 AM6/26/21
to CodenameOne Discussions
Hi,
the initial code you listed should work fine in Codename One. What isn't working when you try to run that code?

rdvg...@gmail.com

unread,
Jun 26, 2021, 2:11:46 AM6/26/21
to CodenameOne Discussions
Hi,

When execute   String f  = cambiaFormatoFecha(new Date().toString(), "F");
Return "Invalid year value".

Javier Anton

unread,
Jun 26, 2021, 4:58:03 AM6/26/21
to codenameone...@googlegroups.com
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);
    }


--
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 codenameone-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/908e2e0c-ede8-4864-aebe-525fdc011492n%40googlegroups.com.

Javier Anton

unread,
Jun 26, 2021, 5:03:19 AM6/26/21
to codenameone...@googlegroups.com
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 );
    }

Javier Anton

unread,
Jun 26, 2021, 6:07:23 AM6/26/21
to codenameone...@googlegroups.com
The key point is that new Date().toString() will produce different formats depending on each user's culture, so it cannot be relied upon to go into SimpleDateFormat.parse

rdvg...@gmail.com

unread,
Jun 26, 2021, 11:53:19 AM6/26/21
to CodenameOne Discussions

Thank you
Reply all
Reply to author
Forward
0 new messages