Java Code Examples for android.icu.util.Calendar#getTimeInMillis()

The following examples show how to use android.icu.util.Calendar#getTimeInMillis() . 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: TimeZoneTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestGetOffsetDate() {
    Calendar cal = Calendar.getInstance();
    cal.set(1997, Calendar.JANUARY, 30);
    long date = cal.getTimeInMillis();

TimeZone tz_icu = TimeZone.getTimeZone("America/Los_Angeles");
int offset = tz_icu.getOffset(date);
if (offset != -28800000) {
    errln("expected offset -28800000, got: " + offset);
}

cal.set(1997, Calendar.JULY, 30);
date = cal.getTimeInMillis();
offset = tz_icu.getOffset(date);
if (offset != -25200000) {
    errln("expected offset -25200000, got: " + offset);
}
}
 
Example 2
Source File: GlobalizationPreferencesTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestJB5380() {
    GlobalizationPreferences gp = new GlobalizationPreferences();
    GregorianCalendar gcal = new GregorianCalendar();

    // set way old date
    gcal.set(Calendar.YEAR, 1950);

    // set calendar to GP
    gp.setCalendar(gcal);

    Calendar cal = gp.getCalendar();
    // Calendar instance returned from GP should be initialized
    // by the current time
    long timeDiff = System.currentTimeMillis() - cal.getTimeInMillis();
    if (Math.abs(timeDiff) > 1000) {
        // if difference is more than 1 second..
        errln("FAIL: The Calendar was not initialized by current time - difference:" + timeDiff);
    }
}
 
Example 3
Source File: CalendarViewLegacyDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a calendar for locale bootstrapped with the value of a given calendar.
 *
 * @param oldCalendar The old calendar.
 * @param locale The locale.
 */
private static Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
    if (oldCalendar == null) {
        return Calendar.getInstance(locale);
    } else {
        final long currentTimeMillis = oldCalendar.getTimeInMillis();
        Calendar newCalendar = Calendar.getInstance(locale);
        newCalendar.setTimeInMillis(currentTimeMillis);
        return newCalendar;
    }
}
 
Example 4
Source File: CalendarViewLegacyDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the month displayed at the top of this view based on time. Override
 * to add custom events when the title is changed.
 *
 * @param calendar A day in the new focus month.
 */
private void setMonthDisplayed(Calendar calendar) {
    mCurrentMonthDisplayed = calendar.get(Calendar.MONTH);
    mAdapter.setFocusMonth(mCurrentMonthDisplayed);
    final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY
            | DateUtils.FORMAT_SHOW_YEAR;
    final long millis = calendar.getTimeInMillis();
    String newMonthName = DateUtils.formatDateRange(mContext, millis, millis, flags);
    mMonthName.setText(newMonthName);
    mMonthName.invalidate();
}
 
Example 5
Source File: CalendarViewLegacyDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @return Returns the number of weeks between the current <code>date</code>
 *         and the <code>mMinDate</code>.
 */
private int getWeeksSinceMinDate(Calendar date) {
    if (date.before(mMinDate)) {
        throw new IllegalArgumentException("fromDate: " + mMinDate.getTime()
                + " does not precede toDate: " + date.getTime());
    }
    long endTimeMillis = date.getTimeInMillis()
            + date.getTimeZone().getOffset(date.getTimeInMillis());
    long startTimeMillis = mMinDate.getTimeInMillis()
            + mMinDate.getTimeZone().getOffset(mMinDate.getTimeInMillis());
    long dayOffsetMillis = (mMinDate.get(Calendar.DAY_OF_WEEK) - mFirstDayOfWeek)
            * MILLIS_IN_DAY;
    return (int) ((endTimeMillis - startTimeMillis + dayOffsetMillis) / MILLIS_IN_WEEK);
}
 
Example 6
Source File: DatePickerSpinnerDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a calendar for locale bootstrapped with the value of a given calendar.
 *
 * @param oldCalendar The old calendar.
 * @param locale The locale.
 */
private Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
    if (oldCalendar == null) {
        return Calendar.getInstance(locale);
    } else {
        final long currentTimeMillis = oldCalendar.getTimeInMillis();
        Calendar newCalendar = Calendar.getInstance(locale);
        newCalendar.setTimeInMillis(currentTimeMillis);
        return newCalendar;
    }
}
 
Example 7
Source File: DateTimeView.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @param timeZone the timezone we are in
 * @return the timepoint in millis at UTC at midnight in the current timezone
 */
private long computeNextMidnight(TimeZone timeZone) {
    Calendar c = Calendar.getInstance();
    c.setTimeZone(libcore.icu.DateUtilsBridge.icuTimeZone(timeZone));
    c.add(Calendar.DAY_OF_MONTH, 1);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTimeInMillis();
}
 
Example 8
Source File: TimePicker.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
@Override
public long getDate() {
    Calendar cal = Calendar.getInstance(mLocale);
    cal.set(Calendar.HOUR_OF_DAY, getHour());
    cal.set(Calendar.MINUTE, getMinute());
    return cal.getTimeInMillis();
}
 
Example 9
Source File: DatePickerSpinnerDelegate.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a calendar for locale bootstrapped with the value of a given calendar.
 *
 * @param oldCalendar The old calendar.
 * @param locale      The locale.
 */
private Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
    if (oldCalendar == null) {
        return Calendar.getInstance(locale);
    } else {
        final long currentTimeMillis = oldCalendar.getTimeInMillis();
        Calendar newCalendar = Calendar.getInstance(locale);
        newCalendar.setTimeInMillis(currentTimeMillis);
        return newCalendar;
    }
}
 
Example 10
Source File: CalendarTestFmwk.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert year,month,day values to the form "year/month/day".
 */
static public String ymdToString(Calendar cal) {
    double day = getJulianDay(cal);
    if (cal instanceof ChineseCalendar) {
        return "" + cal.get(Calendar.EXTENDED_YEAR) + "/" +
            (cal.get(Calendar.MONTH)+1) +
            (cal.get(Calendar.IS_LEAP_MONTH)==1?"(leap)":"") + "/" +
            cal.get(Calendar.DATE) + " (" + day + ", time=" + cal.getTimeInMillis() + ")";
    }
    return ymdToString(cal.get(Calendar.EXTENDED_YEAR),
                        cal.get(MONTH), cal.get(DATE)) +
                        " (" + day + ", time=" + cal.getTimeInMillis() + ")";
}
 
Example 11
Source File: DateIntervalFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetInstance_String_DateIntervalInfo() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(new ULocale("ca"));
    DateIntervalFormat dateIntervalFormat = DateIntervalFormat.getInstance(
            DateFormat.YEAR_MONTH, Locale.ENGLISH, dateIntervalInfo);
    Calendar from = Calendar.getInstance();
    from.set(2000, Calendar.JANUARY, 1, 12, 0);
    Calendar to = Calendar.getInstance();
    to.set(2001, Calendar.FEBRUARY, 1, 12, 0);
    DateInterval interval = new DateInterval(from.getTimeInMillis(), to.getTimeInMillis());
    dateIntervalFormat.setTimeZone(from.getTimeZone());
    // Month names are default (English), format is Catalan
    assertEquals("Wrong date interval",
            "January de 2000 – February de 2001", dateIntervalFormat.format(interval));
}
 
Example 12
Source File: DateIntervalFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetInstance_String_Locale_DateIntervalInfo() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(new ULocale("ca"));
    DateIntervalFormat dateIntervalFormat = DateIntervalFormat.getInstance(
            DateFormat.YEAR_MONTH, Locale.GERMAN, dateIntervalInfo);
    Calendar from = Calendar.getInstance();
    from.set(2000, Calendar.JANUARY, 1, 12, 0);
    Calendar to = Calendar.getInstance();
    to.set(2001, Calendar.FEBRUARY, 1, 12, 0);
    DateInterval interval = new DateInterval(from.getTimeInMillis(), to.getTimeInMillis());
    dateIntervalFormat.setTimeZone(from.getTimeZone());
    // Month names are German, format is Catalan
    assertEquals("Wrong date interval",
            "Januar de 2000 – Februar de 2001", dateIntervalFormat.format(interval));
}