本文整理匯總了Java中java.util.Calendar.getTimeInMillis方法的典型用法代碼示例。如果您正苦於以下問題:Java Calendar.getTimeInMillis方法的具體用法?Java Calendar.getTimeInMillis怎麽用?Java Calendar.getTimeInMillis使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.Calendar
的用法示例。
在下文中一共展示了Calendar.getTimeInMillis方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getDaysBetween
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 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;
}
示例2: getDayBetween
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 間隔天數
*
* @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));
}
示例3: getMinusDaysBetweenTwoDate
import java.util.Calendar; //導入方法依賴的package包/類
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);
}
示例4: fastDateCreate
import java.util.Calendar; //導入方法依賴的package包/類
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);
}
}
}
示例5: setTimeToZeroInCurrentTimeZone
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 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();
}
示例6: test50
import java.util.Calendar; //導入方法依賴的package包/類
@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));
}
示例7: getMidnightTime
import java.util.Calendar; //導入方法依賴的package包/類
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();
}
示例8: getFirstDayOfMonth
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 返回月的第一天
*
* @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();
}
示例9: testToSqlTime
import java.util.Calendar; //導入方法依賴的package包/類
@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);
}
示例10: updateNextTriggerTime
import java.util.Calendar; //導入方法依賴的package包/類
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;
}
示例11: testRoundDownTimeStampHoursWithTimeZone
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 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);
}
示例12: getTimesMorning
import java.util.Calendar; //導入方法依賴的package包/類
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);
}
示例13: toTime
import java.util.Calendar; //導入方法依賴的package包/類
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();
}
示例14: parseExpires
import java.util.Calendar; //導入方法依賴的package包/類
/** 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();
}
示例15: formatLongTypeTimeToString
import java.util.Calendar; //導入方法依賴的package包/類
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();
}