List of usage examples for android.text.format Time setJulianDay
public long setJulianDay(int julianDay)
Sets the time from the given Julian day number, which must be based on the same timezone that is set in this Time object.
From source file:com.dgsd.android.ShiftTracker.Service.ShiftListWidgetService.java
private static int adjustJulianDay(int startWeekday, int jd) { Time time = new Time(); time.setJulianDay(jd); if (time.weekDay == startWeekday) { //Great, no adjustment needed return jd; } else {/*from w w w. j a va 2 s . c om*/ while (time.weekDay != startWeekday) time.setJulianDay(jd--); return TimeUtils.getJulianDay(time); } }
From source file:com.joeyturczak.jtscanner.utils.Utility.java
public static long normalizeDate(long startDate) { // normalize the start date to the beginning of the (UTC) day Time time = new Time(); time.set(startDate);/*from w w w . j ava2 s .c o m*/ int julianDay = Time.getJulianDay(startDate, time.gmtoff); return time.setJulianDay(julianDay); }
From source file:com.dgsd.android.ShiftTracker.Fragment.HoursAndIncomeSummaryFragment.java
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_hour_and_income_summary, null); mMonth = (TextView) v.findViewById(R.id.month); mThreeMonth = (TextView) v.findViewById(R.id.three_months); mSixMonth = (TextView) v.findViewById(R.id.six_months); mNineMonth = (TextView) v.findViewById(R.id.nine_months); mThisYear = (TextView) v.findViewById(R.id.year); AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); b.setView(v);//from w ww. j av a 2 s . c om Time time = new Time(); time.setJulianDay(mJulianDay); b.setTitle(DateFormat.getDateFormat(getActivity()).format(time.toMillis(true))); Dialog d = b.create(); d.setCanceledOnTouchOutside(true); return d; }
From source file:com.dgsd.android.ShiftTracker.Fragment.HoursAndIncomeSummaryFragment.java
@Override public Loader<Cursor> onCreateLoader(int id, Bundle bundle) { Time time = new Time(); time.setJulianDay(mJulianDay); switch (id) { case LOADER_ID_MONTH: { time.month--;//from w w w.j a va 2 s. c om break; } case LOADER_ID_3_MONTH: { time.month -= 3; break; } case LOADER_ID_6_MONTH: { time.month -= 6; break; } case LOADER_ID_9_MONTH: { time.month -= 9; break; } case LOADER_ID_YEAR: time.year--; break; } time.normalize(true); return getLoaderBetween(TimeUtils.getJulianDay(time), mJulianDay); }
From source file:com.dgsd.android.ShiftTracker.MainActivity.java
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.settings) { startActivity(new Intent(this, SettingsActivity.class)); } else if (item.getItemId() == R.id.go_to) { if (mGoToFragment != null && mGoToFragment.isResumed()) { //We're showing already! return true; }//from ww w . j a v a 2 s. co m final int centerJd = mWeekPagerAdapter.getJulianDayForPosition(mWeekPagerAdapter.getCenterPosition()); final int count = mWeekPagerAdapter.getCount() * 7; final Time time = new Time(); time.setJulianDay(centerJd - (count / 2)); final long min = time.toMillis(true); time.setJulianDay(centerJd + (count / 2)); final long max = time.toMillis(true); mGoToFragment = DatePickerFragment.newInstance("Go to date..", min, max, -1); mGoToFragment.setOnDateSelectedListener(this); mGoToFragment.show(getSupportFragmentManager(), null); } else if (item.getItemId() == R.id.get_full_version) { Uri uri = Uri.parse("market://details?id=com.dgsd.android.ShiftTracker"); startActivity(new Intent(Intent.ACTION_VIEW, uri)); } else if (item.getItemId() == R.id.stats) { final int selectedItem = getSupportActionBar().getSelectedNavigationIndex(); if (StApp.isFreeApp(this)) { if (mLinkToPaidAppFragment == null || !mLinkToPaidAppFragment.isResumed()) { mLinkToPaidAppFragment = LinkToPaidAppFragment .newInstance(getString(R.string.summary_unavailable_message)); mLinkToPaidAppFragment.show(getSupportFragmentManager(), null); } } else { if (mHoursAndIncomeFragment == null || !mHoursAndIncomeFragment.isResumed()) { final int jd; if (selectedItem == NAV_INDEX_MONTH) jd = mMonthPagerAdapter.getSelectedJulianDay(mPager.getCurrentItem()); else if (selectedItem == NAV_INDEX_WEEK) jd = mWeekPagerAdapter.getJulianDayForPosition(mPager.getCurrentItem()) + 6; else jd = TimeUtils.getCurrentJulianDay(); mHoursAndIncomeFragment = HoursAndIncomeSummaryFragment.newInstance(jd); mHoursAndIncomeFragment.show(getSupportFragmentManager(), null); } } } return super.onOptionsItemSelected(item); }
From source file:com.android.calendar.agenda.AgendaFragment.java
@Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int julianDay = mAgendaListView .getJulianDayFromPosition(firstVisibleItem - mAgendaListView.getHeaderViewsCount()); // On error - leave the old view if (julianDay == 0) { return;//from ww w . ja v a 2s .c om } // If the day changed, update the ActionBar if (mJulianDayOnTop != julianDay) { mJulianDayOnTop = julianDay; Time t = new Time(mTimeZone); t.setJulianDay(mJulianDayOnTop); mController.setTime(t.toMillis(true)); // Cannot sent a message that eventually may change the layout of the views // so instead post a runnable that will run when the layout is done if (!mIsTabletConfig) { view.post(new Runnable() { @Override public void run() { Time t = new Time(mTimeZone); t.setJulianDay(mJulianDayOnTop); mController.sendEvent(this, EventType.UPDATE_TITLE, t, t, null, -1, ViewType.CURRENT, 0, null, null); } }); } } }
From source file:com.dgsd.android.ShiftTracker.Fragment.EditShiftFragment.java
@Override public void onClick(View view) { final int id = view.getId(); if (id == R.id.start_date || id == R.id.end_date) { if (mDateDialog != null && mDateDialog.isResumed()) return; //We're showing already! final int centerJd = TimeUtils.getCurrentJulianDay(); final int count = 104 * 7; // 2 years final Time time = new Time(); time.setJulianDay(centerJd - (count / 2)); final long min = time.toMillis(true); time.setJulianDay(centerJd + (count / 2)); final long max = time.toMillis(true); final Integer date; final int type; if (id == R.id.start_date) { date = (Integer) mStartDate.getTag(); type = TYPE_CODE_START;/*w w w. j av a 2s . com*/ } else { date = (Integer) mEndDate.getTag(); type = TYPE_CODE_END; } mDateDialog = DatePickerFragment.newInstance("Date of shift", min, max, date == null ? -1 : date, type); mDateDialog.setOnDateSelectedListener(this); mDateDialog.show(getSherlockActivity().getSupportFragmentManager(), null); } else if (id == R.id.end_time) { mLastTimeSelected = LastTimeSelected.END; if (mTimeDialog != null && mTimeDialog.isResumed()) return; //We're showing already! long time = mEndTime.getTag() == null ? -1 : (Long) mEndTime.getTag(); mTimeDialog = TimePickerFragment.newInstance(time); mTimeDialog.setOnTimeSelectedListener(this); mTimeDialog.show(getSherlockActivity().getSupportFragmentManager(), null); } else if (id == R.id.start_time) { mLastTimeSelected = LastTimeSelected.START; if (mTimeDialog != null && mTimeDialog.isResumed()) return; //We're showing already! long time = mStartTime.getTag() == null ? -1 : (Long) mStartTime.getTag(); mTimeDialog = TimePickerFragment.newInstance(time); mTimeDialog.setOnTimeSelectedListener(this); mTimeDialog.show(getSherlockActivity().getSupportFragmentManager(), null); } }
From source file:co.carlosjimenez.android.currencyalerts.app.DetailActivityFragment.java
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { if (data == null) { Log.d(LOG_TAG, "Detail Forex Loader Finished: No data returned"); return;//from w ww . ja va 2s .c o m } if (!data.moveToFirst()) { Log.d(LOG_TAG, "Detail Forex Loader Finished: No data returned"); data.close(); return; } long lDate = 0; String sDate = ""; String sCurrency = ""; int i = 0; double dRateAverage = 0; double dVal = 0; double dMinVal = 0; double dMaxVal = 0; data.moveToPosition(0); mCurrencyFromId = data.getString(COL_CURRENCY_FROM_ID); String currencyFromName = data.getString(COL_CURRENCY_FROM_NAME); String currencyFromSymbol = data.getString(COL_CURRENCY_FROM_SYMBOL); String countryFromName = data.getString(COL_COUNTRY_FROM_NAME); double currencyFromRate = ForexContract.RateEntry.getRateFromUri(mUri); mCurrencyToId = data.getString(COL_CURRENCY_TO_ID); String currencyToName = data.getString(COL_CURRENCY_TO_NAME); String currencyToSymbol = data.getString(COL_CURRENCY_TO_SYMBOL); String countryToName = data.getString(COL_COUNTRY_TO_NAME); double currencyToRate = ForexContract.RateEntry.getRateFromUri(mUri) * data.getDouble(COL_RATE_VAL); Glide.with(getActivity()).load(data.getString(COL_COUNTRY_FROM_FLAG)).error(R.drawable.globe).crossFade() .into(mIvFlagFrom); mIvFlagFrom.setContentDescription(Utility.formatCountryFlagName(mContext, countryFromName)); Glide.with(getActivity()).load(data.getString(COL_COUNTRY_TO_FLAG)).error(R.drawable.globe).crossFade() .into(mIvFlagTo); mIvFlagFrom.setContentDescription(Utility.formatCountryFlagName(mContext, countryToName)); mTvCurrencyFromDesc.setText(currencyFromName); mTvCurrencyFromDesc.setContentDescription(currencyFromName); mTvCurrencyFromRate .setText(Utility.formatCurrencyRate(getActivity(), currencyFromSymbol, currencyFromRate)); mTvCurrencyFromRate.setContentDescription(String.valueOf(currencyFromRate) + " " + currencyFromName); mTvCurrencyToDesc.setText(currencyToName); mTvCurrencyToDesc.setContentDescription(currencyToName); mTvCurrencyToRate.setText(Utility.formatCurrencyRate(getActivity(), currencyToSymbol, currencyToRate)); mTvCurrencyToRate.setContentDescription(String.valueOf(currencyToRate) + " " + currencyToName); Time dayTime = new Time(); dayTime.setToNow(); int julianDate = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); dayTime = new Time(); long lMinDate = dayTime.setJulianDay(julianDate - DEFAULT_DAYS_FOREX_AVERAGE); sDate = Utility.getDateString(getActivity(), data.getLong(COL_RATE_DATE)); for (i = 0; i < data.getCount() && i < DEFAULT_DAYS_FOREX_AVERAGE; i++) { data.moveToPosition(i); lDate = data.getLong(COL_RATE_DATE); if (lDate < lMinDate) { break; } sCurrency = data.getString(COL_CURRENCY_TO_ID); sDate = Utility.getDateString(getActivity(), lDate); dVal = data.getDouble(COL_RATE_VAL); dRateAverage += dVal; if (i == 0) { dMinVal = dVal; dMaxVal = dVal; } else { dMinVal = dMinVal < dVal ? dMinVal : dVal; dMaxVal = dMaxVal > dVal ? dMaxVal : dVal; } } dRateAverage = dRateAverage / i; if (data.getCount() > 1) mTvPeriod.setText(data.getCount() + " days"); else mTvPeriod.setText(data.getCount() + " day"); mTvMaxRate.setContentDescription(mTvPeriod.getText()); mTvMaxRate.setText( Utility.formatCurrencyRate(getActivity(), data.getString(COL_CURRENCY_TO_SYMBOL), dMaxVal)); mTvMaxRate.setContentDescription(mTvMaxRate.getText()); mTvMinRate.setText( Utility.formatCurrencyRate(getActivity(), data.getString(COL_CURRENCY_TO_SYMBOL), dMinVal)); mTvMinRate.setContentDescription(mTvMinRate.getText()); mTvAverageRate.setText( Utility.formatCurrencyRate(getActivity(), data.getString(COL_CURRENCY_TO_SYMBOL), dRateAverage)); mTvAverageRate.setContentDescription(mTvAverageRate.getText()); // String text to share if user clicks on share menu icon mDisplayedRate = String.format("%s - %s %s = %s %s", sDate, currencyFromRate, mCurrencyFromId, currencyToRate, mCurrencyToId); mDisplayedCurrencyIds = mCurrencyFromId + "-" + mCurrencyToId; mDisplayedCurrencyNames = currencyToName; mContext.supportStartPostponedEnterTransition(); if (null != mToolbar) { Menu menu = mToolbar.getMenu(); if (null != menu) menu.clear(); mToolbar.inflateMenu(R.menu.detailfragment); finishCreatingMenu(mToolbar.getMenu()); } }
From source file:co.carlosjimenez.android.currencyalerts.app.sync.ForexSyncAdapter.java
/** * Take the String representing the complete forex in JSON Format and * pull out the data we need to construct the Strings needed for the wireframes. * <p/>//from ww w. java2 s . c om * Fortunately parsing is easy: constructor takes the JSON string and converts it * into an Object hierarchy for us. */ private void getForexDataFromJson(String forexJsonStr, String currencyQuery) throws JSONException { // Now we have a String representing the complete forex in JSON Format. // Fortunately parsing is easy: constructor takes the JSON string and converts it // into an Object hierarchy for us. // These are the names of the JSON objects that need to be extracted. final String OWM_RESULT = "results"; final String OWM_RATE_FROM = "fr"; final String OWM_RATE_TO = "to"; final String OWM_RATE = "val"; String[] currencies; boolean alertAvailable = false; try { if (mAlertData != null && mAlertData.getCurrencyFrom() != null && mAlertData.getCurrencyTo() != null) { alertAvailable = true; } JSONObject forexJson = new JSONObject(forexJsonStr); // do we have an error? if (!forexJson.has(OWM_RESULT)) { setForexStatus(getContext(), FOREX_STATUS_INVALID); return; } currencies = currencyQuery.split(","); JSONObject forexResultObject = forexJson.getJSONObject(OWM_RESULT); // OWM returns daily rates based upon the local time of the city that is being // asked for, which means that we need to know the GMT offset to translate this data // properly. // Since this data is also sent in-order and the first day is always the // current day, we're going to take advantage of that to get a nice // normalized UTC date for all of our rates. Time dayTime = new Time(); dayTime.setToNow(); // we start at the day returned by local time. Otherwise this is a mess. int julianDate = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); // now we work exclusively in UTC dayTime = new Time(); long dateTime = dayTime.setJulianDay(julianDate); // Insert the new rates information into the database Vector<ContentValues> cVVector = new Vector<>(currencies.length); for (int i = 0; i < currencies.length; i++) { JSONObject currencyObject = forexResultObject.getJSONObject(currencies[i]); String rate_from = currencyObject.getString(OWM_RATE_FROM); String rate_to = currencyObject.getString(OWM_RATE_TO); double result = currencyObject.getDouble(OWM_RATE); ContentValues forexValues = new ContentValues(); forexValues.put(ForexContract.RateEntry.COLUMN_RATE_FROM_KEY, rate_from); forexValues.put(ForexContract.RateEntry.COLUMN_RATE_TO_KEY, rate_to); forexValues.put(ForexContract.RateEntry.COLUMN_RATE_DATE, dateTime); forexValues.put(ForexContract.RateEntry.COLUMN_RATE_VALUE, result); if (alertAvailable && mAlertData.getCurrencyFrom().getId().equals(rate_from) && mAlertData.getCurrencyTo().getId().equals(rate_to)) { mCurrentAlertRate = result; } cVVector.add(forexValues); } int inserted = 0; // add to database if (cVVector.size() > 0) { ContentValues[] cvArray = new ContentValues[cVVector.size()]; cVVector.toArray(cvArray); getContext().getContentResolver().bulkInsert(ForexContract.RateEntry.CONTENT_URI, cvArray); // delete old data so we don't build up an endless history getContext().getContentResolver().delete(ForexContract.RateEntry.CONTENT_URI, ForexContract.RateEntry.COLUMN_RATE_DATE + " <= ?", new String[] { Long.toString(dayTime.setJulianDay(julianDate - FOREX_DAYS_TO_KEEP)) }); setForexSyncDate(getContext(), System.currentTimeMillis()); sendSyncBroadcast(FOREX_STATUS_OK); checkCurrencyData(); } Log.d(LOG_TAG, "ForexSyncAdapter: Sync Complete. " + cVVector.size() + " Inserted"); setForexStatus(getContext(), FOREX_STATUS_OK); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); setForexStatus(getContext(), FOREX_STATUS_SERVER_INVALID); } }
From source file:com.example.mihai.inforoute.app.FetchWeatherTask.java
private void getWeatherDataFromJson(String forecastJsonStr, String locationSetting) throws JSONException { final String OWM_CITY = "city"; final String OWM_CITY_NAME = "name"; final String OWM_COORD = "coord"; // Location coordinate final String OWM_LATITUDE = "lat"; final String OWM_LONGITUDE = "lon"; // Weather information. Each day's forecast info is an element of the "list" array. final String OWM_LIST = "list"; final String OWM_PRESSURE = "pressure"; final String OWM_HUMIDITY = "humidity"; final String OWM_WINDSPEED = "speed"; final String OWM_WIND_DIRECTION = "deg"; // All temperatures are children of the "temp" object. final String OWM_TEMPERATURE = "temp"; final String OWM_MAX = "max"; final String OWM_MIN = "min"; final String OWM_WEATHER = "weather"; final String OWM_DESCRIPTION = "main"; final String OWM_WEATHER_ID = "id"; try {//w w w . ja v a 2 s.c o m JSONObject forecastJson = new JSONObject(forecastJsonStr); JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY); String cityName = cityJson.getString(OWM_CITY_NAME); JSONObject cityCoord = cityJson.getJSONObject(OWM_COORD); double cityLatitude = cityCoord.getDouble(OWM_LATITUDE); double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE); long locationId = addLocation(locationSetting, cityName, cityLatitude, cityLongitude); // Insert the new weather information into the database Vector<ContentValues> cVVector = new Vector<ContentValues>(weatherArray.length()); Time dayTime = new Time(); dayTime.setToNow(); // we start at the day returned by local time. Otherwise this is a mess. int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); // now we work exclusively in UTC dayTime = new Time(); for (int i = 0; i < weatherArray.length(); i++) { // These are the values that will be collected. long dateTime; double pressure; int humidity; double windSpeed; double windDirection; double high; double low; String description; int weatherId; // Get the JSON object representing the day JSONObject dayForecast = weatherArray.getJSONObject(i); // Cheating to convert this to UTC time, which is what we want anyhow dateTime = dayTime.setJulianDay(julianStartDay + i); pressure = dayForecast.getDouble(OWM_PRESSURE); humidity = dayForecast.getInt(OWM_HUMIDITY); windSpeed = dayForecast.getDouble(OWM_WINDSPEED); windDirection = dayForecast.getDouble(OWM_WIND_DIRECTION); // Description is in a child array called "weather", which is 1 element long. // That element also contains a weather code. JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0); description = weatherObject.getString(OWM_DESCRIPTION); weatherId = weatherObject.getInt(OWM_WEATHER_ID); // Temperatures are in a child object called "temp". Try not to name variables // "temp" when working with temperature. It confuses everybody. JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE); high = temperatureObject.getDouble(OWM_MAX); low = temperatureObject.getDouble(OWM_MIN); ContentValues weatherValues = new ContentValues(); weatherValues.put(WeatherEntry.COLUMN_LOC_KEY, locationId); weatherValues.put(WeatherEntry.COLUMN_DATE, dateTime); weatherValues.put(WeatherEntry.COLUMN_HUMIDITY, humidity); weatherValues.put(WeatherEntry.COLUMN_PRESSURE, pressure); weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED, windSpeed); weatherValues.put(WeatherEntry.COLUMN_DEGREES, windDirection); weatherValues.put(WeatherEntry.COLUMN_MAX_TEMP, high); weatherValues.put(WeatherEntry.COLUMN_MIN_TEMP, low); weatherValues.put(WeatherEntry.COLUMN_SHORT_DESC, description); weatherValues.put(WeatherEntry.COLUMN_WEATHER_ID, weatherId); cVVector.add(weatherValues); } int inserted = 0; // add to database if (cVVector.size() > 0) { ContentValues[] cvArray = new ContentValues[cVVector.size()]; cVVector.toArray(cvArray); inserted = mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, cvArray); } Log.d(LOG_TAG, "FetchWeatherTask Complete. " + inserted + " Inserted"); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } }