Java Code Examples for java.util.Calendar.getTimeInMillis()

The following are Jave code examples for showing how to use getTimeInMillis() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: javaportfolio   File: XIRRData.java   View Source Code Vote up 7 votes
/**
	 * Calculates the number of days between two calendar days in a manner
	 * which is independent of the Calendar type used.
	 *
	 * @param d1    The first date.
	 * @param d2    The second date.
	 *
	 * @return      The number of days between the two dates.  Zero is
	 *              returned if the dates are the same, one if the dates are
	 *              adjacent, etc.  The order of the dates
	 *              does not matter, the value returned is always >= 0.
	 *              If Calendar types of d1 and d2
	 *              are different, the result may not be accurate.
	 */
	public static int getDaysBetween( Calendar d1, Calendar d2 ) {
		if ( d1.after(d2) ) {
			// swap dates so that d1 is start and d2 is end
			Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		long days = (d2.getTimeInMillis() - d1.getTimeInMillis()) / (1000 * 60 * 60 * 24);
		return (int) days;
		
		
//		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
//		int y2   = d2.get(Calendar.YEAR);
//		if (d1.get(Calendar.YEAR) != y2) {
//			d1 = (Calendar) d1.clone();
//			do {
//				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
//				d1.add(Calendar.YEAR, 1);
//			} while (d1.get(Calendar.YEAR) != y2);
//		}
//		return days;
	}
 
Example 2
Project: automat   File: DateUtil.java   View Source Code Vote up 7 votes
/**
 * 间隔天数
 * 
 * @param startDate
 * @param endDate
 * @return
 */
public static final Integer getDayBetween(Date startDate, Date endDate) {
	Calendar start = Calendar.getInstance();
	start.setTime(startDate);
	start.set(Calendar.HOUR_OF_DAY, 0);
	start.set(Calendar.MINUTE, 0);
	start.set(Calendar.SECOND, 0);
	start.set(Calendar.MILLISECOND, 0);
	Calendar end = Calendar.getInstance();
	end.setTime(endDate);
	end.set(Calendar.HOUR_OF_DAY, 0);
	end.set(Calendar.MINUTE, 0);
	end.set(Calendar.SECOND, 0);
	end.set(Calendar.MILLISECOND, 0);

	long n = end.getTimeInMillis() - start.getTimeInMillis();
	return (int) (n / (60 * 60 * 24 * 1000l));
}
 
Example 3
Project: letv   File: LetvUtils.java   View Source Code Vote up 7 votes
public static int getMinusDaysBetweenTwoDate(long endTime, long startTime) {
    Date endDate = new Date(endTime);
    Date beginDate = new Date(startTime);
    Calendar c = Calendar.getInstance();
    c.setTime(endDate);
    c.set(11, 0);
    c.set(12, 0);
    c.set(13, 0);
    c.set(14, 0);
    long endMilliSec = c.getTimeInMillis();
    c.setTime(beginDate);
    c.set(11, 0);
    c.set(12, 0);
    c.set(13, 0);
    c.set(14, 0);
    return (int) ((endMilliSec - c.getTimeInMillis()) / 86400000);
}
 
Example 4
Project: the-vigilantes   File: TimeUtil.java   View Source Code Vote up 6 votes
final static Date fastDateCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day) {

        Calendar dateCal = cal;

        if (useGmtConversion) {

            if (gmtCalIfNeeded == null) {
                gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
            }

            dateCal = gmtCalIfNeeded;
        }

        synchronized (dateCal) {
            java.util.Date origCalDate = dateCal.getTime();
            try {
                dateCal.clear();
                dateCal.set(Calendar.MILLISECOND, 0);

                // why-oh-why is this different than java.util.date, in the year part, but it still keeps the silly '0' for the start month????
                dateCal.set(year, month - 1, day, 0, 0, 0);

                long dateAsMillis = dateCal.getTimeInMillis();

                return new Date(dateAsMillis);
            } finally {
                dateCal.setTime(origCalDate);
            }
        }

    }
 
Example 5
Project: oscm   File: GetRevenueListCommand.java   View Source Code Vote up 6 votes
/**
 * Sets hour, minute, second and millisecond of the provided time stamp to
 * zero and converts it to the server time zone.
 * 
 * @param timeStamp
 *            the time stamp to convert
 * @return the time stamp representing the day 00:00:00 000 passed in the
 *         time stamp in the current time zone
 */
private static final long setTimeToZeroInCurrentTimeZone(long timeStamp) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(timeStamp);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}
 
Example 6
Project: jdk8u-jdk   File: TimestampTests.java   View Source Code Vote up 6 votes
@Test
public void test50() {
    Calendar cal = Calendar.getInstance();
    Timestamp ts1 = new Timestamp(System.currentTimeMillis());
    cal.setTimeInMillis(ts1.getTime());
    cal.add(Calendar.MONTH, -1);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    Timestamp ts2 = new Timestamp(cal.getTimeInMillis());
    assertTrue(!ts1.equals(ts2) && ts1.equals(ts1));
}
 
Example 7
Project: incubator-netbeans   File: ResultsOutlineCellRenderer.java   View Source Code Vote up 6 votes
private long getMidnightTime() {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MILLISECOND, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.HOUR, 0);
    return c.getTimeInMillis();
}
 
Example 8
Project: android-project-gallery   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 返回月的第一天
 * 
 * @return
 */
public static long getFirstDayOfMonth()
{
	Calendar calendar = new GregorianCalendar();

	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);

	Log.D(TAG, "getFirstDayOfMonth: " + calendar.getTime());
	return calendar.getTimeInMillis();
}
 
Example 9
Project: spanner-jdbc   File: CloudSpannerConversionUtilTest.java   View Source Code Vote up 6 votes
@Test
public void testToSqlTime()
{
	com.google.cloud.Timestamp ts = com.google.cloud.Timestamp.parseTimestamp("2000-01-01T13:15:10.0010001Z");
	Time t1 = CloudSpannerConversionUtil.toSqlTime(ts);
	Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
	cal.set(2000, 0, 1, 13, 15, 10);
	cal.set(Calendar.MILLISECOND, 1);
	Time t2 = new Time(cal.getTimeInMillis());
	assertEquals(t2.getTime(), t1.getTime());
	assertEquals(t2, t1);
}
 
Example 10
Project: openjdk-jdk10   File: TimestampTests.java   View Source Code Vote up 6 votes
@Test
public void test50() {
    Calendar cal = Calendar.getInstance();
    Timestamp ts1 = new Timestamp(System.currentTimeMillis());
    cal.setTimeInMillis(ts1.getTime());
    cal.add(Calendar.MONTH, -1);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    Timestamp ts2 = new Timestamp(cal.getTimeInMillis());
    assertTrue(!ts1.equals(ts2) && ts1.equals(ts1));
}
 
Example 11
Project: ProyectoPacientes   File: TimeUtil.java   View Source Code Vote up 6 votes
final static Date fastDateCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day) {

        Calendar dateCal = cal;

        if (useGmtConversion) {

            if (gmtCalIfNeeded == null) {
                gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
            }

            dateCal = gmtCalIfNeeded;
        }

        synchronized (dateCal) {
            java.util.Date origCalDate = dateCal.getTime();
            try {
                dateCal.clear();
                dateCal.set(Calendar.MILLISECOND, 0);

                // why-oh-why is this different than java.util.date, in the year part, but it still keeps the silly '0' for the start month????
                dateCal.set(year, month - 1, day, 0, 0, 0);

                long dateAsMillis = dateCal.getTimeInMillis();

                return new Date(dateAsMillis);
            } finally {
                dateCal.setTime(origCalDate);
            }
        }

    }
 
Example 12
Project: NoMoreOversleeps   File: SleepEntry.java   View Source Code Vote up 6 votes
public void updateNextTriggerTime()
{
	int newEnd = this.end < this.start ? this.end + 1440 : this.end;
	long currentTime = MainDialog.now;
	Calendar calendar2 = Calendar.getInstance();
	calendar2.setTimeInMillis(currentTime);
	calendar2.set(Calendar.HOUR_OF_DAY, this.start / 60);
	calendar2.set(Calendar.MINUTE, this.start % 60);
	calendar2.set(Calendar.SECOND, 0);
	calendar2.set(Calendar.MILLISECOND, 0);
	long m = calendar2.getTimeInMillis();
	long n = m + ((newEnd - this.start) * 60000L);
	if (n < currentTime)
	{
		calendar2 = Calendar.getInstance();
		calendar2.setTimeInMillis(currentTime + 86400000L);
		calendar2.set(Calendar.HOUR_OF_DAY, this.start / 60);
		calendar2.set(Calendar.MINUTE, this.start % 60);
		calendar2.set(Calendar.SECOND, 0);
		calendar2.set(Calendar.MILLISECOND, 0);
		m = calendar2.getTimeInMillis();
		n = m + ((newEnd - this.start) * 60000L);
	}
	else if ((n - currentTime) >= 86400000L)
	{
		calendar2 = Calendar.getInstance();
		calendar2.setTimeInMillis(currentTime - 86400000L);
		calendar2.set(Calendar.HOUR_OF_DAY, this.start / 60);
		calendar2.set(Calendar.MINUTE, this.start % 60);
		calendar2.set(Calendar.SECOND, 0);
		calendar2.set(Calendar.MILLISECOND, 0);
		m = calendar2.getTimeInMillis();
		n = m + ((newEnd - this.start) * 60000L);
	}
	this.nextStartTime = m;
	this.nextEndTime = n;
}
 
Example 13
Project: flume-release-1.7.0   File: TestTimestampRoundDownUtil.java   View Source Code Vote up 6 votes
/**
 * Tests if the timestamp with the custom timezone is properly rounded down
 * to 2 hours.
 */
@Test
public void testRoundDownTimeStampHoursWithTimeZone() {
  Calendar cal = BASE_CALENDAR_WITH_CUSTOM_TIMEZONE;
  Calendar cal2 = createCalendar(2012, 5, 15, 14, 0, 0, 0, CUSTOM_TIMEZONE);

  long timeToVerify = cal2.getTimeInMillis();
  long withoutTimeZone = TimestampRoundDownUtil.roundDownTimeStampHours(
      cal.getTimeInMillis(), 2);
  long withTimeZone = TimestampRoundDownUtil.roundDownTimeStampHours(
      cal.getTimeInMillis(), 2, CUSTOM_TIMEZONE);

  assertThat(withoutTimeZone, not(equalTo(timeToVerify)));
  Assert.assertEquals(withTimeZone, timeToVerify);
}
 
Example 14
Project: fiery   File: DateTimeHelper.java   View Source Code Vote up 6 votes
public static long getTimesMorning(Long timestamp) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(timestamp * 1000);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return (int) (cal.getTimeInMillis() / 1000);
}
 
Example 15
Project: uroborosql   File: DateTimeApiPropertyMapperTest.java   View Source Code Vote up 6 votes
private long toTime(final TemporalAccessor temporalAccessor) {
	Calendar calendar = Calendar.getInstance();
	setField(calendar, Calendar.YEAR, temporalAccessor, ChronoField.YEAR, 0, 1970);
	setField(calendar, Calendar.MONTH, temporalAccessor, ChronoField.MONTH_OF_YEAR, -1, 0);
	setField(calendar, Calendar.DATE, temporalAccessor, ChronoField.DAY_OF_MONTH, 0, 1);
	setField(calendar, Calendar.HOUR_OF_DAY, temporalAccessor, ChronoField.HOUR_OF_DAY, 0, 0);
	setField(calendar, Calendar.MINUTE, temporalAccessor, ChronoField.MINUTE_OF_HOUR, 0, 0);
	setField(calendar, Calendar.SECOND, temporalAccessor, ChronoField.SECOND_OF_MINUTE, 0, 0);
	setField(calendar, Calendar.MILLISECOND, temporalAccessor, ChronoField.MILLI_OF_SECOND, 0, 0);
	return calendar.getTimeInMillis();
}
 
Example 16
Project: GitHub   File: Cookie.java   View Source Code Vote up 5 votes
/** Parse a date as specified in RFC 6265, section 5.1.1. */
private static long parseExpires(String s, int pos, int limit) {
  pos = dateCharacterOffset(s, pos, limit, false);

  int hour = -1;
  int minute = -1;
  int second = -1;
  int dayOfMonth = -1;
  int month = -1;
  int year = -1;
  Matcher matcher = TIME_PATTERN.matcher(s);

  while (pos < limit) {
    int end = dateCharacterOffset(s, pos + 1, limit, true);
    matcher.region(pos, end);

    if (hour == -1 && matcher.usePattern(TIME_PATTERN).matches()) {
      hour = Integer.parseInt(matcher.group(1));
      minute = Integer.parseInt(matcher.group(2));
      second = Integer.parseInt(matcher.group(3));
    } else if (dayOfMonth == -1 && matcher.usePattern(DAY_OF_MONTH_PATTERN).matches()) {
      dayOfMonth = Integer.parseInt(matcher.group(1));
    } else if (month == -1 && matcher.usePattern(MONTH_PATTERN).matches()) {
      String monthString = matcher.group(1).toLowerCase(Locale.US);
      month = MONTH_PATTERN.pattern().indexOf(monthString) / 4; // Sneaky! jan=1, dec=12.
    } else if (year == -1 && matcher.usePattern(YEAR_PATTERN).matches()) {
      year = Integer.parseInt(matcher.group(1));
    }

    pos = dateCharacterOffset(s, end + 1, limit, false);
  }

  // Convert two-digit years into four-digit years. 99 becomes 1999, 15 becomes 2015.
  if (year >= 70 && year <= 99) year += 1900;
  if (year >= 0 && year <= 69) year += 2000;

  // If any partial is omitted or out of range, return -1. The date is impossible. Note that leap
  // seconds are not supported by this syntax.
  if (year < 1601) throw new IllegalArgumentException();
  if (month == -1) throw new IllegalArgumentException();
  if (dayOfMonth < 1 || dayOfMonth > 31) throw new IllegalArgumentException();
  if (hour < 0 || hour > 23) throw new IllegalArgumentException();
  if (minute < 0 || minute > 59) throw new IllegalArgumentException();
  if (second < 0 || second > 59) throw new IllegalArgumentException();

  Calendar calendar = new GregorianCalendar(UTC);
  calendar.setLenient(false);
  calendar.set(Calendar.YEAR, year);
  calendar.set(Calendar.MONTH, month - 1);
  calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  calendar.set(Calendar.HOUR_OF_DAY, hour);
  calendar.set(Calendar.MINUTE, minute);
  calendar.set(Calendar.SECOND, second);
  calendar.set(Calendar.MILLISECOND, 0);
  return calendar.getTimeInMillis();
}
 
Example 17
Project: react-native-udesk   File: UdeskUtil.java   View Source Code Vote up 5 votes
public static String formatLongTypeTimeToString(Context context, long time) {
    long OFFSET_DAY = 3600 * 24;
    String timeYes = context.getString(R.string.udesk_im_time_format_yday);
    String timeQt = context.getString(R.string.udesk_im_time_format_dby);
    String timeDate = "yyyy/MM/dd";
    Calendar calendar = Calendar.getInstance();
    StringBuilder build = new StringBuilder();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

    // 解析需要转化时间
    calendar.setTimeInMillis(time);
    int year = calendar.get(Calendar.YEAR);
    int day = calendar.get(Calendar.DAY_OF_YEAR);

    // 拼接 转化结果
    build.append(" ").append(sdf.format(calendar.getTime()));// 先添加

    // 先解析当前时间。取出当前年,日 等信息
    calendar.setTimeInMillis(System.currentTimeMillis());
    int nowYear = calendar.get(Calendar.YEAR);
    int nowDay = calendar.get(Calendar.DAY_OF_YEAR);

    if (year != nowYear) {// 不是一年内
        calendar.set(Calendar.HOUR_OF_DAY, 0); // 凌晨1点
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        if ((calendar.getTimeInMillis() - time) <= OFFSET_DAY) {// 昨天
            return timeYes;
        } else if ((calendar.getTimeInMillis() - time) <= (OFFSET_DAY << 2)) {// 前天
            // 。这里不用判断是否大于OFFSET_DAY
            return timeQt;
        } else {
            sdf.applyLocalizedPattern(timeDate);
            return sdf.format(time);
        }

    } else if (day == nowDay) {// 这里是一年内的当天
        // 当天的话 就不用管了
    } else {// 一年内
        int dayOffset = (nowDay - day);// nowDay要大一些
        if (dayOffset == 0) {
            // 同一天不用 添加日期判断
        } else if (dayOffset == 1) {// 1表示差一天,即昨天
            return timeYes;
        } else if (dayOffset == 2) {// 1表示差两天,即前天
            return timeQt;
        } else {
            timeDate = "MM月dd日";
            sdf.applyLocalizedPattern(timeDate);
            return sdf.format(time);
        }
    }

    return build.toString();
}
 
Example 18
Project: lams   File: DefaultAccessLogReceiver.java   View Source Code Vote up 5 votes
/**
 * processes all queued log messages
 */
@Override
public void run() {
    if (!stateUpdater.compareAndSet(this, 1, 2)) {
        return;
    }
    if (forceLogRotation) {
        doRotate();
    } else if(initialRun && defaultLogFile.exists()) {
        //if there is an existing log file check if it should be rotated
        long lm = defaultLogFile.lastModified();
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(changeOverPoint);
        c.add(Calendar.DATE, -1);
        if(lm <= c.getTimeInMillis()) {
            doRotate();
        }
    }
    initialRun = false;
    List<String> messages = new ArrayList<>();
    String msg = null;
    //only grab at most 1000 messages at a time
    for (int i = 0; i < 1000; ++i) {
        msg = pendingMessages.poll();
        if (msg == null) {
            break;
        }
        messages.add(msg);
    }
    try {
        if (!messages.isEmpty()) {
            writeMessage(messages);
        }
    } finally {
        stateUpdater.set(this, 0);
        //check to see if there is still more messages
        //if so then run this again
        if (!pendingMessages.isEmpty() || forceLogRotation) {
            if (stateUpdater.compareAndSet(this, 0, 1)) {
                logWriteExecutor.execute(this);
            }
        } else if(closed) {
            try {
                writer.flush();
                writer.close();
                writer = null;
            } catch (IOException e) {
                UndertowLogger.ROOT_LOGGER.errorWritingAccessLog(e);
            }
        }
    }
}
 
Example 19
Project: the-vigilantes   File: TimeUtil.java   View Source Code Vote up 5 votes
final static Timestamp fastTimestampCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day, int hour,
        int minute, int seconds, int secondsPart) {

    synchronized (cal) {
        java.util.Date origCalDate = cal.getTime();
        try {
            cal.clear();

            // why-oh-why is this different than java.util.date, in the year part, but it still keeps the silly '0' for the start month????
            cal.set(year, month - 1, day, hour, minute, seconds);

            int offsetDiff = 0;

            if (useGmtConversion) {
                int fromOffset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);

                if (gmtCalIfNeeded == null) {
                    gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                }
                gmtCalIfNeeded.clear();

                gmtCalIfNeeded.setTimeInMillis(cal.getTimeInMillis());

                int toOffset = gmtCalIfNeeded.get(Calendar.ZONE_OFFSET) + gmtCalIfNeeded.get(Calendar.DST_OFFSET);
                offsetDiff = fromOffset - toOffset;
            }

            if (secondsPart != 0) {
                cal.set(Calendar.MILLISECOND, secondsPart / 1000000);
            }

            long tsAsMillis = cal.getTimeInMillis();

            Timestamp ts = new Timestamp(tsAsMillis + offsetDiff);

            ts.setNanos(secondsPart);

            return ts;
        } finally {
            cal.setTime(origCalDate);
        }
    }
}
 
Example 20
Project: Android-Week-View   File: WeekView.java   View Source Code Vote up 3 votes
/**
 * Checks if time1 occurs after (or at the same time) time2.
 * @param time1 The time to check.
 * @param time2 The time to check against.
 * @return true if time1 and time2 are equal or if time1 is after time2. Otherwise false.
 */
private boolean isTimeAfterOrEquals(Calendar time1, Calendar time2) {
    return !(time1 == null || time2 == null) && time1.getTimeInMillis() >= time2.getTimeInMillis();
}