Java 8 Calendar - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Calendar class.

This code example shows how to use the following methods:getTimeInMillis, set, setTimeInMillis
        return DateUtilities.getRelativeDay(context, date, true);
    } 
 
    private static final Calendar calendar = Calendar.getInstance();
    public static long getStartOfDay(long time) {
        calendar.setTimeInMillis(time);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    } 
 
    private static long clearTime(Date date) {
        date.setTime(date.getTime() / 1000L * 1000);
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        return date.getTime();
    } 
8
CodeRank
This code example shows how to use the following methods:get
        "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 
    }; 
 
    private Object getPart(Calendar c, String part) {
        if ("hours".equals(part) || "hour".equals(part) || "h".equals(part)) {
            return c.get(Calendar.HOUR_OF_DAY);
        } else if ("minutes".equals(part) || "minute".equals(part) || "min".equals(part)) { // avoid 'm' to avoid confusion with month
            return c.get(Calendar.MINUTE);
        } else if ("seconds".equals(part) || "sec".equals(part) || "s".equals(part)) {
            return c.get(Calendar.SECOND);
        } else if ("milliseconds".equals(part) || "ms".equals(part) || "S".equals(part)) {
            return c.get(Calendar.MILLISECOND);
        } else if ("years".equals(part) || "year".equals(part)) {
            return c.get(Calendar.YEAR);
        } else if ("months".equals(part) || "month".equals(part)) { // avoid 'm' to avoid confusion with minute
            return c.get(Calendar.MONTH) + 1; // ISSUE 115 - people expect January to be 1 not 0
        } else if ("weeks".equals(part) || "week".equals(part) || "w".equals(part)) {
            return c.get(Calendar.WEEK_OF_MONTH);
        } else if ("days".equals(part) || "day".equals(part) || "d".equals(part)) {
            return c.get(Calendar.DAY_OF_MONTH);

8
CodeRank
This code example shows how to use the following methods:get, getTimeInMillis
        new SerializationTester<Calendar>(calendar, s).test();
    } 
 
    private void assertCalendarEquals(Calendar calendar,
            int year, int month, int day, int hour, int minute) {
        assertEquals(year, calendar.get(Calendar.YEAR));
        assertEquals(month, calendar.get(Calendar.MONTH));
        assertEquals(day, calendar.get(Calendar.DATE));
        assertEquals(hour, calendar.get(Calendar.HOUR_OF_DAY));
        assertEquals(minute, calendar.get(Calendar.MINUTE));
    } 
 
    private static double hoursSinceEpoch(Calendar c) {
        double ONE_HOUR = 3600d * 1000d;
        return c.getTimeInMillis() / ONE_HOUR;
    } 
} 
8
CodeRank
This code example shows how to use the following methods:setTime
 
 
    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");
8
CodeRank
This code example shows how to use the following methods:get
    updateLabel(); 
  } 
   
  public void chooseDate(View v) {
    new DatePickerDialog(ChronoDemo.this, d,
                          dateAndTime.get(Calendar.YEAR),
                          dateAndTime.get(Calendar.MONTH),
                          dateAndTime.get(Calendar.DAY_OF_MONTH))
      .show();
  } 
   
  public void chooseTime(View v) {
    new TimePickerDialog(ChronoDemo.this, t,
                          dateAndTime.get(Calendar.HOUR_OF_DAY),
                          dateAndTime.get(Calendar.MINUTE),
                          true) 
      .show();
  } 
   
  private void updateLabel() { 
8
CodeRank
This code example shows how to use the following methods:getTimeInMillis, set
   
  private void updateLabel() { 
    dateAndTimeLabel
      .setText(DateUtils
                 .formatDateTime(this,
                                 dateAndTime.getTimeInMillis(),
                                 DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_SHOW_TIME));
  } 
   
  DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, 
                          int dayOfMonth) { 
      dateAndTime.set(Calendar.YEAR, year); 
      dateAndTime.set(Calendar.MONTH, monthOfYear); 
      dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
      updateLabel(); 
    } 
  }; 
   
  TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
8
CodeRank
This code example shows how to use the following methods:get, getTimeZone
     * @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;
 
8
CodeRank
This code example shows how to use the following methods:setTime
        // 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);
        } 
8
CodeRank
This code example shows how to use the following methods:get
    return clone;
  } 
 
  public static Calendar ymd(Calendar basis) {
    Calendar clone = Time.ym(basis);
    clone.set(Calendar.DAY_OF_MONTH, basis.get(Calendar.DAY_OF_MONTH));
    return clone;
  } 
 
  public static Calendar ymdh(Calendar basis) {
    Calendar clone = Time.ymd(basis);
    clone.set(Calendar.HOUR_OF_DAY, basis.get(Calendar.HOUR_OF_DAY));
    return clone;
  } 
 
  public static Calendar ymdhm(Calendar basis) {
    Calendar clone = Time.ymdh(basis);
    clone.set(Calendar.MINUTE, basis.get(Calendar.MINUTE));
    return clone;
  } 

8
CodeRank
This code example shows how to use the following methods:get
        schedule.start(start.getTime());
 
        CalendarBasedTimeout calendarTimeout = new CalendarBasedTimeout(schedule);
        Calendar firstTimeout = calendarTimeout.getFirstTimeout();
 
        if(firstTimeout.get(Calendar.DAY_OF_MONTH) != 1 ||
                firstTimeout.get(Calendar.HOUR_OF_DAY) != 0 ||
                firstTimeout.get(Calendar.MINUTE) != 0 ||
                firstTimeout.get(Calendar.SECOND) != 0) {
            Assert.fail(timeZoneDisplayName);
        } 
    } 
 
    /** 
     * Check a Timeout if the Schedule start date in the future (day after) 
     * The schedule is on the first day of month to ensure that the calculated time must be moved to the next month. 
     */ 
    //@Test 
    public void testWithStartInTheFutureAndLaterSchedule() { 
        ScheduleExpression schedule = this.getTimezoneSpecificScheduleExpression();
8
CodeRank

Related Java 8 Calendar Questions & Answers:

Other Java 8 Calendar Examples

5:	    public Dialog onCreateDialog(Bundle savedInstanceState) {
6:	        // Use the current date as the default date in the picker
7:	        final Calendar c = Calendar.getInstance();
8:	        int year = c.get(Calendar.YEAR);
9:	        int month = c.get(Calendar.MONTH);

Read More

5:	    public Dialog onCreateDialog(Bundle savedInstanceState) {
6:	        // Use the current time as the default values for the picker
7:	        final Calendar c = Calendar.getInstance();
8:	        int hour = c.get(Calendar.HOUR_OF_DAY);
9:	        int minute = c.get(Calendar.MINUTE);

Read More

See Code Examples for Java 8 Calendar Methods: