These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 GregorianCalendar class.
private GregorianCalendar parse(String o1, DateFormat formatter) { try { Date date = formatter.parse(o1); GregorianCalendar c = new GregorianCalendar(); c.setTime(date); return c; } catch (java.text.ParseException e) { return null; } } @Override public void write(JSONWriter writer, Properties options) throws JSONException { writer.object(); writer.key("description"); writer.value("Returns o converted to a date object, you can hint if the day or the month is listed first, or give an ordered list of possible formats using this syntax: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html");
// current "nextExpiration" Date currentTimeout = timer.getNextExpiration(); if (currentTimeout == null) { return null; } Calendar cal = new GregorianCalendar(); cal.setTime(currentTimeout); // now compute the next timeout date Calendar nextTimeout = ((CalendarTimer) timer).getCalendarTimeout().getNextTimeout(cal); if (nextTimeout != null) { return nextTimeout.getTime(); } return null; } @Override protected void scheduleTimeoutIfRequired(TimerImpl timer) { if (timer.getNextExpiration() != null) { timer.scheduleTimeout(false); }
* @param calendar The {@link java.util.Calendar} whose {@link java.util.Calendar#MONTH} field will be used * as the current month * @return */ public static int getLastDateOfMonth(Calendar calendar) { Calendar tmpCal = new GregorianCalendar(calendar.getTimeZone()); tmpCal.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); tmpCal.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); tmpCal.set(Calendar.DAY_OF_MONTH, 1); return tmpCal.getActualMaximum(Calendar.DAY_OF_MONTH); } public static Integer getNthDayOfMonth(Calendar cal, int n, int dayOfWeek) { int dateOfFirstXXXDay = getFirstDateInMonthForDayOfWeek(cal, dayOfWeek); final int FIRST_WEEK = 1; final int NUM_DAYS_IN_WEEK = 7; int weekDiff = n - FIRST_WEEK;
} private void testSetSelfConsistent(TimeZone timeZone, int year, int month, int day) { int hour = 0; int minute = 0; Calendar calendar = new GregorianCalendar(timeZone); calendar.clear(); calendar.set(year, month, day, hour, minute); assertEquals(year, calendar.get(Calendar.YEAR)); assertEquals(month, calendar.get(Calendar.MONTH)); assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH)); assertEquals(hour, calendar.get(Calendar.HOUR_OF_DAY)); assertEquals(minute, calendar.get(Calendar.MINUTE)); } // http://b/5179775 public void testCalendarSerialization() { String s = "aced00057372001b6a6176612e7574696c2e477265676f7269616e43616c656e6461728f3dd7d6e" + "5b0d0c103000749000f63757272656e7459656172536b65774a0010677265676f7269616e4375746" + "f7665725a000869734361636865644a00126c6173744d69646e696768744d696c6c697349000c6c6"
} return toAppendTo.append(formatted); } private String ordinalSuffix(NSTimestamp timestamp) { GregorianCalendar calendar = new GregorianCalendar(); calendar.setTimeZone(defaultFormatTimeZone()); calendar.setTime(timestamp); switch (calendar.get(GregorianCalendar.DAY_OF_MONTH)) { case 1: case 21: case 31: return "st"; case 2: case 22: return "nd"; case 3: case 23: return "rd"; default:
ClockImpl clock = new ClockImpl(); GregorianCalendar dateRetrieved = clock.nowForCachingPurpose(); assertThat(dateRetrieved.get(GregorianCalendar.YEAR)) .isEqualTo(now.get(GregorianCalendar.YEAR)); assertThat(dateRetrieved.get(GregorianCalendar.MONTH)) .isEqualTo(now.get(GregorianCalendar.MONTH)); assertThat(dateRetrieved.get(GregorianCalendar.DAY_OF_MONTH)) .isEqualTo(now.get(GregorianCalendar.DAY_OF_MONTH)); assertThat(dateRetrieved.get(GregorianCalendar.HOUR_OF_DAY)) .isEqualTo(now.get(GregorianCalendar.HOUR_OF_DAY)); assertThat(dateRetrieved.get(GregorianCalendar.MINUTE)) .isEqualTo(1);
Date pastDate = DateUtils.getDateWithoutTimeStamp(calendar.getTime()); return pastDate; } Date getFirstDateForWeek(GregorianCalendar gc, Date startDate, int meetingDayOfWeek) { gc.setTime(startDate); // Jump to next week if the required weekday has passed for current week if (gc.get(Calendar.DAY_OF_WEEK) > meetingDayOfWeek) { gc.add(Calendar.WEEK_OF_MONTH, 1); } // Set the day of week as the require weekday gc.set(Calendar.DAY_OF_WEEK, meetingDayOfWeek); return gc.getTime(); } @Test public void testDayOfWeek() { Date twoWeeksAgo = createPreviousDate(14);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); //Set the alarm time String time = app_preferences.getString("timeAutoDownload", "2:0"); GregorianCalendar currentCalendar = new GregorianCalendar(); long alarmTime = new GregorianCalendar( currentCalendar.get(Calendar.YEAR), currentCalendar.get(Calendar.MONTH), currentCalendar.get(Calendar.DAY_OF_MONTH), Integer.valueOf(time.split(":")[0]), Integer.valueOf(time.split(":")[1])).getTime().getTime(); //Add a day if the hour has passed if (alarmTime < currentCalendar.getTime().getTime()) alarmTime = alarmTime + AlarmManager.INTERVAL_DAY; am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY, alarm_sender); Log.i("AlarmHostService", "set " + (new Date(alarmTime))); }
} else { // The when string can be local time, or UTC if it ends with a Z Date date; if (when.length() == 16 && when.charAt(15) == 'Z') { date = DATE_TIME_FORMAT.parse(when.substring(0, 15)); Calendar calendar = new GregorianCalendar(); long milliseconds = date.getTime(); // Account for time zone difference milliseconds += calendar.get(Calendar.ZONE_OFFSET); // Might need to correct for daylight savings time, but use target time since // now might be in DST but not then, or vice versa calendar.setTime(new Date(milliseconds)); milliseconds += calendar.get(Calendar.DST_OFFSET); date = new Date(milliseconds); } else { date = DATE_TIME_FORMAT.parse(when); } return date; } }
* @param calendar * @return String representing the relative date */ public static String getRelativeDate(Calendar calendar) { Calendar today = new GregorianCalendar(); today.set(GregorianCalendar.HOUR_OF_DAY, 0); today.set(GregorianCalendar.MINUTE, 0); today.set(GregorianCalendar.SECOND, 0); today.set(GregorianCalendar.MILLISECOND, 0); return getRelativeDate(today, calendar); } public static String getRelativeDate(Calendar d1, Calendar d2) { long diff = d1.getTimeInMillis() - d2.getTimeInMillis(); if (diff < 0 || diff >= YEAR) { // future or far in past, // just return yyyy-mm-dd