Java Code Examples for android.text.format.Time#switchTimezone()

The following examples show how to use android.text.format.Time#switchTimezone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormatUtil.java    From sms-ticket with Apache License 2.0 5 votes vote down vote up
public static Time timeFrom3339(String s3339) {
    final Time time = new Time();
    if (TextUtils.isEmpty(s3339)) {
        return time;
    }
    try {
        time.parse3339(s3339);
    } catch (TimeFormatException e) {
        return time;
    }
    time.switchTimezone(Time.getCurrentTimezone());
    return time;
}
 
Example 2
Source File: TicketsAdapter.java    From sms-ticket with Apache License 2.0 5 votes vote down vote up
/**
 * Gets validity of ticket with time {@code t} in minutes.
 */
public static int getValidityMinutes(Time t) {
    if (t == null) {
        return -1;
    }

    final Time now = new Time();
    now.setToNow();
    now.switchTimezone(Time.getCurrentTimezone());
    return (int)Math.ceil((t.toMillis(true) - now.toMillis(true)) / 1000d / 60d);
}
 
Example 3
Source File: City.java    From sms-ticket with Apache License 2.0 4 votes vote down vote up
protected Time parseDate(String sms, String datePattern, Ticket ticket) throws ParseException {
    Matcher m = Pattern.compile(datePattern).matcher(sms);
    if (m.find()) {
        String d = m.group(1);
        if (!TextUtils.isEmpty(d)) {
            d = d.replaceAll(";", "");

            for (int i = 0; i < 2; i++) {
                final Date date;
                try {
                    if (i == 0) {
                        date = sdf.parse(d); // full date/time
                    } else if (i == 1 && sdfTime != null) {
                        date = sdfTime.parse(d); // only time
                    } else {
                        break;
                    }
                } catch (Exception e) {
                    continue;
                }

                if (i == 1 && ticket != null && ticket.getValidFrom() != null) {
                    final Date prevDate = new Date(ticket.getValidFrom().toMillis(true));
                    date.setYear(prevDate.getYear());
                    date.setMonth(prevDate.getMonth());
                    date.setDate(prevDate.getDate());
                }

                final Calendar c = Calendar.getInstance();
                c.setTime(date);

                final Time t = new Time();
                synchronized (format3339) {
                    String zone = timezone.format(date);
                    zone = zone.substring(0, 3) + ":" + zone.substring(3);
                    String s = format3339.format(date) + zone;
                    t.parse3339(s);
                    t.switchTimezone(Time.getCurrentTimezone());
                }

                return t;
            }
        }
    }

    throw new ParseException("Cannot parse date from the message: " + sms, 0);
}
 
Example 4
Source File: TimeFieldEditor.java    From opentasks with Apache License 2.0 3 votes vote down vote up
/**
 * Updates a {@link Time} instance to date and time of the given time zone, but in the original time zone.
 * <p>
 * Example:
 * </p>
 * <p>
 * <pre>
 * input time: 2013-04-02 16:00 Europe/Berlin (GMT+02:00)
 * input timeZone: America/New_York (GMT-04:00)
 * </pre>
 * <p>
 * will result in
 * <p>
 * <pre>
 * 2013-04-02 10:00 Europe/Berlin (because the original time is equivalent to 2013-04-02 10:00 America/New_York)
 * </pre>
 * <p>
 * All-day times are not modified.
 *
 * @param time
 *         The {@link Time} to update.
 * @param timeZone
 *         A time zone id.
 */
private void applyTimeInTimeZone(Time time, String timeZone)
{
    if (!time.allDay)
    {
        /*
         * Switch to timeZone and reset back to original time zone. That updates date & time to the values in timeZone but keeps the original time zone.
         */
        String originalTimeZone = time.timezone;
        time.switchTimezone(timeZone);
        time.timezone = originalTimeZone;
        time.set(time.second, time.minute, time.hour, time.monthDay, time.month, time.year);
    }
}