List of usage examples for java.util GregorianCalendar set
public void set(int field, int value)
From source file:com.bt.heliniumstudentapp.ScheduleFragment.java
@SuppressWarnings("ConstantConditions") @Override/*w w w . j ava 2 s. c o m*/ public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) { mainContext = (AppCompatActivity) getActivity(); scheduleLayout = inflater.inflate(R.layout.fragment_schedule, viewGroup, false); MainActivity.setToolbarTitle(mainContext, getResources().getString(R.string.schedule), null); weekDaysLV = (ListView) scheduleLayout.findViewById(R.id.lv_weekDays_fs); final boolean restart = PreferenceManager.getDefaultSharedPreferences(mainContext) .getBoolean("forced_restart", false); if (restart) PreferenceManager.getDefaultSharedPreferences(mainContext).edit().putBoolean("forced_restart", false) .apply(); if (restart) { //TODO Database check? MainActivity.setStatusBar(mainContext); scheduleFocus = new GregorianCalendar(HeliniumStudentApp.LOCALE).get(Calendar.WEEK_OF_YEAR); scheduleJson = PreferenceManager.getDefaultSharedPreferences(mainContext).getString("schedule_0", null); if (MainActivity.isOnline()) parseData(HeliniumStudentApp.ACTION_ONLINE); else parseData(HeliniumStudentApp.ACTION_OFFLINE); } else if (scheduleJson == null) { final boolean online = MainActivity.isOnline(); scheduleFocus = new GregorianCalendar(HeliniumStudentApp.LOCALE).get(Calendar.WEEK_OF_YEAR); if (online && PreferenceManager.getDefaultSharedPreferences(mainContext) .getBoolean("pref_updates_auto_update", true)) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) new UpdateClass(mainContext, false).execute(); else new UpdateClass(mainContext, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } if (online && (PreferenceManager.getDefaultSharedPreferences(mainContext) .getBoolean("pref_schedule_init", true) || PreferenceManager.getDefaultSharedPreferences(mainContext).getString("schedule_0", null) == null)) { getSchedule(HeliniumStudentApp.DIREC_CURRENT, HeliniumStudentApp.ACTION_INIT_IN); } else if (checkDatabase() != HeliniumStudentApp.DB_REFRESHING) { MainActivity.setStatusBar(mainContext); scheduleJson = PreferenceManager.getDefaultSharedPreferences(mainContext).getString("schedule_0", null); if (online) parseData(HeliniumStudentApp.ACTION_ONLINE); else parseData(HeliniumStudentApp.ACTION_OFFLINE); } } ((SwipeRefreshLayout) scheduleLayout).setColorSchemeResources(MainActivity.accentSecondaryColor, MainActivity.accentPrimaryColor, MainActivity.primaryColor); ((SwipeRefreshLayout) scheduleLayout).setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refresh(); } }); MainActivity.prevIV.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (checkDatabase() != HeliniumStudentApp.DB_REFRESHING) { if (MainActivity.isOnline()) { scheduleFocus--; getSchedule(HeliniumStudentApp.DIREC_BACK, HeliniumStudentApp.ACTION_REFRESH_IN); } else { final int currentWeek = new GregorianCalendar(HeliniumStudentApp.LOCALE) .get(Calendar.WEEK_OF_YEAR); if (scheduleFocus > currentWeek + 1) { scheduleFocus = currentWeek + 1; scheduleJson = PreferenceManager.getDefaultSharedPreferences(mainContext) .getString("schedule_1", null); } else { scheduleFocus = currentWeek; scheduleJson = PreferenceManager.getDefaultSharedPreferences(mainContext) .getString("schedule_0", null); } parseData(HeliniumStudentApp.ACTION_OFFLINE); } } } }); MainActivity.historyIV.setOnClickListener(new OnClickListener() { //FIXME Huge mess private int year; private int monthOfYear; private int dayOfMonth; @Override public void onClick(View v) { if (checkDatabase() != HeliniumStudentApp.DB_REFRESHING) { if (MainActivity.isOnline()) { MainActivity.setUI(HeliniumStudentApp.VIEW_SCHEDULE, HeliniumStudentApp.ACTION_ONLINE); final AlertDialog.Builder weekpickerDialogBuilder = new AlertDialog.Builder( new ContextThemeWrapper(mainContext, MainActivity.themeDialog)); final View view = View.inflate(mainContext, R.layout.dialog_schedule, null); weekpickerDialogBuilder.setView(view); final DatePicker datePicker = (DatePicker) view.findViewById(R.id.np_weekpicker_dw); year = new GregorianCalendar(HeliniumStudentApp.LOCALE).get(Calendar.YEAR); monthOfYear = new GregorianCalendar(HeliniumStudentApp.LOCALE).get(Calendar.MONTH); dayOfMonth = new GregorianCalendar(HeliniumStudentApp.LOCALE).get(Calendar.DAY_OF_MONTH); weekpickerDialogBuilder.setTitle(getString(R.string.go_to)); weekpickerDialogBuilder.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { final GregorianCalendar date = new GregorianCalendar( HeliniumStudentApp.LOCALE); final GregorianCalendar today = new GregorianCalendar( HeliniumStudentApp.LOCALE); date.set(Calendar.YEAR, year); date.set(Calendar.MONTH, monthOfYear); date.set(Calendar.DAY_OF_MONTH, dayOfMonth); date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); today.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); scheduleFocus = (today.get(Calendar.WEEK_OF_YEAR)) - ((int) ((today.getTimeInMillis() / (1000 * 60 * 60 * 24 * 7)) - (date.getTimeInMillis() / (1000 * 60 * 60 * 24 * 7)))); getSchedule(HeliniumStudentApp.DIREC_OTHER, HeliniumStudentApp.ACTION_REFRESH_IN); } }); weekpickerDialogBuilder.setNegativeButton(getString(android.R.string.cancel), null); final AlertDialog weekPickerDialog = weekpickerDialogBuilder.create(); final GregorianCalendar minDate = new GregorianCalendar(HeliniumStudentApp.LOCALE); minDate.set(Calendar.YEAR, 2000); minDate.set(Calendar.WEEK_OF_YEAR, 1); final GregorianCalendar maxDate = new GregorianCalendar(HeliniumStudentApp.LOCALE); maxDate.set(Calendar.YEAR, 2038); maxDate.set(Calendar.WEEK_OF_YEAR, 1); datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() { final GregorianCalendar newDate = new GregorianCalendar(HeliniumStudentApp.LOCALE); @Override public void onDateChanged(DatePicker view, int dialogYear, int dialogMonthOfYear, int dialogDayOfMonth) { newDate.set(year, monthOfYear, dayOfMonth); year = dialogYear; monthOfYear = dialogMonthOfYear; dayOfMonth = dialogDayOfMonth; if (minDate != null && minDate.after(newDate)) view.init(minDate.get(Calendar.YEAR), minDate.get(Calendar.MONTH), minDate.get(Calendar.DAY_OF_MONTH), this); else if (maxDate != null && maxDate.before(newDate)) view.init(maxDate.get(Calendar.YEAR), maxDate.get(Calendar.MONTH), maxDate.get(Calendar.DAY_OF_MONTH), this); else view.init(year, monthOfYear, dayOfMonth, this); } }); weekPickerDialog.setCanceledOnTouchOutside(true); weekPickerDialog.show(); weekPickerDialog.getButton(AlertDialog.BUTTON_POSITIVE) .setTextColor(getColor(mainContext, MainActivity.accentSecondaryColor)); weekPickerDialog.getButton(AlertDialog.BUTTON_NEGATIVE) .setTextColor(getColor(mainContext, MainActivity.accentSecondaryColor)); } else { scheduleFocus = new GregorianCalendar(HeliniumStudentApp.LOCALE).get(Calendar.WEEK_OF_YEAR); scheduleJson = PreferenceManager.getDefaultSharedPreferences(mainContext) .getString("schedule_0", null); parseData(HeliniumStudentApp.ACTION_OFFLINE); } } } }); MainActivity.nextIV.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (checkDatabase() != HeliniumStudentApp.DB_REFRESHING) { if (MainActivity.isOnline()) { scheduleFocus++; getSchedule(HeliniumStudentApp.DIREC_NEXT, HeliniumStudentApp.ACTION_REFRESH_IN); } else { final int currentWeek = new GregorianCalendar(HeliniumStudentApp.LOCALE) .get(Calendar.WEEK_OF_YEAR); if (PreferenceManager.getDefaultSharedPreferences(mainContext).getString("schedule_1", null) != null && scheduleFocus >= currentWeek) { scheduleFocus = currentWeek + 1; scheduleJson = PreferenceManager.getDefaultSharedPreferences(mainContext) .getString("schedule_1", null); } else { scheduleFocus = currentWeek; scheduleJson = PreferenceManager.getDefaultSharedPreferences(mainContext) .getString("schedule_0", null); } parseData(HeliniumStudentApp.ACTION_OFFLINE); } } } }); return scheduleLayout; }
From source file:org.obm.icalendar.Ical4jHelper.java
@Override public List<Date> dateInInterval(EventRecurrence recurrence, Date eventDate, Date start, Date end, Set<Date> dateExce) { List<Date> ret = new LinkedList<Date>(); Recur recur = getRecur(recurrence, eventDate); if (recur == null) { ret.add(eventDate);/*from ww w.ja v a 2 s .com*/ return ret; } if (end == null) { if (start.before(eventDate)) { ret.add(eventDate); return ret; } return ImmutableList.of(); } DateList dl = recur.getDates(new DateTime(eventDate), new DateTime(start), new DateTime(end), Value.DATE_TIME); for (Iterator<?> it = dl.iterator(); it.hasNext();) { Date evD = (Date) it.next(); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(evD); cal.set(GregorianCalendar.MILLISECOND, 0); if (!dateExce.contains(cal.getTime())) { ret.add(evD); } } return ret; }
From source file:org.obm.icalendar.Ical4jHelper.java
private Recur getRecurFrom(EventRecurrence eventRecurrence, String frequency) { if (eventRecurrence.getEnd() == null) { return new Recur(frequency, null); } else {//w ww. j a v a 2s. c om GregorianCalendar cal = new GregorianCalendar(); cal.setTime(eventRecurrence.getEnd()); cal.set(GregorianCalendar.SECOND, 0); return new Recur(frequency, new DateTime(cal.getTime())); } }
From source file:PVGraph.java
public java.util.List<PeriodData> getMonthData(int year, int month) { Statement stmt = null;/* w w w. j a v a 2 s . c o m*/ String query = "select * from DayData where year(DateTime) = " + year + " and month(DateTime) = " + month + " and CurrentPower != 0 order by DateTime"; Map<String, PeriodData> result = new HashMap<String, PeriodData>(); GregorianCalendar gc = new GregorianCalendar(); try { getDatabaseConnection(); stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String serial = rs.getString("serial"); PeriodData pd = result.get(serial); if (pd == null) { pd = new PeriodData(); pd.serial = serial; pd.inverter = rs.getString("inverter"); pd.startTotalPower = rs.getDouble("ETotalToday"); gc.setTime(rs.getTimestamp("DateTime")); gc.set(Calendar.DAY_OF_MONTH, 1); gc.add(Calendar.MONTH, 1); gc.add(Calendar.DAY_OF_MONTH, -1); pd.numPowers = gc.get(Calendar.DAY_OF_MONTH); result.put(serial, pd); } double power = rs.getDouble("ETotalToday"); gc.setTime(rs.getTimestamp("DateTime")); pd.powers[gc.get(Calendar.DAY_OF_MONTH) - 1] = power; pd.endTotalPower = power; } } catch (SQLException e) { System.err.println("Query failed: " + e.getMessage()); } finally { try { stmt.close(); } catch (SQLException e) { // relax } } return new java.util.ArrayList<PeriodData>(result.values()); }
From source file:com.zimbra.cs.mailbox.calendar.ZRecur.java
private boolean checkMonthList(GregorianCalendar cal) { if (mByMonthList.size() > 0) { for (Integer cur : mByMonthList) { int curMonth = cal.get(Calendar.MONTH) + 1; if (cur == curMonth) return true; // since the month list is in order, if we hit a HIGHER month, // then we know out current month isn't in the list, and therefore // we should go to this next one if (cur > curMonth) { cal.set(Calendar.MONTH, cur - 1); return false; // must re-start checks }/*from w ww . jav a2 s. co m*/ } // we've not found a match AND we've not found a // higher value in our list -- so wrap cal.set(Calendar.MONTH, mByMonthList.get(0) - 1); cal.add(Calendar.YEAR, 1); return false; // must re-start checks } return true; }
From source file:com.zimbra.cs.mailbox.calendar.ZRecur.java
/** * @param cal//from w ww.j ava 2s. c om * @return */ private boolean checkHourList(GregorianCalendar cal) { if (mByHourList.size() > 0) { for (Integer cur : mByHourList) { int curHour = cal.get(Calendar.HOUR_OF_DAY); if (curHour == cur.intValue()) return true; // since the month list is in order, if we hit a HIGHER month, // then we know out current month isn't in the list, and therefore // we should go to this next one if (cur > curHour) { cal.set(Calendar.HOUR_OF_DAY, cur); return false; // must re-start checks } } // we've not found a match AND we've not found a // higher value in our list -- so wrap cal.set(Calendar.HOUR, mByHourList.get(0)); cal.add(Calendar.DAY_OF_YEAR, 1); return false; // must re-start checks } return true; }
From source file:com.zimbra.cs.mailbox.calendar.ZRecur.java
private boolean checkMonthDayList(GregorianCalendar cal) { if (mByMonthDayList.size() > 0) { for (Integer cur : mByMonthDayList) { int curMonthDay = cal.get(Calendar.DAY_OF_MONTH); if (cur == curMonthDay) return true; // since the list is in order, if we hit a HIGHER one, // then we know out current one isn't in the list, and therefore // we should go to this one we just found in the list if (cur > curMonthDay) { cal.set(Calendar.DAY_OF_MONTH, cur); return false; }//from w w w. j ava 2 s . c o m } // we've not found a match AND we've not found a // higher value in our list -- so wrap cal.set(Calendar.DAY_OF_MONTH, mByMonthDayList.get(0)); cal.add(Calendar.MONTH, 1); return false; } return true; }
From source file:com.zimbra.cs.mailbox.calendar.ZRecur.java
private boolean checkYearDayList(GregorianCalendar cal) { if (mByYearDayList.size() > 0) { for (Integer cur : mByYearDayList) { int curYearDay = cal.get(Calendar.DAY_OF_YEAR); if (cur == curYearDay) return true; // since the YearDay list is in order, if we hit a HIGHER one, // then we know out current one isn't in the list, and therefore // we should go to this one we just found in the list if (cur > curYearDay) { cal.set(Calendar.DAY_OF_YEAR, cur); return false; }//from w w w . jav a 2s . co m } // we've not found a match AND we've not found a // higher value in our list -- so wrap cal.set(Calendar.DAY_OF_YEAR, mByYearDayList.get(0)); cal.add(Calendar.YEAR, 1); return false; } return true; }
From source file:com.zimbra.cs.mailbox.calendar.ZRecur.java
/** * This version is for HOURLY/DAILY frequencies: it does NOT check the ordinal at all, * it only verifies that the day-of-the-week matches * * @param cal/*from ww w. j a v a 2 s . com*/ * @return */ private boolean checkDayList(GregorianCalendar cal) { assert (mFreq != Frequency.MONTHLY && mFreq != Frequency.YEARLY && mFreq != Frequency.WEEKLY); if (mByDayList.size() > 0) { for (ZWeekDayNum listCur : mByDayList) { int curDayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if (listCur.mDay.getCalendarDay() == curDayOfWeek) return true; // since the DayOfWeek list is in week-order, if we hit a HIGHER one, // then we know out current one isn't in the list, and therefore // we should go to this one we just found in the list if (listCur.mDay.getCalendarDay() > curDayOfWeek) { cal.set(Calendar.DAY_OF_WEEK, listCur.mDay.getCalendarDay()); return false; } } // we've not found a match AND we've not found a // higher value in our list -- so wrap cal.set(Calendar.DAY_OF_WEEK, mByDayList.get(0).mDay.getCalendarDay()); cal.add(Calendar.WEEK_OF_YEAR, 1); return false; } return true; }
From source file:biz.wolschon.finance.jgnucash.panels.TaxReportPanel.java
/** * @param aFile the file to write to/*from w w w. jav a2 s . co m*/ * @param aGran the granularity */ private void exportCSV(final File aFile, final ExportGranularities aGran) { //TODO: implement CSV-export FileWriter fw = null; try { fw = new FileWriter(aFile); // write headers List<TransactionSum> sums = mySums; fw.write("day"); for (TransactionSum transactionSum : sums) { fw.write(","); fw.write(transactionSum.getName()); } fw.write("\n"); // write data GregorianCalendar cal = new GregorianCalendar(); int add = aGran.getCalendarConstant(); DateFormat dateFormat = DateFormat.getDateInstance(); //NumberFormat numberFormat = NumberFormat.getInstance(); // we do NOT use getCurrencyInstance because it // contains a locale-specific currency-symbol for (int i = 0; i < 100; i++) { Date maxDate = cal.getTime(); fw.write(dateFormat.format(maxDate)); int transactionsCounted = 0; for (TransactionSum transactionSum : sums) { fw.write(","); try { transactionSum.setMaxDate(maxDate); FixedPointNumber value = transactionSum.getValue(); if (value != null) { //fw.write(numberFormat.format(value)); fw.write(value.toString()); } transactionsCounted += transactionSum.getTransactionsCounted(); } catch (JAXBException e) { LOGGER.error("Error calculating one of the TransactionSums", e); fw.write("ERROR"); } } fw.write("\n"); if (transactionsCounted == 0) { break; // we are walking back in time, // when there are no matching transactions, // all future runs will we a waste of time. // This happens often when add==YEAR } long old = cal.getTimeInMillis(); if (add == GregorianCalendar.MONTH) { cal.set(GregorianCalendar.DAY_OF_MONTH, 1); } if (add == GregorianCalendar.YEAR) { cal.set(GregorianCalendar.DAY_OF_MONTH, 1); cal.set(GregorianCalendar.MONTH, 1); } // usually we are in the middle of a month, // so do not skip the first day of the current // month if (i != 0 || old == cal.getTimeInMillis() || add == GregorianCalendar.DAY_OF_MONTH) { cal.add(add, -1); } } fw.close(); fw = null; } catch (IOException e) { LOGGER.error("cannot write csv-file", e); JOptionPane.showMessageDialog(this, "Cannot write CSV-file\n" + e.getMessage()); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { LOGGER.error("cannot close csv-file", e); } } for (TransactionSum transactionSum : mySums) { try { transactionSum.setMaxDate(null); } catch (JAXBException e) { LOGGER.error("cannot set maxDate back to it's original value", e); } } } }