'codenameone convert time to utc

I am working on codename one project and I am struggling to convert device time to UTC. I use this code :

Calendar cal = Calendar.getInstance();
System.out.println(cal.getTimeZone());
TimeZone tzUTC = TimeZone.getTimeZone("UTC");
com.codename1.l10n.DateFormat dtfmt = new com.codename1.l10n.SimpleDateFormat("EEE, yyyy-MM-dd KK:mm a z");
dtfmt.setTimeZone(tzUTC);
System.out.println("UTC: " + dtfmt.format(cal.getTime()));

and codename one reject the setTImeZone method.

I use java.text.DateFormat but when I run it, condename one cant compile it also.



Solution 1:[1]

It may not really answer your real question, but the following works for me:

    Calendar cal = Calendar.getInstance();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
    TimeZone tzUtc = TimeZone.getTimeZone("UTC");
    df.setTimeZone(tzUtc);
    System.out.println("UTC: " + df.format(cal.getTime()));

I don’t know com.codename1.l10n.DateFormat, so I’m sorry I cannot help you there.

Solution 2:[2]

Use:

    java.util.Calendar cal = java.util.Calendar.getInstance();
    TimeZone tz = cal.getTimeZone();
    cal.setTime(new Date(System.currentTimeMillis() - tz.getRawOffset()));
    com.codename1.l10n.DateFormat dtfmt = new com.codename1.l10n.SimpleDateFormat("EEE, yyyy-MM-dd KK:mm a");
    System.out.println("UTC: " + dtfmt.format(cal.getTime()));

Then append UTC to the string as the value is always UTC.

Solution 3:[3]

Was using Shai's solution but noticed that it wasn't giving the correct UTC time when the device timezone was in daylight savings time. Below is a more general solution using tz.getOffset() instead of tz.getRawOffset(). Seems like there should be simpler way!

        L10NManager l10n = L10NManager.getInstance();
        long sysRtnTime = System.currentTimeMillis() / 1000L;
        Date errorDate = new Date(sysRtnTime * 1000L);
        Date currentDate = new Date();
        String deviceTime = l10n.formatDateTime(errorDate);
        Calendar c = Calendar.getInstance();
        long unixtime = errorDate.getTime();
        TimeZone tz = c.getTimeZone();
        // to get offset, we need era, year, month, day, dayOfWeek,millis
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
        SimpleDateFormat monthDateFormat = new SimpleDateFormat("MM");
        SimpleDateFormat dayDateFormat = new SimpleDateFormat("dd");
        SimpleDateFormat dayOfWeekDateFormat = new SimpleDateFormat("F");
        SimpleDateFormat millisDateFormat = new SimpleDateFormat("S");
        int year = Integer.parseInt(yearFormat.format(currentDate));
        int month = Integer.parseInt(monthDateFormat.format(currentDate));
        int day = Integer.parseInt(dayDateFormat.format(currentDate));
        int dayOfWeek = Integer.parseInt(dayOfWeekDateFormat.format(currentDate));
        int millis = Integer.parseInt(millisDateFormat.format(currentDate));
//            c.setTime(new Date(unixtime - tz.getRawOffset()));    // c in UTC only if device not DST           
        month = month - 1; // since getOffset assume 0 = Jan and 11 = Dec
        c.setTime(new Date(unixtime - tz.getOffset(1, year, month, day, dayOfWeek, millis)));    // c in UTC (even if device in DST)
        Date cDateUTC = c.getTime();                          // sDate in UTC
        String timeInUTC = serverDateFormat.format(cDateUTC);
        Log.p("Time (in device timezone): " + deviceTime);
        Log.p("Time (in UTC): " + timeInUTC);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ole V.V.
Solution 2
Solution 3