Problems with conversion from milliseconds to date.

15 views
Skip to first unread message

rdvg...@gmail.com

unread,
Jun 21, 2021, 12:36:23 AM6/21/21
to CodenameOne Discussions
My app sends the date in milliseconds to a WS. Upon receiving it, I convert it to date format in the WS, but it appears to me with differences with respect to the initial value.
I decided to send the date in string format and as my logic in the app always uses the date in milliseconds, it seemed correct to create a method to return the date in string.

The problem I have is that he always returns me with a wrong month. I have tried to use DateUtil, but can't find a way to apply it.

My method is as follows and I appreciate the support.

    public static String cambiaNumeroFecha(Long fecha) {
        Calendar c = Calendar.getInstance();
        Date d = new Date(fecha);

         String r = Integer.toString(c.get(Calendar.YEAR)) + "-"
                + Integer.toString(c.get(Calendar.MONTH)) + "-"
                + Integer.toString(c.get(Calendar.DAY_OF_MONTH));
        return r;
    }

rdvg...@gmail.com

unread,
Jun 21, 2021, 12:40:00 AM6/21/21
to CodenameOne Discussions
Sorry, correction:

   public static String cambiaNumeroFecha(Long fecha) {
        Calendar c = Calendar.getInstance();
        Date d = new Date(fecha);
        c.setTime(d);

Javier Anton

unread,
Jun 21, 2021, 2:39:50 AM6/21/21
to codenameone...@googlegroups.com
Try this:

 public static String cambiaNumeroFecha(Long fecha) {
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.applyPattern("yyyy-MM-dd");
        Date d = new Date(fecha);

         return dateFormat.format(d);
    }

Javier

--
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/e9bf0310-47d8-4bcd-b9fe-dce771e3f0c6n%40googlegroups.com.

Shai Almog

unread,
Jun 21, 2021, 10:14:35 PM6/21/21
to CodenameOne Discussions
Notice that date in milliseconds is in GMT/UTC so it should work well universally even if your server is in a different time zone (which is very likely). That's probably the source of the problem you're experiencing. This is why the response from Javier should work as it removes the time/location from the date and focuses on the calendar day.
Reply all
Reply to author
Forward
0 new messages