Java Code Examples for java.util.Calendar#setTimeInMillis()

The following examples show how to use java.util.Calendar#setTimeInMillis() . 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: MainActivity.java    From white-label-event-app with Apache License 2.0 6 votes vote down vote up
private boolean onMenuFakeDateTimeSelected() {
    if (event != null) {
        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(event.getTimezoneName()));
        cal.setTimeInMillis(clock.getCurrentTimeMillis());
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
        getSupportFragmentManager().beginTransaction()
            .add(DatePickerDialogFragment.instantiate(year, month, day_of_month), "date-picker")
            .commit();
    }
    else {
        Toast.makeText(this, "Event not yet available for TZ", Toast.LENGTH_SHORT).show();
    }
    return true;
}
 
Example 2
Source File: BoaTimeIntrinsics.java    From compiler with Apache License 2.0 5 votes vote down vote up
private static long truncToMinute(final long t, final TimeZone tz) {
	final Calendar calendar = Calendar.getInstance(tz);

	calendar.setTimeInMillis(t / 1000);
	calendar.set(Calendar.SECOND, 0);
	calendar.set(Calendar.MILLISECOND, 0);

	return calendar.getTimeInMillis() * 1000;
}
 
Example 3
Source File: Times.java    From symphonyx with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the week end time with the specified time.
 *
 * @param time the specified time
 * @return week end time
 */
public static long getWeekEndTime(final long time) {
    final Calendar end = Calendar.getInstance();

    end.setFirstDayOfWeek(Calendar.MONDAY);

    end.setTimeInMillis(time);
    end.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    end.set(Calendar.HOUR, 23);
    end.set(Calendar.MINUTE, 59);
    end.set(Calendar.SECOND, 59);
    end.set(Calendar.MILLISECOND, 999);

    return end.getTime().getTime();
}
 
Example 4
Source File: BaseSCActivity.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private void q()
{
    Calendar calendar = Calendar.getInstance();
    if (Keeper.readSyncTime() > -1L)
    {
        calendar.setTimeInMillis(Keeper.readSyncTime());
    }
    Calendar calendar1 = Calendar.getInstance();
    if (calendar.get(6) != calendar1.get(6))
    {
        int i1 = calendar1.get(11);
        UmengAnalytics.event(r, "BleSyncDataFirst", i1);
    }
}
 
Example 5
Source File: ReportJsonService.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public Long apply(Long captureTime) {
    Calendar calendar;
    switch (rollup) {
        case HOURLY:
            return CaptureTimes.getRollup(captureTime, HOURS.toMillis(1), timeZone);
        case DAILY:
            return getDailyRollupCaptureTime(captureTime).getTimeInMillis();
        case WEEKLY:
            calendar = getDailyRollupCaptureTime(captureTime);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            int diff = baseDayOfWeek - dayOfWeek;
            if (diff < 0) {
                diff += 7;
            }
            calendar.add(Calendar.DATE, diff);
            return calendar.getTimeInMillis();
        case MONTHLY:
            calendar = Calendar.getInstance(timeZone);
            calendar.setTimeInMillis(captureTime);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            if (calendar.getTimeInMillis() == captureTime) {
                return captureTime;
            } else {
                calendar.add(Calendar.MONTH, 1);
                return calendar.getTimeInMillis();
            }
        default:
            throw new IllegalStateException("Unexpected rollup: " + rollup);
    }
}
 
Example 6
Source File: DateUtils.java    From litchi with Apache License 2.0 5 votes vote down vote up
public static long getMillisecondOfDay(long ms) {
	Calendar calendar = getCalendar();
	calendar.setTimeInMillis(ms);
	calendar.set(11, 0);
	calendar.set(12, 0);
	calendar.set(13, 0);
	calendar.set(14, 0);
	return ms - calendar.getTimeInMillis();
}
 
Example 7
Source File: LocaleController.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public static String formatDateAudio(long date, boolean shortFormat) {
    try {
        date *= 1000;
        Calendar rightNow = Calendar.getInstance();
        int day = rightNow.get(Calendar.DAY_OF_YEAR);
        int year = rightNow.get(Calendar.YEAR);
        rightNow.setTimeInMillis(date);
        int dateDay = rightNow.get(Calendar.DAY_OF_YEAR);
        int dateYear = rightNow.get(Calendar.YEAR);

        if (dateDay == day && year == dateYear) {
            if (shortFormat) {
                return LocaleController.formatString("TodayAtFormatted", R.string.TodayAtFormatted, getInstance().formatterDay.format(new Date(date)));
            } else {
                return LocaleController.formatString("TodayAtFormattedWithToday", R.string.TodayAtFormattedWithToday, getInstance().formatterDay.format(new Date(date)));
            }
        } else if (dateDay + 1 == day && year == dateYear) {
            return LocaleController.formatString("YesterdayAtFormatted", R.string.YesterdayAtFormatted, getInstance().formatterDay.format(new Date(date)));
        } else if (Math.abs(System.currentTimeMillis() - date) < 31536000000L) {
            return LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, getInstance().formatterDayMonth.format(new Date(date)), getInstance().formatterDay.format(new Date(date)));
        } else {
            return LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, getInstance().formatterYear.format(new Date(date)), getInstance().formatterDay.format(new Date(date)));
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    return "LOC_ERR";
}
 
Example 8
Source File: SuntimesWidget0.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @return time of update event (in millis)
 */
protected long getUpdateTimeMillis( Context context, int appWidgetID )
{
    long suggestedUpdateMillis = WidgetSettings.getNextSuggestedUpdate(context, appWidgetID);
    if (suggestedUpdateMillis <= 0)
    {
        return getUpdateTimeMillis(null);

    } else {
        Calendar suggestedUpdate = Calendar.getInstance();
        suggestedUpdate.setTimeInMillis(suggestedUpdateMillis);
        return getUpdateTimeMillis(suggestedUpdate);
    }
}
 
Example 9
Source File: TResultSet.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Override
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
    Timestamp ts = getTimestamp(columnIndex);
    if (ts == null) {
        wasNull = true;
        return null;
    } else {
        wasNull = false;
        cal.setTimeInMillis(ts.getTime());
        return new Time(cal.getTimeInMillis());
    }
}
 
Example 10
Source File: ValueDateUtilTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCalculateDateWorkingDiff_MAR() throws KettleValueException {
  ValueMetaInterface metaA = new ValueMetaDate();
  ValueMetaInterface metaB = new ValueMetaDate();
  Calendar startDate = Calendar.getInstance();
  Calendar endDate = Calendar.getInstance();
  startDate.setTimeInMillis( 1235865600000L ); // 2009-03-01 00:00:00
  endDate.setTimeInMillis( 1238457600000L ); // 2009-03-31 00:00:00
  Object workingDayOfMAR = ValueDataUtil.DateWorkingDiff( metaA, endDate.getTime(), metaB, startDate.getTime() );
  assertEquals( "Working days count in MAR ", 22L, workingDayOfMAR );
}
 
Example 11
Source File: DateUtils.java    From dubbo-spring-boot-learning with MIT License 5 votes vote down vote up
public static long getDayOfAnyTime(long timestamp, int hour, int minute, int second) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, second);
    return calendar.getTimeInMillis();
}
 
Example 12
Source File: ZipUtilTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test public void testTimeConversion_UnixToDos_LowBound() {
  Calendar time = Calendar.getInstance();
  time.setTimeInMillis(ZipUtil.DOS_EPOCH);
  time.add(Calendar.SECOND, -1);
  thrown.expect(IllegalArgumentException.class);
  ZipUtil.unixToDosTime(time.getTimeInMillis());
}
 
Example 13
Source File: BaseEntity.java    From phone with Apache License 2.0 5 votes vote down vote up
/****
 * uinx时间戳转换为标准日期格式
 * 格式为 yyyy-MM-dd HH:mm:ss
 * @param date
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String fromUinxTime(Integer date) {
	if (date == null || date.intValue() == 0) {
		return "";
	}
	SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
	try {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date * 1000L);
		return sdf.format(c.getTime());
	} catch (Exception e) {
		e.printStackTrace();
	}
	return "";
}
 
Example 14
Source File: SharesCalculatorBeanCutOffDayIT.java    From development with Apache License 2.0 4 votes vote down vote up
private static long getEndMonth(long baseTime, int numMonths) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(getStartMonth(baseTime, numMonths));
    cal.add(Calendar.MONTH, 1);
    return cal.getTimeInMillis();
}
 
Example 15
Source File: FileUtils.java    From AndroidPicker with MIT License 4 votes vote down vote up
/**
 * 获取格式化后的文件/目录创建或最后修改时间
 */
public static String getDateTime(File file, String format) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(file.lastModified());
    return DateUtils.formatDate(cal.getTime(), format);
}
 
Example 16
Source File: BillingServiceBeanIT.java    From development with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if the results of the method "performBillingRunOrganization"
 * contain a billing result for each of the two created subscriptions and
 * validates the respective billing XML documents.
 * 
 * @throws Exception
 */

@Test
public void testPerformBillingRunForOrganization_MultipleSubscriptions()
        throws Exception {
    testYear = 2010;
    int testMonth = Calendar.MAY;
    long startTime = 1272664800000L;
    long endTime = 1273156744630L;

    Date subscriptionStartDate = getDate(testYear, testMonth, 14, 8, 0);

    // Create two subscriptions
    Subscription subscription1 = creSub(P_1_ID, SUBSCRIPTION_ID,
            subscriptionStartDate, null);

    Subscription subscription2 = creSub(P_1_ID, "sub2",
            subscriptionStartDate, null);
    // Execute
    List<BillingResult> resultsList = serviceBill
            .generateBillingForAnyPeriod(startTime, endTime, customerTkey);

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(startTime);
    Date startDate = cal.getTime();

    // Result must not be null and must contain 2 elements.
    Assert.assertNotNull("Results must not be null", resultsList);
    Assert.assertEquals(
            "There must be 2 billing results for the 2 subscriptions.", 2,
            resultsList.size());

    // The subscription keys must match those stored in the billing results.
    BillingResult result = resultsList.get(0);
    assertNotNull(result.getSubscriptionKey());
    assertEquals(subscription1.getKey(),
            result.getSubscriptionKey().longValue());

    result = resultsList.get(1);
    assertNotNull(result.getSubscriptionKey());
    assertEquals(subscription2.getKey(),
            result.getSubscriptionKey().longValue());

    // Verify the billing XML document for subscription1 and if the price
    // stored matches
    // the expected price.
    Date periods[][];
    periods = new Date[][] { { startDate, new Date(endTime) } };

    verify(XMLConverter.convertToDocument(resultsList.get(0).getResultXML(),
            true), SUBSCRIPTION_ID, periods, new BigDecimal("183.67"),
            BigDecimal.ZERO);

    // Verify the billing XML document for subscription2.
    verify(XMLConverter.convertToDocument(resultsList.get(1).getResultXML(),
            true), "sub2", periods, new BigDecimal("183.67"),
            BigDecimal.ZERO);

    // Validate the XML structure.
    xmlValidator.validateBillingResultXML();
}
 
Example 17
Source File: DateTimeType.java    From evosql with Apache License 2.0 4 votes vote down vote up
public static BigDecimal subtractMonthsSpecial(Session session,
        TimestampData a, TimestampData b) {

    long    s1    = (a.getSeconds() + a.getZone()) * 1000;
    long    s2    = (b.getSeconds() + b.getZone()) * 1000;
    boolean minus = false;

    if (s1 < s2) {
        minus = true;

        long temp = s1;

        s1 = s2;
        s2 = temp;
    }

    s1 = HsqlDateTime.getNormalisedDate(session.getCalendarGMT(), s1);
    s2 = HsqlDateTime.getNormalisedDate(session.getCalendarGMT(), s2);

    Calendar cal = session.getCalendarGMT();

    cal.setTimeInMillis(s1);

    int lastDay1;
    int months1 = cal.get(Calendar.MONTH) + cal.get(Calendar.YEAR) * 12;
    int day1    = cal.get(Calendar.DAY_OF_MONTH);

    cal.set(Calendar.DAY_OF_MONTH, 1);

    long millis = cal.getTimeInMillis();

    cal.add(Calendar.MONTH, 1);

    millis = cal.getTimeInMillis();

    cal.add(Calendar.DAY_OF_MONTH, -1);

    millis   = cal.getTimeInMillis();
    lastDay1 = cal.get(Calendar.DAY_OF_MONTH);

    cal.setTimeInMillis(s2);

    int lastDay2;
    int months2 = cal.get(Calendar.MONTH) + cal.get(Calendar.YEAR) * 12;
    int day2    = cal.get(Calendar.DAY_OF_MONTH);

    cal.set(Calendar.DAY_OF_MONTH, 1);

    millis = cal.getTimeInMillis();

    cal.add(Calendar.MONTH, 1);

    millis = cal.getTimeInMillis();

    cal.add(Calendar.DAY_OF_MONTH, -1);

    millis   = cal.getTimeInMillis();
    lastDay2 = cal.get(Calendar.DAY_OF_MONTH);

    double months;
    double days;

    if (day1 == day2 || (day1 == lastDay1 && day2 == lastDay2)) {
        months = months1 - months2;

        if (minus) {
            months = -months;
        }

        return BigDecimal.valueOf(months);
    } else if (day2 > day1) {
        months = months1 - months2 - 1;
        days   = lastDay2 - day2 + day1;
        months += days / 31;

        if (minus) {
            months = -months;
        }

        return BigDecimal.valueOf(months);
    } else {
        months = months1 - months2;
        days   = day1 - day2;
        months += days / 31;

        if (minus) {
            months = -months;
        }

        return BigDecimal.valueOf(months);
    }
}
 
Example 18
Source File: HNewsDate.java    From yahnac with Apache License 2.0 4 votes vote down vote up
public HNewsDate twoDaysAgo() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(this.calendar.getTimeInMillis() - MILLIS_IN_TWO_DAYS);
    return new HNewsDate(calendar);
}
 
Example 19
Source File: Utilities.java    From jVoiD with Apache License 2.0 4 votes vote down vote up
public static String getCurrentDate(String format) {
	DateFormat formatter = new SimpleDateFormat(format);
	Calendar calendar = Calendar.getInstance();
	calendar.setTimeInMillis(System.currentTimeMillis());
	return formatter.format(calendar.getTime());
}
 
Example 20
Source File: PodcastServiceImpl.java    From sakai with Educational Community License v2.0 3 votes vote down vote up
/**
 * Returns the date set in GMT time
 * 
 * @param date
 * 			The date represented as a long value
 * 
 * @return Date
 * 			The Date object set in GMT time
 */
public Date getGMTdate(long date) {
	final Calendar cal = Calendar.getInstance(userTimeService.getLocalTimeZone());
	cal.setTimeInMillis(date);
	
	int gmtoffset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
	
	return new Date(date - gmtoffset);
}