public String toString() { StringBuilder sb = new StringBuilder("\nPower Profile Constraints for: ").append(powerProfileID); sb.append("\nnotification time= ") .append(CalendarUtil.toSecondString(contraintTimeNotification)); sb.append("\nstart after= ").append(CalendarUtil.toSecondString(getStartAfterConstraint())); sb.append("\nstop before= ").append(CalendarUtil.toSecondString(getStopBeforeConstraint())); sb.append('\n'); return sb.toString(); }
public void testSetToFirstDayOfMonth() { // Start in middle of month { Date date = new Date(99, 5, 15); CalendarUtil.setToFirstDayOfMonth(date); assertTrue(CalendarUtil.isSameDate(new Date(99, 5, 1), date)); } // Start on first day of month { Date date = new Date(99, 5, 1); CalendarUtil.setToFirstDayOfMonth(date); assertTrue(CalendarUtil.isSameDate(new Date(99, 5, 1), date)); } }
public void testAddMonthsToDate() { Date start = new Date(99, 5, 15, 3, 30, 5); // Add 0 months CalendarUtil.addMonthsToDate(start, 0); assertEquals(new Date(99, 5, 15, 3, 30, 5), start); // Add 5 months CalendarUtil.addMonthsToDate(start, 5); assertEquals(new Date(99, 10, 15, 3, 30, 5), start); // Subtract 6 months CalendarUtil.addMonthsToDate(start, -6); assertEquals(new Date(99, 4, 15, 3, 30, 5), start); }
public void testAddDaysToDate() { Date start = new Date(99, 5, 15, 3, 30, 5); // Add 0 days CalendarUtil.addDaysToDate(start, 0); assertEquals(new Date(99, 5, 15, 3, 30, 5), start); // Add 5 days CalendarUtil.addDaysToDate(start, 5); assertEquals(new Date(99, 5, 20, 3, 30, 5), start); // Subtract 10 days CalendarUtil.addDaysToDate(start, -10); assertEquals(new Date(99, 5, 10, 3, 30, 5), start); }
/** * 读取日期类型的属性值 * * @param key 属性名 * @param defaultValue 缺省值 * @return 日期类型的属性值。如果未指定属性值,则返回缺省值 */ public Calendar getDate(String key, Calendar defaultValue) { String value = getProperty(key); if (value == null) { return defaultValue; } return CalendarUtil.parse(value, defaultValue); }
public void setTime(int year, int month, int day) { if (month > 12) return; if (day > 31) return; if (month <= 0 || year <= 0 || day <= 0) return; // 设置数据 yearPicker.setData(calendarUtil.getYears()); monthPicker.setData(calendarUtil.getMonths()); dayPicker.setData(calendarUtil.getDays(year, month - 1)); // 设置默认值为当前 yearPicker.setDefault(year - 1970); monthPicker.setDefault(month - 1); if ((day - 1) >= calendarUtil.getDays(year, month - 1).size()) { dayPicker.setDefault(calendarUtil.getDays(year, month - 1).size() - 1); } else { dayPicker.setDefault(day - 1); } }
public String toString() { StringBuilder sb = new StringBuilder("\nPower Profile State for: ").append(powerProfileId); sb.append("\nenergy phase ID = ").append(energyPhaseId); sb.append("\nremote controllable = " + isRemoteControllable); sb.append("\nprofile state = ").append(getNameOf(this)); sb.append("\nnotification time = ") .append(CalendarUtil.toSecondString(stateTimeNotification)); sb.append('\n'); return sb.toString(); }
public void testIsSameDate() { // Same date, same time { Date d0 = new Date(99, 5, 15, 3, 30, 5); Date d1 = new Date(99, 5, 15, 3, 30, 5); assertTrue(CalendarUtil.isSameDate(d0, d1)); } // Same date, different time { Date d0 = new Date(99, 5, 15, 3, 30, 5); Date d1 = new Date(99, 5, 15, 4, 20, 5); assertTrue(CalendarUtil.isSameDate(d0, d1)); } // Different date, same time { Date d0 = new Date(99, 5, 15, 3, 30, 5); Date d1 = new Date(99, 5, 18, 3, 30, 5); assertFalse(CalendarUtil.isSameDate(d0, d1)); } }
public String toString() { StringBuilder sb = new StringBuilder("\nPower Profile ID ").append(profileId); sb.append(profileCurrentState); sb.append(profileTimeConstraints); sb.append("\nProfile scheduled time = ") .append(CalendarUtil.toMinuteString(energyPhases[0].getScheduledSlot())); sb.append('\n'); for (int i = 0; i < energyPhases.length; ++i) { sb.append(energyPhases[i]); } sb.append('\n'); return sb.toString(); }
public void testCopyDate() { Date original = new Date(99, 5, 15, 3, 30, 5); // Copy the date Date copy = CalendarUtil.copyDate(original); assertEquals(original, copy); assertEquals(99, original.getYear()); // Mutate the copy copy.setYear(70); assertEquals(99, original.getYear()); assertEquals(70, copy.getYear()); }
public void testGetDaysBetween() { // Same date, same time { Date d0 = new Date(99, 5, 15, 3, 30, 5); Date d1 = new Date(99, 5, 15, 3, 30, 5); assertEquals(0, CalendarUtil.getDaysBetween(d0, d1)); } // Same date, different time { Date d0 = new Date(99, 5, 15, 3, 30, 5); Date d1 = new Date(99, 5, 15, 4, 20, 5); assertEquals(0, CalendarUtil.getDaysBetween(d0, d1)); } // Three days ahead, same time { Date d0 = new Date(99, 5, 15, 3, 30, 5); Date d1 = new Date(99, 5, 18, 3, 30, 5); assertEquals(3, CalendarUtil.getDaysBetween(d0, d1)); } // Three days ahead, different time { Date d0 = new Date(99, 5, 15, 3, 30, 5); Date d1 = new Date(99, 5, 18, 4, 20, 5); assertEquals(3, CalendarUtil.getDaysBetween(d0, d1)); } // Three days behind, sametime { Date d0 = new Date(99, 5, 15, 3, 30, 5); Date d1 = new Date(99, 5, 12, 3, 30, 5); assertEquals(-3, CalendarUtil.getDaysBetween(d0, d1)); } }
public EnergyPhaseScheduleTime[] getProposedScheduledEnergyPhases() { int currentTimeSlot = CalendarUtil.getSlotOf(Calendar.getInstance()); int delay = energyPhases[0].getScheduledSlot() - currentTimeSlot; if (delay < -TIME_TOLERANCE_EQUALITY) throw new IllegalStateException("Obsolete Schedule: Start Time << Current Time."); if (delay < 0) delay = 0; EnergyPhaseScheduleTime[] proposedSchedule = new EnergyPhaseScheduleTime[energyPhases.length]; proposedSchedule[0] = new EnergyPhaseScheduleTime(energyPhases[0].getEnergyPhaseID(), delay); for (int i = 1; i < energyPhases.length; ++i) { proposedSchedule[i] = new EnergyPhaseScheduleTime( energyPhases[i].getEnergyPhaseID(), energyPhases[i].getScheduledSlot()); } return proposedSchedule; }
private void SetAlarmByDate(Calendar settingCal, String type) { // 時間をセット Calendar calendar = settingCal; // 秒は0にしとく calendar.set(Calendar.SECOND, 0); // context appContext = MyApplication.getAppContext(); Intent intent = new Intent(appContext, AlarmBroadcastReceiver.class); // intentにidを渡す intent.putExtra("intentId", GetIntentId()); intent.putExtra("alarmType", type); PendingIntent pending = PendingIntent.getBroadcast(appContext, GetIntentId(), intent, 0); // アラームをセットする AlarmManager am = (AlarmManager) appContext.getSystemService(appContext.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending); LogUtil.LogString("set alarm" + CalendarUtil.GetCalendarInfo(calendar)); Toast.makeText(MyApplication.getAppContext(), "SetAlarm", Toast.LENGTH_LONG).show(); if (type == "YABA") { yabaCalendarDate = settingCal; } }
/** * 读取日期类型的属性值 * * @param key 属性名 * @return 日期类型的属性值。如果未指定属性值,则返回 null */ public Calendar getDate(String key) { String value = getProperty(key); return CalendarUtil.parse(value); }
/** * 日期选择控件 * * @author suzhenpeng */ public class DatePicker extends LinearLayout { /** 滑动控件 */ private ScrollerNumberPicker yearPicker; private ScrollerNumberPicker monthPicker; private ScrollerNumberPicker dayPicker; /** 日历 */ private CalendarUtil calendarUtil = CalendarUtil.getSingleton(); /** 日历 */ public Calendar calendar; /** 临时日期 */ private int tempYearIndex = -1; private int tempMonthIndex = -1; /** 选择监听 */ private OnSelectingListener onSelectingListener; /** 刷新界面 */ private static final int REFRESH_VIEW = 0x001; public DatePicker(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public DatePicker(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onFinishInflate() { // TODO Auto-generated method stub super.onFinishInflate(); LayoutInflater.from(getContext()).inflate(R.layout.su_date_picker, this); // 初始化日历 calendar = Calendar.getInstance(); calendarUtil = CalendarUtil.getSingleton(); // 获取控件引用 yearPicker = (ScrollerNumberPicker) findViewById(R.id.year); monthPicker = (ScrollerNumberPicker) findViewById(R.id.month); dayPicker = (ScrollerNumberPicker) findViewById(R.id.day); // 获取当前年月日 int y = calendar.get(Calendar.YEAR); int m = calendar.get(Calendar.MONTH); int d = calendar.get(Calendar.DAY_OF_MONTH); // 设置数据 yearPicker.setData(calendarUtil.getYears()); monthPicker.setData(calendarUtil.getMonths()); dayPicker.setData(calendarUtil.getDays(y, m)); // 设置默认值为当前 yearPicker.setDefault(y - 1970); monthPicker.setDefault(m); dayPicker.setDefault(d - 1); yearPicker.setOnSelectListener( new OnSelectListener() { @Override public void endSelect(int id, String text) { // TODO Auto-generated method stub if (text == null || text.equals("")) return; if (tempYearIndex != id) { String selectDay = dayPicker.getSelectedText(); if (selectDay == null || selectDay.equals("")) return; String selectMonth = monthPicker.getSelectedText(); if (selectMonth == null || selectMonth.equals("")) return; dayPicker.setData( calendarUtil.getDays(Integer.valueOf(text), Integer.valueOf(selectMonth) - 1)); int lastDay = Integer.valueOf(dayPicker.getItemText(dayPicker.getListSize() - 1)); if (Integer.valueOf(selectDay) > lastDay) { dayPicker.setDefault(lastDay - 1); } else { dayPicker.setDefault(Integer.valueOf(selectDay) - 1); } } tempYearIndex = id; Message message = new Message(); message.what = REFRESH_VIEW; handler.sendMessage(message); } @Override public void selecting(int id, String text) { // TODO Auto-generated method stub } }); monthPicker.setOnSelectListener( new OnSelectListener() { @Override public void endSelect(int id, String text) { // TODO Auto-generated method stub if (text.equals("") || text == null) return; if (tempMonthIndex != id) { String selectDay = dayPicker.getSelectedText(); if (selectDay == null || selectDay.equals("")) return; String selectYear = yearPicker.getSelectedText(); if (selectYear == null || selectYear.equals("")) return; dayPicker.setData( calendarUtil.getDays(Integer.valueOf(selectYear), Integer.valueOf(text) - 1)); int lastDay = Integer.valueOf(dayPicker.getItemText(dayPicker.getListSize() - 1)); if (Integer.valueOf(selectDay) > lastDay) { dayPicker.setDefault(lastDay - 1); } else { dayPicker.setDefault(Integer.valueOf(selectDay) - 1); } } tempMonthIndex = id; Message message = new Message(); message.what = REFRESH_VIEW; handler.sendMessage(message); } @Override public void selecting(int id, String text) { // TODO Auto-generated method stub } }); dayPicker.setOnSelectListener( new OnSelectListener() { @Override public void selecting(int id, String text) { // TODO Auto-generated method stub } @Override public void endSelect(int id, String text) { // TODO Auto-generated method stub Message message = new Message(); message.what = REFRESH_VIEW; handler.sendMessage(message); } }); } public void setTime(long time) { setDate(new Date(time)); } public void setDate(Date date) { int year; if (date.getYear() < 70) { year = (70 + 1900); } else { year = (date.getYear() + 1900); } setTime(year, date.getMonth() + 1, date.getDay() + 1); } public void setTime(int year, int month, int day) { if (month > 12) return; if (day > 31) return; if (month <= 0 || year <= 0 || day <= 0) return; // 设置数据 yearPicker.setData(calendarUtil.getYears()); monthPicker.setData(calendarUtil.getMonths()); dayPicker.setData(calendarUtil.getDays(year, month - 1)); // 设置默认值为当前 yearPicker.setDefault(year - 1970); monthPicker.setDefault(month - 1); if ((day - 1) >= calendarUtil.getDays(year, month - 1).size()) { dayPicker.setDefault(calendarUtil.getDays(year, month - 1).size() - 1); } else { dayPicker.setDefault(day - 1); } } /** * 获取时间 * * @return */ public Date getDate() { String timeString = yearPicker.getSelectedText() + "-" + monthPicker.getSelectedText() + "-" + dayPicker.getSelectedText(); Date date = new Date(Long.valueOf(formatToTime(timeString, "yyyy-MM-dd"))); return date; } /** * 获取时间戳 * * @return */ public long getTime() { return getDate().getTime(); } public int getYear() { return Integer.valueOf(yearPicker.getSelectedText()); } public int getMonth() { return Integer.valueOf(monthPicker.getSelectedText()); } public int getDay() { return Integer.valueOf(dayPicker.getSelectedText()); } public String getFormaTime() { return (yearPicker.getSelectedText() + "-" + monthPicker.getSelectedText() + "-" + dayPicker.getSelectedText()); } public String getFormatTime(String format) { return getFormatTime(getTime(), format); } public void setOnSelectingListener(OnSelectingListener onSelectingListener) { this.onSelectingListener = onSelectingListener; } /** * 从格式化时间获取time * * @param time * @param format * @return */ public static String formatToTime(String time, String format) { Date date = null; SimpleDateFormat formatTime = new SimpleDateFormat(format, Locale.CHINA); try { try { date = formatTime.parse(time); } catch (java.text.ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return String.valueOf(date.getTime()); } /** * 返回指定格式时间 * * @param time * @param format * @return */ public static String getFormatTime(long time, String format) { SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA); return dateFormat.format(time); } @SuppressLint("HandlerLeak") Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case REFRESH_VIEW: if (onSelectingListener != null) onSelectingListener.selected(true); break; default: break; } } }; /** * 日期监听 * * @author suzhenpeng */ public interface OnSelectingListener { public void selected(boolean selected); } }
@Override protected void onFinishInflate() { // TODO Auto-generated method stub super.onFinishInflate(); LayoutInflater.from(getContext()).inflate(R.layout.su_date_picker, this); // 初始化日历 calendar = Calendar.getInstance(); calendarUtil = CalendarUtil.getSingleton(); // 获取控件引用 yearPicker = (ScrollerNumberPicker) findViewById(R.id.year); monthPicker = (ScrollerNumberPicker) findViewById(R.id.month); dayPicker = (ScrollerNumberPicker) findViewById(R.id.day); // 获取当前年月日 int y = calendar.get(Calendar.YEAR); int m = calendar.get(Calendar.MONTH); int d = calendar.get(Calendar.DAY_OF_MONTH); // 设置数据 yearPicker.setData(calendarUtil.getYears()); monthPicker.setData(calendarUtil.getMonths()); dayPicker.setData(calendarUtil.getDays(y, m)); // 设置默认值为当前 yearPicker.setDefault(y - 1970); monthPicker.setDefault(m); dayPicker.setDefault(d - 1); yearPicker.setOnSelectListener( new OnSelectListener() { @Override public void endSelect(int id, String text) { // TODO Auto-generated method stub if (text == null || text.equals("")) return; if (tempYearIndex != id) { String selectDay = dayPicker.getSelectedText(); if (selectDay == null || selectDay.equals("")) return; String selectMonth = monthPicker.getSelectedText(); if (selectMonth == null || selectMonth.equals("")) return; dayPicker.setData( calendarUtil.getDays(Integer.valueOf(text), Integer.valueOf(selectMonth) - 1)); int lastDay = Integer.valueOf(dayPicker.getItemText(dayPicker.getListSize() - 1)); if (Integer.valueOf(selectDay) > lastDay) { dayPicker.setDefault(lastDay - 1); } else { dayPicker.setDefault(Integer.valueOf(selectDay) - 1); } } tempYearIndex = id; Message message = new Message(); message.what = REFRESH_VIEW; handler.sendMessage(message); } @Override public void selecting(int id, String text) { // TODO Auto-generated method stub } }); monthPicker.setOnSelectListener( new OnSelectListener() { @Override public void endSelect(int id, String text) { // TODO Auto-generated method stub if (text.equals("") || text == null) return; if (tempMonthIndex != id) { String selectDay = dayPicker.getSelectedText(); if (selectDay == null || selectDay.equals("")) return; String selectYear = yearPicker.getSelectedText(); if (selectYear == null || selectYear.equals("")) return; dayPicker.setData( calendarUtil.getDays(Integer.valueOf(selectYear), Integer.valueOf(text) - 1)); int lastDay = Integer.valueOf(dayPicker.getItemText(dayPicker.getListSize() - 1)); if (Integer.valueOf(selectDay) > lastDay) { dayPicker.setDefault(lastDay - 1); } else { dayPicker.setDefault(Integer.valueOf(selectDay) - 1); } } tempMonthIndex = id; Message message = new Message(); message.what = REFRESH_VIEW; handler.sendMessage(message); } @Override public void selecting(int id, String text) { // TODO Auto-generated method stub } }); dayPicker.setOnSelectListener( new OnSelectListener() { @Override public void selecting(int id, String text) { // TODO Auto-generated method stub } @Override public void endSelect(int id, String text) { // TODO Auto-generated method stub Message message = new Message(); message.what = REFRESH_VIEW; handler.sendMessage(message); } }); }
private Date getStartDate() { Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 1); CalendarUtil.setToMidnight(c); return new Date(c.getTimeInMillis()); }
@Override public void onResume() { super.onResume(); final ListView budgetList = (ListView) findViewById(R.id.list); final TextView helpText = (TextView) findViewById(R.id.helpText); DBHelper db = new DBHelper(this); if (db.getBudgetCount() > 0) { budgetList.setVisibility(View.VISIBLE); helpText.setVisibility(View.GONE); } else { budgetList.setVisibility(View.GONE); helpText.setVisibility(View.VISIBLE); helpText.setText(R.string.noBudgets); } final Calendar date = Calendar.getInstance(); // Set to the last ms at the end of the month final long dateMonthEndMs = CalendarUtil.getEndOfMonthMs(date.get(Calendar.YEAR), date.get(Calendar.MONTH)); // Set to beginning of the month final long dateMonthStartMs = CalendarUtil.getStartOfMonthMs(date.get(Calendar.YEAR), date.get(Calendar.MONTH)); final Bundle b = getIntent().getExtras(); final long budgetStartMs = b != null ? b.getLong("budgetStart", dateMonthStartMs) : dateMonthStartMs; final long budgetEndMs = b != null ? b.getLong("budgetEnd", dateMonthEndMs) : dateMonthEndMs; date.setTimeInMillis(budgetStartMs); String budgetStartString = DateFormat.getDateInstance(DateFormat.SHORT).format(date.getTime()); date.setTimeInMillis(budgetEndMs); String budgetEndString = DateFormat.getDateInstance(DateFormat.SHORT).format(date.getTime()); String dateRangeFormat = getResources().getString(R.string.dateRangeFormat); String dateRangeString = String.format(dateRangeFormat, budgetStartString, budgetEndString); final TextView dateRangeField = (TextView) findViewById(R.id.dateRange); dateRangeField.setText(dateRangeString); final List<Budget> budgets = db.getBudgets(budgetStartMs, budgetEndMs); final BudgetAdapter budgetListAdapter = new BudgetAdapter(this, budgets); budgetList.setAdapter(budgetListAdapter); budgetList.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Budget budget = (Budget) parent.getItemAtPosition(position); Intent i = new Intent(getApplicationContext(), BudgetViewActivity.class); Bundle bundle = new Bundle(); bundle.putString("id", budget.name); bundle.putBoolean("view", true); i.putExtras(bundle); startActivity(i); } }); }