List of usage examples for java.util Calendar AM
int AM
To view the source code for java.util Calendar AM.
Click Source Link
From source file:com.etime.ETimeActivity.java
/** * Converts a punch to a human readable time format * "12:00 AM"/* w ww . j a va 2 s. c om*/ * @param punch Punch to convert to readable string * @return A string conversion of a Punch in a readable format */ public String punchToTimeString(Punch punch) { Calendar calendar = punch.getCalendar(); int hour = getHourFromCalendar(calendar); int minute = calendar.get(Calendar.MINUTE); String minStr = (minute < 10) ? "0" + minute : "" + minute; return " " + hour + ":" + minStr + " " + ((calendar.get(Calendar.AM_PM) == Calendar.AM) ? "AM" : "PM"); }
From source file:com.owncloud.android.ui.activity.ContactsPreferenceActivity.java
public void openDate(View v) { String backupFolderString = getResources().getString(R.string.contacts_backup_folder) + OCFile.PATH_SEPARATOR;//from w w w. j ava 2s.c o m OCFile backupFolder = getStorageManager().getFileByPath(backupFolderString); Vector<OCFile> backupFiles = getStorageManager().getFolderContent(backupFolder, false); Collections.sort(backupFiles, new Comparator<OCFile>() { @Override public int compare(OCFile o1, OCFile o2) { if (o1.getModificationTimestamp() == o2.getModificationTimestamp()) { return 0; } if (o1.getModificationTimestamp() > o2.getModificationTimestamp()) { return 1; } else { return -1; } } }); Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { String backupFolderString = getResources().getString(R.string.contacts_backup_folder) + OCFile.PATH_SEPARATOR; OCFile backupFolder = getStorageManager().getFileByPath(backupFolderString); Vector<OCFile> backupFiles = getStorageManager().getFolderContent(backupFolder, false); // find file with modification with date and time between 00:00 and 23:59 // if more than one file exists, take oldest Calendar date = Calendar.getInstance(); date.set(year, month, dayOfMonth); // start date.set(Calendar.HOUR, 0); date.set(Calendar.MINUTE, 0); date.set(Calendar.SECOND, 1); date.set(Calendar.MILLISECOND, 0); date.set(Calendar.AM_PM, Calendar.AM); Long start = date.getTimeInMillis(); // end date.set(Calendar.HOUR, 23); date.set(Calendar.MINUTE, 59); date.set(Calendar.SECOND, 59); Long end = date.getTimeInMillis(); OCFile backupToRestore = null; for (OCFile file : backupFiles) { if (start < file.getModificationTimestamp() && end > file.getModificationTimestamp()) { if (backupToRestore == null) { backupToRestore = file; } else if (backupToRestore.getModificationTimestamp() < file.getModificationTimestamp()) { backupToRestore = file; } } } if (backupToRestore != null) { Fragment contactListFragment = ContactListFragment.newInstance(backupToRestore, getAccount()); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.contacts_linear_layout, contactListFragment); transaction.commit(); } else { Toast.makeText(ContactsPreferenceActivity.this, R.string.contacts_preferences_no_file_found, Toast.LENGTH_SHORT).show(); } } }; DatePickerDialog datePickerDialog = new DatePickerDialog(this, dateSetListener, year, month, day); datePickerDialog.getDatePicker().setMaxDate(backupFiles.lastElement().getModificationTimestamp()); datePickerDialog.getDatePicker().setMinDate(backupFiles.firstElement().getModificationTimestamp()); datePickerDialog.show(); }
From source file:com.etime.ETimeActivity.java
/** * Update the button timeToClockOut/*from w w w . j av a 2 s . c o m*/ * @param eightHrPunchCalendar A calendar object specifying the time where * the user has been punched in for exactly 8 hrs * today. */ private void updateTimeToClockOut(Calendar eightHrPunchCalendar) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Clock out at "); StringBuilder clockTimeString = new StringBuilder(); clockTimeString.append(Integer.toString(getHourFromCalendar(eightHrPunchCalendar))).append(":"); int min = eightHrPunchCalendar.get(Calendar.MINUTE); if (min < 10) { clockTimeString.append("0"); } clockTimeString.append(Integer.toString(min)); if (eightHrPunchCalendar.get(Calendar.AM_PM) == Calendar.AM) { clockTimeString.append(" AM"); } else { clockTimeString.append(" PM"); } stringBuilder.append(clockTimeString); timeToClockOut.setText(stringBuilder.toString()); }
From source file:com.panet.imeta.job.entries.special.JobEntrySpecial.java
private long getNextMonthlyExecutionTime() { Calendar calendar = Calendar.getInstance(); long nowMillis = calendar.getTimeInMillis(); int amHour = hour; if (amHour > 12) { amHour = amHour - 12;//w w w . j a v a2s .co m calendar.set(Calendar.AM_PM, Calendar.PM); } else { calendar.set(Calendar.AM_PM, Calendar.AM); } calendar.set(Calendar.HOUR, amHour); calendar.set(Calendar.MINUTE, minutes); calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); if (calendar.getTimeInMillis() <= nowMillis) { calendar.add(Calendar.MONTH, 1); } return calendar.getTimeInMillis() - nowMillis; }
From source file:com.panet.imeta.job.entries.special.JobEntrySpecial.java
private long getNextWeeklyExecutionTime() { Calendar calendar = Calendar.getInstance(); long nowMillis = calendar.getTimeInMillis(); int amHour = hour; if (amHour > 12) { amHour = amHour - 12;/*from ww w . ja va 2 s . c o m*/ calendar.set(Calendar.AM_PM, Calendar.PM); } else { calendar.set(Calendar.AM_PM, Calendar.AM); } calendar.set(Calendar.HOUR, amHour); calendar.set(Calendar.MINUTE, minutes); calendar.set(Calendar.DAY_OF_WEEK, weekDay + 1); if (calendar.getTimeInMillis() <= nowMillis) { calendar.add(Calendar.WEEK_OF_YEAR, 1); } return calendar.getTimeInMillis() - nowMillis; }
From source file:org.kuali.ole.sys.batch.service.impl.SchedulerServiceImpl.java
protected boolean isPastScheduleCutoffTime(Calendar dateTime, boolean log) { try {//from www. j a v a 2s . c o m Date scheduleCutoffTimeTemp = scheduler.getTriggersOfJob(SCHEDULE_JOB_NAME, SCHEDULED_GROUP)[0] .getPreviousFireTime(); Calendar scheduleCutoffTime; if (scheduleCutoffTimeTemp == null) { scheduleCutoffTime = dateTimeService.getCurrentCalendar(); } else { scheduleCutoffTime = dateTimeService.getCalendar(scheduleCutoffTimeTemp); } String cutoffParameter = parameterService.getParameterValueAsString(ScheduleStep.class, OLEConstants.SystemGroupParameterNames.BATCH_SCHEDULE_CUTOFF_TIME); String[] scheduleStepCutoffTime = StringUtils.split(cutoffParameter, ":"); if (scheduleStepCutoffTime.length != 3 && scheduleStepCutoffTime.length != 4) { throw new IllegalArgumentException( "Error! The " + OLEConstants.SystemGroupParameterNames.BATCH_SCHEDULE_CUTOFF_TIME + " parameter had an invalid value: " + cutoffParameter); } // if there are 4 components, then we have an AM/PM delimiter // otherwise, assume 24-hour time if (scheduleStepCutoffTime.length == 4) { int hour = Integer.parseInt(scheduleStepCutoffTime[0]); // need to adjust for meaning of hour if (hour == 12) { hour = 0; } else { hour--; } scheduleCutoffTime.set(Calendar.HOUR, hour); if (StringUtils.containsIgnoreCase(scheduleStepCutoffTime[3], "AM")) { scheduleCutoffTime.set(Calendar.AM_PM, Calendar.AM); } else { scheduleCutoffTime.set(Calendar.AM_PM, Calendar.PM); } } else { scheduleCutoffTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(scheduleStepCutoffTime[0])); } scheduleCutoffTime.set(Calendar.MINUTE, Integer.parseInt(scheduleStepCutoffTime[1])); scheduleCutoffTime.set(Calendar.SECOND, Integer.parseInt(scheduleStepCutoffTime[2])); if (parameterService.getParameterValueAsBoolean(ScheduleStep.class, OLEConstants.SystemGroupParameterNames.BATCH_SCHEDULE_CUTOFF_TIME_IS_NEXT_DAY)) { scheduleCutoffTime.add(Calendar.DAY_OF_YEAR, 1); } boolean isPastScheduleCutoffTime = dateTime.after(scheduleCutoffTime); if (log) { LOG.info(new StringBuilder("isPastScheduleCutoffTime=").append(isPastScheduleCutoffTime) .append(" : ").append(dateTimeService.toDateTimeString(dateTime.getTime())).append(" / ") .append(dateTimeService.toDateTimeString(scheduleCutoffTime.getTime()))); } return isPastScheduleCutoffTime; } catch (NumberFormatException e) { throw new RuntimeException( "Caught exception while checking whether we've exceeded the schedule cutoff time", e); } catch (SchedulerException e) { throw new RuntimeException( "Caught exception while checking whether we've exceeded the schedule cutoff time", e); } }
From source file:amhamogus.com.daysoff.fragments.AddEventFragment.java
@Override public void onTimeSet(TimePicker timePicker, int pickerHour, int pickerMinute) { String displayHour;/*from w w w .ja va2 s . c o m*/ String displayMinute; Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MINUTE, pickerMinute); calendar.set(Calendar.HOUR_OF_DAY, pickerHour); hour = pickerHour; minute = pickerMinute; if (hour == 0) { displayHour = "12"; noonOrNight = "AM"; } else if (hour > 12) { displayHour = (hour - 12) + ""; noonOrNight = "PM"; } else { noonOrNight = "AM"; displayHour = hour + ""; } if (minute < 10) { displayMinute = "0" + minute; } else { displayMinute = minute + ""; } // Add AM or PM to the user selected time range String amOrPm = ((calendar.get(Calendar.AM_PM)) == Calendar.AM) ? "am" : "pm"; timeLabel.setText(displayHour + ":" + displayMinute + " " + amOrPm); }
From source file:eionet.util.Util.java
/** * A method for calculating time difference in MILLISECONDS, between a date-time specified in input parameters and the current * date-time. <BR>/*from www . j a v a 2 s . com*/ * This should be useful for calculating sleep time for code that has a certain schedule for execution. * * @param hour * An integer from 0 to 23. If less than 0 or more than 23, then the closest next hour to current hour is taken. * @param date * An integer from 1 to 31. If less than 1 or more than 31, then the closest next date to current date is taken. * @param month * An integer from Calendar.JANUARY to Calendar.DECEMBER. If out of those bounds, the closest next month to current * month is taken. * @param wday * An integer from 1 to 7. If out of those bounds, the closest next weekday to weekday month is taken. * @param zone * A String specifying the time-zone in which the calculations should be done. Please see Java documentation an * allowable time-zones and formats. * @return Time difference in milliseconds. */ public static long timeDiff(int hour, int date, int month, int wday, String zone) { GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone(zone)); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.setFirstDayOfWeek(Calendar.MONDAY); /* * here we force the hour to be one of the defualts if (hour < 0) hour = 0; if (hour > 23) hour = 23; */ int cur_hour = cal.get(Calendar.HOUR); if (cal.get(Calendar.AM_PM) == Calendar.PM) { cur_hour = 12 + cur_hour; } // here we assume that every full hour is accepted /* * if (hour < 0 || hour > 23) { hour = cur_hour>=23 ? 0 : cur_hour + 1; } */ if (wday >= 1 && wday <= 7) { int cur_wday = cal.get(Calendar.DAY_OF_WEEK); if (hour < 0 || hour > 23) { if (cur_wday != wday) { hour = 0; } else { hour = cur_hour >= 23 ? 0 : cur_hour + 1; } } int amount = wday - cur_wday; if (amount < 0) { amount = 7 + amount; } if (amount == 0 && cur_hour >= hour) { amount = 7; } cal.add(Calendar.DAY_OF_WEEK, amount); } else if (month >= Calendar.JANUARY && month <= Calendar.DECEMBER) { // do something about when every date is accepted if (date < 1) { date = 1; } if (date > 31) { date = 31; } int cur_month = cal.get(Calendar.MONTH); int amount = month - cur_month; if (amount < 0) { amount = 12 + amount; } if (amount == 0) { if (cal.get(Calendar.DATE) > date) { amount = 12; } else if (cal.get(Calendar.DATE) == date) { if (cur_hour >= hour) { amount = 12; } } } // cal.set(Calendar.DATE, date); cal.add(Calendar.MONTH, amount); if (date > cal.getActualMaximum(Calendar.DATE)) { date = cal.getActualMaximum(Calendar.DATE); } cal.set(Calendar.DATE, date); } else if (date >= 1 && date <= 31) { int cur_date = cal.get(Calendar.DATE); if (cur_date > date) { cal.add(Calendar.MONTH, 1); } else if (cur_date == date) { if (cur_hour >= hour) { cal.add(Calendar.MONTH, 1); } } cal.set(Calendar.DATE, date); } else { if (hour < 0 || hour > 23) { hour = cur_hour >= 23 ? 0 : cur_hour + 1; } if (cur_hour >= hour) { cal.add(Calendar.DATE, 1); } } if (hour >= 12) { cal.set(Calendar.HOUR, hour - 12); cal.set(Calendar.AM_PM, Calendar.PM); } else { cal.set(Calendar.HOUR, hour); cal.set(Calendar.AM_PM, Calendar.AM); } Date nextDate = cal.getTime(); Date currDate = new Date(); long nextTime = cal.getTime().getTime(); long currTime = (new Date()).getTime(); return nextTime - currTime; }
From source file:org.kuali.kfs.sys.batch.service.impl.SchedulerServiceImpl.java
protected boolean isPastScheduleCutoffTime(Calendar dateTime, boolean log) { try {// w ww . ja v a 2 s. com Date scheduleCutoffTimeTemp = scheduler.getTriggersOfJob(SCHEDULE_JOB_NAME, SCHEDULED_GROUP)[0] .getPreviousFireTime(); Calendar scheduleCutoffTime; if (scheduleCutoffTimeTemp == null) { scheduleCutoffTime = dateTimeService.getCurrentCalendar(); } else { scheduleCutoffTime = dateTimeService.getCalendar(scheduleCutoffTimeTemp); } String cutoffParameter = parameterService.getParameterValueAsString(ScheduleStep.class, KFSConstants.SystemGroupParameterNames.BATCH_SCHEDULE_CUTOFF_TIME); String[] scheduleStepCutoffTime = StringUtils.split(cutoffParameter, ":"); if (scheduleStepCutoffTime.length != 3 && scheduleStepCutoffTime.length != 4) { throw new IllegalArgumentException( "Error! The " + KFSConstants.SystemGroupParameterNames.BATCH_SCHEDULE_CUTOFF_TIME + " parameter had an invalid value: " + cutoffParameter); } // if there are 4 components, then we have an AM/PM delimiter // otherwise, assume 24-hour time if (scheduleStepCutoffTime.length == 4) { int hour = Integer.parseInt(scheduleStepCutoffTime[0]); // need to adjust for meaning of hour if (hour == 12) { hour = 0; } else { hour--; } scheduleCutoffTime.set(Calendar.HOUR, hour); if (StringUtils.containsIgnoreCase(scheduleStepCutoffTime[3], "AM")) { scheduleCutoffTime.set(Calendar.AM_PM, Calendar.AM); } else { scheduleCutoffTime.set(Calendar.AM_PM, Calendar.PM); } } else { scheduleCutoffTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(scheduleStepCutoffTime[0])); } scheduleCutoffTime.set(Calendar.MINUTE, Integer.parseInt(scheduleStepCutoffTime[1])); scheduleCutoffTime.set(Calendar.SECOND, Integer.parseInt(scheduleStepCutoffTime[2])); if (parameterService.getParameterValueAsBoolean(ScheduleStep.class, KFSConstants.SystemGroupParameterNames.BATCH_SCHEDULE_CUTOFF_TIME_IS_NEXT_DAY)) { scheduleCutoffTime.add(Calendar.DAY_OF_YEAR, 1); } boolean isPastScheduleCutoffTime = dateTime.after(scheduleCutoffTime); if (log) { LOG.info(new StringBuilder("isPastScheduleCutoffTime=").append(isPastScheduleCutoffTime) .append(" : ").append(dateTimeService.toDateTimeString(dateTime.getTime())).append(" / ") .append(dateTimeService.toDateTimeString(scheduleCutoffTime.getTime()))); } return isPastScheduleCutoffTime; } catch (NumberFormatException e) { throw new RuntimeException( "Caught exception while checking whether we've exceeded the schedule cutoff time", e); } catch (SchedulerException e) { throw new RuntimeException( "Caught exception while checking whether we've exceeded the schedule cutoff time", e); } }
From source file:com.panet.imeta.job.entries.special.JobEntrySpecial.java
private long getNextDailyExecutionTime() { Calendar calendar = Calendar.getInstance(); long nowMillis = calendar.getTimeInMillis(); int amHour = hour; if (amHour > 12) { amHour = amHour - 12;// w w w. j a v a 2 s. c o m calendar.set(Calendar.AM_PM, Calendar.PM); } else { calendar.set(Calendar.AM_PM, Calendar.AM); } calendar.set(Calendar.HOUR, amHour); calendar.set(Calendar.MINUTE, minutes); if (calendar.getTimeInMillis() <= nowMillis) { calendar.add(Calendar.DAY_OF_MONTH, 1); } return calendar.getTimeInMillis() - nowMillis; }