Java Code Examples for java.util.Calendar#HOUR
The following examples show how to use
java.util.Calendar#HOUR .
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: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Tests DateUtils.round()-method with Calendar.HOUR * Includes rounding the extremes of one hour * Includes rounding to January 1 * * @throws Exception * @since 3.0 */ @Test public void testRoundHour() throws Exception { final int calendarField = Calendar.HOUR; Date roundedUpDate, roundedDownDate, lastRoundedDownDate; Date minDate, maxDate; roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000"); roundedDownDate = targetHourDate; lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999"); baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); //round to January 1 minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000"); maxDate = dateTimeParser.parse("January 1, 2008 0:29:59.999"); roundToJanuaryFirst(minDate, maxDate, calendarField); }
Example 2
Source File: Utilities.java From sakai with Educational Community License v2.0 | 6 votes |
/** * Calculate the time according to the input parameters. * * @param date * a Date object. * @param time * an int value. * @param dateType * a string value. * @return a converted Date object according to the input parameters. */ public static Date subTractTimeToDate(Date date, int time, String dateType) { if (time == 0) return date; int type = -1; if (dateType.equals(MINUTES)) type = Calendar.MINUTE; else if (dateType.equals(HOURS)) type = Calendar.HOUR; else if (dateType.equals(START_NOW)){ Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); }else {// days{ time = 24 * time; // convert to hours type = Calendar.HOUR; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(type, -1 * time); return calendar.getTime(); }
Example 3
Source File: TimePickerClockPresenter.java From material-components-android with Apache License 2.0 | 6 votes |
@Override public void onActionUp(float rotation, boolean moveInEventStream) { broadcasting = true; if (time.selection == Calendar.HOUR) { // Current rotation might be half way to an exact hour position. // Snap to the closest hour before animating to the position the minute selection is on. timePickerView.setHandRotation(hourRotation, /* animate= */ false); // Automatically move to minutes once the user finishes choosing the hour. setSelection(MINUTE, /* animate= */ true); } else { int rotationInt = Math.round(rotation); if (!moveInEventStream) { // snap minute to 5 minute increment if there was only a touch down/up. int newRotation = (rotationInt + 15) / 30; time.setMinute(newRotation * 5); minuteRotation = time.minute * DEGREES_PER_MINUTE; } timePickerView.setHandRotation(minuteRotation, /* animate= */ moveInEventStream); } broadcasting = false; updateTime(); }
Example 4
Source File: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Tests DateUtils.round()-method with Calendar.HOUR * Includes rounding the extremes of one hour * Includes rounding to January 1 * * @throws Exception * @since 3.0 */ @Test public void testRoundHour() throws Exception { final int calendarField = Calendar.HOUR; Date roundedUpDate, roundedDownDate, lastRoundedDownDate; Date minDate, maxDate; roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000"); roundedDownDate = targetHourDate; lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999"); baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); //round to January 1 minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000"); maxDate = dateTimeParser.parse("January 1, 2008 0:29:59.999"); roundToJanuaryFirst(minDate, maxDate, calendarField); }
Example 5
Source File: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Tests DateUtils.round()-method with Calendar.HOUR * Includes rounding the extremes of one hour * Includes rounding to January 1 * * @throws Exception * @since 3.0 */ public void testRoundHour() throws Exception { final int calendarField = Calendar.HOUR; Date roundedUpDate, roundedDownDate, lastRoundedDownDate; Date minDate, maxDate; roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000"); roundedDownDate = targetHourDate; lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999"); baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); //round to January 1 minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000"); maxDate = dateTimeParser.parse("January 1, 2008 0:29:59.999"); roundToJanuaryFirst(minDate, maxDate, calendarField); }
Example 6
Source File: ArgumentResolver.java From EasyNLU with Apache License 2.0 | 6 votes |
int getDefault(int field, Calendar now){ switch (field){ case Calendar.MONTH: return Calendar.JANUARY; case Calendar.DAY_OF_WEEK: return now.getFirstDayOfWeek(); case Calendar.DATE: return 1; case Calendar.HOUR_OF_DAY: return 12; case Calendar.HOUR: case Calendar.MINUTE: case Calendar.SECOND: return 0; default: return 0; } }
Example 7
Source File: AbstractCalendarValidator.java From data-binding-validator with Apache License 2.0 | 5 votes |
/** * <p>Compares a calendar time value to another, indicating whether it is * equal, less then or more than at a specified level.</p> * * @param value The Calendar value. * @param compare The <code>Calendar</code> to check the value against. * @param field The field <i>level</i> to compare to - e.g. specifying * <code>Calendar.MINUTE</code> will compare the hours and minutes * portions of the calendar. * @return Zero if the first value is equal to the second, -1 * if it is less than the second or +1 if it is greater than the second. */ protected int compareTime(Calendar value, Calendar compare, int field) { int result = 0; // Compare Hour result = calculateCompareResult(value, compare, Calendar.HOUR_OF_DAY); if (result != 0 || (field == Calendar.HOUR || field == Calendar.HOUR_OF_DAY)) { return result; } // Compare Minute result = calculateCompareResult(value, compare, Calendar.MINUTE); if (result != 0 || field == Calendar.MINUTE) { return result; } // Compare Second result = calculateCompareResult(value, compare, Calendar.SECOND); if (result != 0 || field == Calendar.SECOND) { return result; } // Compare Milliseconds if (field == Calendar.MILLISECOND) { return calculateCompareResult(value, compare, Calendar.MILLISECOND); } throw new IllegalArgumentException("Invalid field: " + field); }
Example 8
Source File: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Test DateUtils.truncate()-method with Calendar.HOUR * * @throws Exception * @since 3.0 */ @Test public void testTruncateHour() throws Exception { final int calendarField = Calendar.HOUR; Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:59:59.999"); baseTruncateTest(targetHourDate, lastTruncateDate, calendarField); }
Example 9
Source File: DateUtils.java From roncoo-pay with Apache License 2.0 | 5 votes |
/** * 根据单位字段比较两个日期 * * @param date * 日期1 * @param otherDate * 日期2 * @param withUnit * 单位字段,从Calendar field取值 * @return 等于返回0值, 大于返回大于0的值 小于返回小于0的值 */ public static int compareDate(Date date, Date otherDate, int withUnit) { Calendar dateCal = Calendar.getInstance(); dateCal.setTime(date); Calendar otherDateCal = Calendar.getInstance(); otherDateCal.setTime(otherDate); switch (withUnit) { case Calendar.YEAR: dateCal.clear(Calendar.MONTH); otherDateCal.clear(Calendar.MONTH); case Calendar.MONTH: dateCal.set(Calendar.DATE, 1); otherDateCal.set(Calendar.DATE, 1); case Calendar.DATE: dateCal.set(Calendar.HOUR_OF_DAY, 0); otherDateCal.set(Calendar.HOUR_OF_DAY, 0); case Calendar.HOUR: dateCal.clear(Calendar.MINUTE); otherDateCal.clear(Calendar.MINUTE); case Calendar.MINUTE: dateCal.clear(Calendar.SECOND); otherDateCal.clear(Calendar.SECOND); case Calendar.SECOND: dateCal.clear(Calendar.MILLISECOND); otherDateCal.clear(Calendar.MILLISECOND); case Calendar.MILLISECOND: break; default: throw new IllegalArgumentException("withUnit 单位字段 " + withUnit + " 不合法!!"); } return dateCal.compareTo(otherDateCal); }
Example 10
Source File: Dates.java From howsun-javaee-framework with Apache License 2.0 | 5 votes |
/** * 获取某时间的开始毫秒数 * * @param calendar 时间 * @param calendarField 类型,只支持年开始的秒数,月开始的秒数,日开始的秒数,小时开始的秒数,分钟开始的秒数 * @return 毫秒数 */ public static long getSecoendOfBegin(Calendar calendar, int calendarField){ Calendar cal = calendar == null ? Calendar.getInstance() : (Calendar)calendar.clone(); cal.set(Calendar.AM_PM, 0); switch (calendarField) { case Calendar.YEAR: cal.set(cal.get(Calendar.YEAR), 0, 1, 0, 0, 0); break; case Calendar.MONTH: cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0); break; case Calendar.DATE: cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0); break; case Calendar.HOUR: cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.HOUR), 0, 0); break; case Calendar.MINUTE: cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), 0); break; default: throw new RuntimeException("不支持的时间模式。"); } cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
Example 11
Source File: PartialDate.java From dialogflow-java-client with Apache License 2.0 | 5 votes |
public Integer get(final int field) { if (field == Calendar.YEAR) { if (!unspecifiedFields.contains(Calendar.YEAR)) { return c.get(field); } return UNSPECIFIED_VALUE; } else if (field == Calendar.MONTH) { if (!unspecifiedFields.contains(Calendar.MONTH)) { return c.get(field); } return UNSPECIFIED_VALUE; } else if (field >= Calendar.WEEK_OF_YEAR && field <= Calendar.DAY_OF_WEEK_IN_MONTH) { if (!unspecifiedFields.contains(Calendar.DATE)) { return c.get(field); } return UNSPECIFIED_VALUE; } else if (field >= Calendar.HOUR && field <= Calendar.HOUR_OF_DAY) { if (!unspecifiedFields.contains(Calendar.HOUR_OF_DAY)) { return c.get(field); } return UNSPECIFIED_VALUE; } else if (field == Calendar.MINUTE) { if (!unspecifiedFields.contains(Calendar.MINUTE)) { return c.get(Calendar.MINUTE); } return UNSPECIFIED_VALUE; } else { return c.get(field); } //return UNSPECIFIED_VALUE; }
Example 12
Source File: DateDifference.java From ApprovalTests.Java with Apache License 2.0 | 5 votes |
public static int convertUnitString(String unit) { int result = 0; if (MILLISECONDS.equalsIgnoreCase(unit)) { result = Calendar.MILLISECOND; } else if (SECONDS.equalsIgnoreCase(unit)) { result = Calendar.SECOND; } else if (MINUTES.equalsIgnoreCase(unit)) { result = Calendar.MINUTE; } else if (HOURS.equalsIgnoreCase(unit)) { result = Calendar.HOUR; } else if (DAYS.equalsIgnoreCase(unit)) { result = Calendar.DATE; } else if (WEEKS.equalsIgnoreCase(unit)) { result = Calendar.WEEK_OF_YEAR; } else if (MONTHS.equalsIgnoreCase(unit)) { result = Calendar.MONTH; } else if (YEARS.equalsIgnoreCase(unit)) { result = Calendar.YEAR; } return result; }
Example 13
Source File: CDateTime.java From nebula with Eclipse Public License 2.0 | 5 votes |
int getCalendarField(Field field) { int cf = field.getCalendarField(); if (cf < 0) { if (field.toString().indexOf("hour 1") > -1) { //$NON-NLS-1$ cf = Calendar.HOUR; } else if (field.toString().contains("zone")) { //$NON-NLS-1$ cf = Calendar.ZONE_OFFSET; } else if (field.toString().contains("hour of day 1")) { //$NON-NLS-1$ cf = Calendar.HOUR_OF_DAY; } } return cf; }
Example 14
Source File: Times.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
/** This used to be 'deltim' in MMS. */ public static double deltaHours(int calUnit, int increments) { if (calUnit == Calendar.DATE) { return 24 * increments; } else if (calUnit == Calendar.HOUR) { return increments; } else if (calUnit == Calendar.MINUTE) { return increments / 60; } else if (calUnit == Calendar.SECOND) { return increments / 3600; } return -1; }
Example 15
Source File: AnalogTimePicker.java From nebula with Eclipse Public License 2.0 | 4 votes |
public int[] getFields() { return new int[] { Calendar.HOUR_OF_DAY, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND, Calendar.AM_PM }; }
Example 16
Source File: AnalogTimePicker.java From nebula with Eclipse Public License 2.0 | 4 votes |
public void setFields(int[] calendarFields) { is24Hour = false; hourHand = false; minHand = false; secHand = false; am_pm = false; for (int field : calendarFields) { if (field == Calendar.HOUR_OF_DAY) { is24Hour = true; } else if (field == Calendar.HOUR) { hourHand = true; } else if (field == Calendar.MINUTE) { minHand = true; } else if (field == Calendar.SECOND) { secHand = true; } else if (field == Calendar.AM_PM) { am_pm = true; } } if ((cdt.style & CDT.CLOCK_12_HOUR) != 0) { is24Hour = false; hourHand = true; am_pm = true; } else if ((cdt.style & CDT.CLOCK_24_HOUR) != 0) { is24Hour = true; } if (is24Hour) { hourHand = true; am_pm = false; } timeAmPm.setVisible(am_pm); boolean sepOK = false; pattern = ""; //$NON-NLS-1$ String cdtPattern = cdt.getPattern(); for (int i = 0; i < cdtPattern.length(); i++) { char c = cdtPattern.charAt(i); if ("Hhmsa".indexOf(c) > -1) { //$NON-NLS-1$ pattern += c; sepOK = true; } else { if (sepOK && ":., ".indexOf(c) > -1) { //$NON-NLS-1$ pattern += c; } sepOK = false; } } if (digitalClock != null) { digitalClock.setPattern(pattern); } updateLabels(); }
Example 17
Source File: DateAxis.java From constellation with Apache License 2.0 | 4 votes |
/** * Makes dates even, in the sense of that years always begin in January, * months always begin on the 1st and days always at midnight. * * @param dates The list of dates. * @return The new list of dates. */ private List<Date> makeDatesEven(List<Date> dates, Calendar calendar) { // if the dates contain more dates than just the lower and upper bounds, make the dates in between even. if (dates.size() > 2) { List<Date> evenDates = new ArrayList<>(); // for each interval, modify the date slightly by a few millis, to make sure they are different days. // this is because Axis stores each value and won't update the tick labels, if the value is already known. // this happens if you display days and then add a date many years in the future the tick label will still be displayed as day. for (int i = 0; i < dates.size(); i++) { calendar.setTime(dates.get(i)); switch (actualInterval.interval) { case Calendar.YEAR: // if it's not the first or last date (lower and upper bound), make the year begin with first month and let the months begin with first day. if (i != 0 && i != dates.size() - 1) { calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DATE, 1); } calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 6); break; case Calendar.MONTH: // if it's not the first or last date (lower and upper bound), make the months begin with first day. if (i != 0 && i != dates.size() - 1) { calendar.set(Calendar.DATE, 1); } calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 5); break; case Calendar.WEEK_OF_YEAR: // make weeks begin with first day of week? calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 4); break; case Calendar.DATE: calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 3); break; case Calendar.HOUR: if (i != 0 && i != dates.size() - 1) { calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); } calendar.set(Calendar.MILLISECOND, 2); break; case Calendar.MINUTE: if (i != 0 && i != dates.size() - 1) { calendar.set(Calendar.SECOND, 0); } calendar.set(Calendar.MILLISECOND, 1); break; case Calendar.SECOND: calendar.set(Calendar.MILLISECOND, 0); break; default: break; } evenDates.add(calendar.getTime()); } return evenDates; } else { return dates; } }
Example 18
Source File: DateUtils.java From dubbo-spring-boot-learning with MIT License | 4 votes |
/** * 获取日期绝对差 * * @param d1 日期1 * @param d2 日期2 * @param field Calendar字段,YEAR、MONTH等 * @return 日期差 */ public static long diff(Date d1, Date d2, int field) { long ms = d2.getTime() - d1.getTime(); //如果d2在d1前面 if (getCalendar(d2).before(getCalendar(d1))) { //记录后一个日期 Date d0 = d2; d2 = d1; d1 = d0; } long res = 0; switch (field) { case Calendar.YEAR: Calendar c = getCalendar(d2); //将年设置成相同的 c.set(Calendar.YEAR, year(d1)); //然后比较日期前后关系,如果d0在d1前面,年份-1 res = year(d2) - year(d1) - (c.before(getCalendar(d1)) ? 1 : 0); break; case Calendar.MONTH: int years = year(d2) - year(d1); Calendar c1 = getCalendar(d2); //将年设置成相同的 c1.set(Calendar.YEAR, year(d1)); //然后比较日期前后关系,如果d0在d1前面,月份-1 c1.set(Calendar.MONTH, month(d1) - 1); res = (month(d2) >= month(d1) ? (month(d2) - month(d1)) : (month(d2) + 12 - month(d1)) % 12) + years * 12 - (c1.before(getCalendar(d1)) ? 1 : 0); break; case Calendar.WEEK_OF_YEAR: res = ms / (7 * 24 * 60 * 60 * 1000); break; case Calendar.DATE: res = ms / (24 * 60 * 60 * 1000); break; case Calendar.HOUR: res = ms / (60 * 60 * 1000); break; case Calendar.MINUTE: res = ms / (60 * 1000); break; case Calendar.SECOND: res = ms / (1000); break; case Calendar.MILLISECOND: res = ms; break; default: break; } return res; }
Example 19
Source File: TimeManagerTest.java From drftpd with GNU General Public License v2.0 | 4 votes |
public void resetHour(Date d) { _lastReset = Calendar.HOUR; }
Example 20
Source File: DateUtilsRoundingTest.java From astor with GNU General Public License v2.0 | 2 votes |
/** * Test DateUtils.truncate()-method with Calendar.HOUR * * @throws Exception * @since 3.0 */ public void testTruncateHour() throws Exception { final int calendarField = Calendar.HOUR; Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:59:59.999"); baseTruncateTest(targetHourDate, lastTruncateDate, calendarField); }