Java Code Examples for java.util.GregorianCalendar#get()
The following examples show how to use
java.util.GregorianCalendar#get() .
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: JTranscTime.java From jtransc with Apache License 2.0 | 6 votes |
static public void fillParts(long time, int[] parts) { GregorianCalendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(time); parts[0] = calendar.get(GregorianCalendar.YEAR); parts[1] = calendar.get(GregorianCalendar.MONTH); parts[2] = calendar.get(GregorianCalendar.DAY_OF_MONTH); parts[3] = calendar.get(GregorianCalendar.DAY_OF_WEEK); parts[4] = calendar.get(GregorianCalendar.DAY_OF_YEAR); parts[5] = calendar.get(GregorianCalendar.HOUR); parts[6] = calendar.get(GregorianCalendar.MINUTE); parts[7] = calendar.get(GregorianCalendar.SECOND); parts[8] = calendar.get(GregorianCalendar.MILLISECOND); parts[9] = 0; //for (int n = 0; n < parts.length; n++) parts[n] = 0; }
Example 2
Source File: GregorianCalendarTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * java.util.GregorianCalendar#clone() */ public void test_clone() { // Regression for HARMONY-498 GregorianCalendar gCalend = new GregorianCalendar(); gCalend.set(Calendar.MILLISECOND, 0); int dayOfMonth = gCalend.get(Calendar.DAY_OF_MONTH); // create clone object and change date GregorianCalendar gCalendClone = (GregorianCalendar) gCalend.clone(); gCalendClone.add(Calendar.DATE, 1); assertEquals("Before", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH)); gCalend.set(Calendar.MILLISECOND, 0);//changes nothing assertEquals("After", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH)); }
Example 3
Source File: BirthUtils.java From flash-waimai with MIT License | 6 votes |
public static int getAge(String cardNo, Date date) { String birthday = cardNo.substring(6, 14); Date birthdate = null; try { birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday); } catch (ParseException e) { e.printStackTrace(); } GregorianCalendar currentDay = new GregorianCalendar(); currentDay.setTime(birthdate); int birYear = currentDay.get(Calendar.YEAR); // 获取年龄 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy"); String thisYear = simpleDateFormat.format(date); int age = Integer.parseInt(thisYear) - birYear; return age; }
Example 4
Source File: DayEditor.java From nebula with Eclipse Public License 2.0 | 6 votes |
private void computeEventRowsForDate(Date date) { GregorianCalendar targetDate = new GregorianCalendar(); targetDate.setTime(date); GregorianCalendar target = new GregorianCalendar(); target.setTime(model.getStartDate()); EventLayoutComputer dayModel = new EventLayoutComputer(model.getNumberOfDivisionsInHour()); for (int dayOffset = 0; dayOffset < model.getNumberOfDays(); ++dayOffset) { if (target.get(Calendar.DATE) == targetDate.get(Calendar.DATE) && target.get(Calendar.MONTH) == targetDate.get(Calendar.MONTH) && target.get(Calendar.YEAR) == targetDate.get(Calendar.YEAR)) { computeEventLayout(dayModel, dayOffset); break; } target.add(Calendar.DATE, 1); } }
Example 5
Source File: AgnUtils.java From openemm with GNU Affero General Public License v3.0 | 5 votes |
public static List<Integer> getCalendarYearList(int startYear) { List<Integer> yearList = new ArrayList<>(); GregorianCalendar calendar = new GregorianCalendar(); int currentYear = calendar.get(Calendar.YEAR); for (int year = currentYear + 1; year >= startYear; year--) { yearList.add(year); } return yearList; }
Example 6
Source File: CalendarRegression.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void Test4073929() { GregorianCalendar foo1 = new GregorianCalendar(1997, 8, 27); foo1.add(DAY_OF_MONTH, +1); int testyear = foo1.get(YEAR); int testmonth = foo1.get(MONTH); int testday = foo1.get(DAY_OF_MONTH); if (testyear != 1997 || testmonth != 8 || testday != 28) { errln("Fail: Calendar not initialized"); } }
Example 7
Source File: TCalendar.java From jexer with MIT License | 5 votes |
/** * Handle mouse down clicks. * * @param mouse mouse button down event */ @Override public void onMouseDown(final TMouseEvent mouse) { if ((mouseOnLeftArrow(mouse)) && (mouse.isMouse1())) { displayCalendar.add(Calendar.MONTH, -1); } else if ((mouseOnRightArrow(mouse)) && (mouse.isMouse1())) { displayCalendar.add(Calendar.MONTH, 1); } else if (mouse.isMouse1()) { // Find the day this might correspond to, and set it. int index = (mouse.getY() - 2) * 7 + (mouse.getX() / 4) + 1; // System.err.println("index: " + index); int lastDayNumber = displayCalendar.getActualMaximum( Calendar.DAY_OF_MONTH); GregorianCalendar firstOfMonth = new GregorianCalendar(); firstOfMonth.setTimeInMillis(displayCalendar.getTimeInMillis()); firstOfMonth.set(Calendar.DAY_OF_MONTH, 1); int dayOf1st = firstOfMonth.get(Calendar.DAY_OF_WEEK) - 1; // System.err.println("dayOf1st: " + dayOf1st); int day = index - dayOf1st; // System.err.println("day: " + day); if ((day < 1) || (day > lastDayNumber)) { return; } calendar.setTimeInMillis(displayCalendar.getTimeInMillis()); calendar.set(Calendar.DAY_OF_MONTH, day); } }
Example 8
Source File: CommuterTest.java From log-synth with Apache License 2.0 | 5 votes |
@Test public void testToWork() throws ParseException { Random rand = new Random(); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeZone(TimeZone.getTimeZone("US/Central")); Commuter c = new Commuter(); double t = df.parse("2015-09-23 23:15:06 CDT").getTime() / 1000.0; int[] counts = new int[24]; for (int i = 0; i < 100000; ) { double bump = -Math.log(1 - rand.nextDouble()); t = c.search(true, t, bump); if (!Commuter.isWeekend(t)) { cal.setTimeInMillis((long) (t * 1000.0)); counts[cal.get(GregorianCalendar.HOUR_OF_DAY)]++; i++; } } for (int i = 0; i < counts.length; i++) { int count = counts[i]; if (i != 7 && i != 8) { assertEquals(100e3 * 2 / 64, count, 200); } else { assertEquals(100e3 * 10 / 64, count, 400); } } }
Example 9
Source File: DayForecastFilter.java From privacy-friendly-weather with GNU General Public License v3.0 | 5 votes |
public static List<Forecast> filter(List<Forecast> list, int numberOfDays) { List<Forecast> result = new ArrayList<Forecast>(); GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); int currentDay = cal.get(Calendar.DAY_OF_WEEK); for (Forecast forecast: list) { cal.setTime(forecast.getForecastTime()); if (currentDay == cal.get(Calendar.DAY_OF_WEEK)) { continue; } if (cal.get(Calendar.HOUR_OF_DAY) == 15 && result.size() < numberOfDays) { result.add(forecast); } if (result.size() == numberOfDays) { break; } } //for a 5 day forecast, there might be no forecast for 3pm on the last day, therefore take the latest forecast for that day if (result.size() < numberOfDays) { result.add(list.get(list.size()-1)); } return result; }
Example 10
Source File: Schedule.java From al-muazzin with GNU Lesser General Public License v2.1 | 5 votes |
public static Schedule today(Context context) { GregorianCalendar now = new GregorianCalendar(); if (today == null) { today = new Schedule(context, now); } else { GregorianCalendar fajr = today.getTimes()[CONSTANT.FAJR]; if (fajr.get(Calendar.YEAR) != now.get(Calendar.YEAR) || fajr.get(Calendar.MONTH) != now.get(Calendar.MONTH) || fajr.get(Calendar.DAY_OF_MONTH) != now.get(Calendar.DAY_OF_MONTH)) { today = new Schedule(context, now); } } return today; }
Example 11
Source File: bug4372743.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private void check(GregorianCalendar gc, int index) { if (gc.get(ERA) != data[index][ERA]) { errln("Invalid era :" + gc.get(ERA) + ", expected :" + data[index][ERA]); } if (gc.get(YEAR) != data[index][YEAR]) { errln("Invalid year :" + gc.get(YEAR) + ", expected :" + data[index][YEAR]); } if (gc.get(MONTH) != data[index][MONTH]) { errln("Invalid month :" + gc.get(MONTH) + ", expected :" + data[index][MONTH]); } }
Example 12
Source File: Date.java From marine-api with GNU Lesser General Public License v3.0 | 5 votes |
/** * Creates a new instance of {@code Date} using the current date. */ public Date() { GregorianCalendar c = new GregorianCalendar(); this.year = c.get(Calendar.YEAR); this.month = c.get(Calendar.MONTH) + 1; this.day = c.get(Calendar.DAY_OF_MONTH); }
Example 13
Source File: DateUtils.java From jeewx with Apache License 2.0 | 4 votes |
public static int getYear(){ GregorianCalendar calendar=new GregorianCalendar(); calendar.setTime(getDate()); return calendar.get(Calendar.YEAR); }
Example 14
Source File: HistogramTracker.java From EasyMPermission with MIT License | 4 votes |
private void printReport(int interval, long[] bars) { StringBuilder sb = new StringBuilder(); if (category != null) sb.append(category).append(" "); sb.append("["); GregorianCalendar gc = new GregorianCalendar(); gc.setTimeInMillis(interval * REPORT_WINDOW); int hour = gc.get(Calendar.HOUR_OF_DAY); int minute = gc.get(Calendar.MINUTE); if (hour < 10) sb.append('0'); sb.append(hour).append(":"); if (minute < 10) sb.append('0'); sb.append(minute).append("] {"); long sum = bars[RANGES.length]; int count = 0; int lastZeroPos = sb.length(); for (int i = 0; i < RANGES.length; i++) { sum += bars[i]; sb.append(bars[i]); if (bars[i] != 0) lastZeroPos = sb.length(); sb.append(" "); count++; if (count == 3) sb.append("-- "); if (count == 9) sb.append("-- "); } if (sum == 0) return; sb.setLength(lastZeroPos); double millis = bars[RANGES.length + 1] / 1000000.0; long over = bars[RANGES.length]; if (over > 0L) { sb.append(" -- ").append(bars[RANGES.length]); } sb.append("} total calls: ").append(sum).append(" total time (millis): ").append((int)(millis + 0.5)); if (out == null) ProblemReporter.info(sb.toString(), null); else out.println(sb.toString()); }
Example 15
Source File: IDCardUtil.java From anyline with Apache License 2.0 | 4 votes |
/** * 验证15位身份证的合法性,该方法验证不准确,最好是将15转为18位后再判断,该类中已提供。 * * @param idcard idcard * @return return */ public static boolean validate15(String idcard) { // 非15位为假 if (idcard.length() != 15) { return false; } // 是否全都为数字 if (isDigital(idcard)) { String provinceid = idcard.substring(0, 2); String birthday = idcard.substring(6, 12); int year = Integer.parseInt(idcard.substring(6, 8)); int month = Integer.parseInt(idcard.substring(8, 10)); int day = Integer.parseInt(idcard.substring(10, 12)); // 判断是否为合法的省份 boolean flag = false; for (String id : CITY_CODE) { if (id.equals(provinceid)) { flag = true; break; } } if (!flag) { return false; } // 该身份证生出日期在当前日期之后时为假 Date birthdate = null; try { birthdate = new SimpleDateFormat("yyMMdd").parse(birthday); } catch (ParseException e) { e.printStackTrace(); } if (birthdate == null || new Date().before(birthdate)) { return false; } // 判断是否为合法的年份 GregorianCalendar curDay = new GregorianCalendar(); int curYear = curDay.get(Calendar.YEAR); int year2bit = Integer.parseInt(String.valueOf(curYear) .substring(2)); // 判断该年份的两位表示法,小于50的和大于当前年份的,为假 if ((year < 50 && year > year2bit)) { return false; } // 判断是否为合法的月份 if (month < 1 || month > 12) { return false; } // 判断是否为合法的日期 boolean mflag = false; curDay.setTime(birthdate); // 将该身份证的出生日期赋于对象curDay switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: mflag = (day >= 1 && day <= 31); break; case 2: // 公历的2月非闰年有28天,闰年的2月是29天。 if (curDay.isLeapYear(curDay.get(Calendar.YEAR))) { mflag = (day >= 1 && day <= 29); } else { mflag = (day >= 1 && day <= 28); } break; case 4: case 6: case 9: case 11: mflag = (day >= 1 && day <= 30); break; } if (!mflag) { return false; } } else { return false; } return true; }
Example 16
Source File: CalendarPage.java From spring-boot with Apache License 2.0 | 4 votes |
/** Compute which days to put where, in the Cal panel */ public void print(int mm, int yy) { /** The number of days to leave blank at the start of this month */ int leadGap = 0; System.out.print(months[mm]); // print month and year System.out.print(" "); System.out.print(yy); System.out.println(); if (mm < 0 || mm > 11) throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11"); GregorianCalendar calendar = new GregorianCalendar(yy, mm, 1); System.out.println("Su Mo Tu We Th Fr Sa"); // Compute how much to leave before the first. // get(DAY_OF_WEEK) returns 0 for Sunday, which is just right. leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1; int daysInMonth = CalUtils.getDaysInMonth(mm); if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && mm == 1) ++daysInMonth; // Blank out the labels before 1st day of month for (int i = 0; i < leadGap; i++) { System.out.print(" "); } // Fill in numbers for the day of month. for (int i = 1; i <= daysInMonth; i++) { // This "if" statement is simpler than fiddling with NumberFormat if (i<=9) System.out.print(' '); System.out.print(i); if ((leadGap + i) % 7 == 0) // wrap if end of line. System.out.println(); else System.out.print(' '); } System.out.println(); }
Example 17
Source File: DataUtils.java From jeewx with Apache License 2.0 | 4 votes |
public static int getYear(){ GregorianCalendar calendar=new GregorianCalendar(); calendar.setTime(getDate()); return calendar.get(Calendar.YEAR); }
Example 18
Source File: TestIsoChronoImpl.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider = "RangeVersusCalendar") public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) { GregorianCalendar cal = new GregorianCalendar(); assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type"); LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate); for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) { for (int minDays = 1; minDays <= 7; minDays++) { WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays); cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1); cal.setMinimalDaysInFirstWeek(minDays); cal.setTimeZone(TimeZone.getTimeZone("GMT+00")); cal.set(Calendar.YEAR, isoDate.get(YEAR)); cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1); cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH)); // For every date in the range while (isoDate.isBefore(isoEndDate)) { assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal); assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate); assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate); int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1; int dow = isoDate.get(weekDef.dayOfWeek()); assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek"); int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH); int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth()); assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth"); int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR); int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear()); assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear"); int jWeekYear = cal.getWeekYear(); int weekBasedYear = isoDate.get(weekDef.weekBasedYear()); assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear"); int jweeksInWeekyear = cal.getWeeksInWeekYear(); int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum(); assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear"); isoDate = isoDate.plus(1, ChronoUnit.DAYS); cal.add(Calendar.DAY_OF_MONTH, 1); } } } }
Example 19
Source File: TestIsoChronoImpl.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider = "RangeVersusCalendar") public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) { GregorianCalendar cal = new GregorianCalendar(); assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type"); LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate); for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) { for (int minDays = 1; minDays <= 7; minDays++) { WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays); cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1); cal.setMinimalDaysInFirstWeek(minDays); cal.setTimeZone(TimeZone.getTimeZone("GMT+00")); cal.set(Calendar.YEAR, isoDate.get(YEAR)); cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1); cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH)); // For every date in the range while (isoDate.isBefore(isoEndDate)) { assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal); assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate); assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate); int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1; int dow = isoDate.get(weekDef.dayOfWeek()); assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek"); int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH); int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth()); assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth"); int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR); int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear()); assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear"); int jWeekYear = cal.getWeekYear(); int weekBasedYear = isoDate.get(weekDef.weekBasedYear()); assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear"); int jweeksInWeekyear = cal.getWeeksInWeekYear(); int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum(); assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear"); isoDate = isoDate.plus(1, ChronoUnit.DAYS); cal.add(Calendar.DAY_OF_MONTH, 1); } } } }
Example 20
Source File: DurationImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
/** * <p>Constructs a new Duration object by specifying the duration * in milliseconds.</p> * * @param durationInMilliSeconds * The length of the duration in milliseconds. */ protected DurationImpl(final long durationInMilliSeconds) { long l = durationInMilliSeconds; if (l > 0) { signum = 1; } else if (l < 0) { signum = -1; if (l == 0x8000000000000000L) { // negating 0x8000000000000000L causes an overflow l++; } l *= -1; } else { signum = 0; } // let GregorianCalendar do the heavy lifting GregorianCalendar gregorianCalendar = new GregorianCalendar(GMT); // duration is the offset from the Epoch gregorianCalendar.setTimeInMillis(l); // now find out how much each field has changed long int2long = 0L; // years int2long = gregorianCalendar.get(Calendar.YEAR) - 1970; this.years = BigInteger.valueOf(int2long); // months int2long = gregorianCalendar.get(Calendar.MONTH); this.months = BigInteger.valueOf(int2long); // days int2long = gregorianCalendar.get(Calendar.DAY_OF_MONTH) - 1; this.days = BigInteger.valueOf(int2long); // hours int2long = gregorianCalendar.get(Calendar.HOUR_OF_DAY); this.hours = BigInteger.valueOf(int2long); // minutes int2long = gregorianCalendar.get(Calendar.MINUTE); this.minutes = BigInteger.valueOf(int2long); // seconds & milliseconds int2long = (gregorianCalendar.get(Calendar.SECOND) * 1000) + gregorianCalendar.get(Calendar.MILLISECOND); this.seconds = BigDecimal.valueOf(int2long, 3); }