Example usage for android.text.format DateFormat format

List of usage examples for android.text.format DateFormat format

Introduction

In this page you can find the example usage for android.text.format DateFormat format.

Prototype

public static CharSequence format(CharSequence inFormat, Calendar inDate) 

Source Link

Document

Given a format string and a java.util.Calendar object, returns a CharSequence containing the requested date.

Usage

From source file:com.balakrish.gpstracker.WaypointsListActivity.java

/**
 * Export waypoints to external file/*from ww  w.j  av a  2 s. c  o  m*/
 */
private void exportWaypoints() {

    Context mContext = this;

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.filename_dialog,
            (ViewGroup) findViewById(R.id.filename_dialog_layout_root));

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

    builder.setTitle(R.string.export_waypoints);
    builder.setView(layout);

    // final String defaultFilename = "wp_" + (new SimpleDateFormat("yyyy-MM-dd", Locale.US)).format((new
    // Date()).getTime());
    final String defaultFilename = "wp_" + (DateFormat.format("yyyy-MM-dd", (new Date()).getTime()));

    // creating references to input fields in order to use them in
    // onClick handler
    final EditText filenameEditText = (EditText) layout.findViewById(R.id.filenameInputText);
    filenameEditText.setText(defaultFilename);

    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {

            // waypoint title from input dialog
            String filenameStr = filenameEditText.getText().toString().trim();

            if (filenameStr.equals("")) {
                filenameStr = defaultFilename;
            }

            waypointToGpx = new WaypointGpxExportTask(WaypointsListActivity.this, filenameStr);
            waypointToGpx.setApp(app);
            waypointToGpx.execute(0L);

            dialog.dismiss();

        }
    });

    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            // dialog.dismiss();
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();

}

From source file:com.android.deskclock.alarms.AlarmStateManager.java

/**
 * Fix and update all alarm instance when a time change event occurs.
 *
 * @param context application context//from   ww w . j ava 2  s.  c o  m
 */
public static void fixAlarmInstances(Context context) {
    // Register all instances after major time changes or when phone restarts
    final ContentResolver contentResolver = context.getContentResolver();
    final Calendar currentTime = getCurrentTime();
    for (AlarmInstance instance : AlarmInstance.getInstances(contentResolver, null)) {
        final Alarm alarm = Alarm.getAlarm(contentResolver, instance.mAlarmId);
        if (alarm == null) {
            unregisterInstance(context, instance);
            AlarmInstance.deleteInstance(contentResolver, instance.mId);
            LogUtils.e("Found instance without matching alarm; deleting instance %s", instance);
            continue;
        }
        final Calendar priorAlarmTime = alarm.getPreviousAlarmTime(instance.getAlarmTime());
        final Calendar missedTTLTime = instance.getMissedTimeToLive();
        if (currentTime.before(priorAlarmTime) || currentTime.after(missedTTLTime)) {
            final Calendar oldAlarmTime = instance.getAlarmTime();
            final Calendar newAlarmTime = alarm.getNextAlarmTime(currentTime);
            final CharSequence oldTime = DateFormat.format("MM/dd/yyyy hh:mm a", oldAlarmTime);
            final CharSequence newTime = DateFormat.format("MM/dd/yyyy hh:mm a", newAlarmTime);
            LogUtils.i("A time change has caused an existing alarm scheduled to fire at %s to"
                    + " be replaced by a new alarm scheduled to fire at %s", oldTime, newTime);

            // The time change is so dramatic the AlarmInstance doesn't make any sense;
            // remove it and schedule the new appropriate instance.
            AlarmStateManager.deleteInstanceAndUpdateParent(context, instance);
        } else {
            registerInstance(context, instance, false);
        }
    }

    updateNextAlarm(context);
}

From source file:com.stasbar.knowyourself.alarms.AlarmStateManager.java

/**
 * Fix and update all alarm instance when a time change event occurs.
 *
 * @param context application context//w w w  .  jav a  2 s  .c om
 */
public static void fixAlarmInstances(Context context) {
    // Register all instances after major time changes or when phone restarts
    final ContentResolver contentResolver = context.getContentResolver();
    final Calendar currentTime = getCurrentTime();

    // Sort the instances in reverse chronological order so that later instances are fixed or
    // deleted before re-scheduling prior instances (which may re-create or update the later
    // instances).
    final List<AlarmInstance> instances = AlarmInstance.getInstances(contentResolver, null /* selection */);
    Collections.sort(instances, new Comparator<AlarmInstance>() {
        @Override
        public int compare(AlarmInstance lhs, AlarmInstance rhs) {
            return rhs.getAlarmTime().compareTo(lhs.getAlarmTime());
        }
    });

    for (AlarmInstance instance : instances) {
        final Alarm alarm = Alarm.getAlarm(contentResolver, instance.mAlarmId);
        if (alarm == null) {
            unregisterInstance(context, instance);
            AlarmInstance.deleteInstance(contentResolver, instance.mId);
            LogUtils.e("Found instance without matching alarm; deleting instance %s", instance);
            continue;
        }
        final Calendar priorAlarmTime = alarm.getPreviousAlarmTime(instance.getAlarmTime());
        final Calendar missedTTLTime = instance.getMissedTimeToLive();
        if (currentTime.before(priorAlarmTime) || currentTime.after(missedTTLTime)) {
            final Calendar oldAlarmTime = instance.getAlarmTime();
            final Calendar newAlarmTime = alarm.getNextAlarmTime(currentTime);
            final CharSequence oldTime = DateFormat.format("MM/dd/yyyy hh:mm a", oldAlarmTime);
            final CharSequence newTime = DateFormat.format("MM/dd/yyyy hh:mm a", newAlarmTime);
            LogUtils.i("A time change has caused an existing alarm scheduled to fire at %s to"
                    + " be replaced by a new alarm scheduled to fire at %s", oldTime, newTime);

            // The time change is so dramatic the AlarmInstance doesn't make any sense;
            // remove it and schedule the new appropriate instance.
            AlarmStateManager.deleteInstanceAndUpdateParent(context, instance);
        } else {
            registerInstance(context, instance, false /* updateNextAlarm */);
        }
    }

    updateNextAlarm(context);
}

From source file:com.androidinspain.deskclock.alarms.AlarmStateManager.java

/**
 * Fix and update all alarm instance when a time change event occurs.
 *
 * @param context application context// w  ww . java 2s  .co m
 */
public static void fixAlarmInstances(Context context) {
    LogUtils.i("Fixing alarm instances");
    // Register all instances after major time changes or when phone restarts
    final ContentResolver contentResolver = context.getContentResolver();
    final Calendar currentTime = getCurrentTime();

    // Sort the instances in reverse chronological order so that later instances are fixed or
    // deleted before re-scheduling prior instances (which may re-create or update the later
    // instances).
    final List<AlarmInstance> instances = AlarmInstance.getInstances(contentResolver, null /* selection */);
    Collections.sort(instances, new Comparator<AlarmInstance>() {
        @Override
        public int compare(AlarmInstance lhs, AlarmInstance rhs) {
            return rhs.getAlarmTime().compareTo(lhs.getAlarmTime());
        }
    });

    for (AlarmInstance instance : instances) {
        final Alarm alarm = Alarm.getAlarm(contentResolver, instance.mAlarmId);
        if (alarm == null) {
            unregisterInstance(context, instance);
            AlarmInstance.deleteInstance(contentResolver, instance.mId);
            LogUtils.e("Found instance without matching alarm; deleting instance %s", instance);
            continue;
        }
        final Calendar priorAlarmTime = alarm.getPreviousAlarmTime(instance.getAlarmTime());
        final Calendar missedTTLTime = instance.getMissedTimeToLive();
        if (currentTime.before(priorAlarmTime) || currentTime.after(missedTTLTime)) {
            final Calendar oldAlarmTime = instance.getAlarmTime();
            final Calendar newAlarmTime = alarm.getNextAlarmTime(currentTime);
            final CharSequence oldTime = DateFormat.format("MM/dd/yyyy hh:mm a", oldAlarmTime);
            final CharSequence newTime = DateFormat.format("MM/dd/yyyy hh:mm a", newAlarmTime);
            LogUtils.i("A time change has caused an existing alarm scheduled to fire at %s to"
                    + " be replaced by a new alarm scheduled to fire at %s", oldTime, newTime);

            // The time change is so dramatic the AlarmInstance doesn't make any sense;
            // remove it and schedule the new appropriate instance.
            AlarmStateManager.deleteInstanceAndUpdateParent(context, instance);
        } else {
            registerInstance(context, instance, false /* updateNextAlarm */);
        }
    }

    updateNextAlarm(context);
}

From source file:uk.co.droidinactu.ebooklauncher.EBookLauncherActivity.java

private void updateCurrentTime() {
    lblClock.setText(DateFormat.format("kk:mm:ss", new Date()));
}

From source file:com.iiordanov.runsoft.bVNC.RemoteCanvasActivity.java

public String getRegdateRelative() {
    String label = DateUtils.getRelativeTimeSpanString(System.currentTimeMillis()).toString();
    if (label.equals("0 ") || label.equals("0 ?")) {
        label = " ";
    }/*from w  ww  .  j  a  va  2  s  . com*/
    if (label.contains("")) {
        return DateFormat.format("yyyy.MM.dd. aahh:mm", System.currentTimeMillis()).toString();
    }
    return label;
}

From source file:edu.usf.cutr.opentripplanner.android.fragments.MainFragment.java

private void requestTrip() {
    LatLng mCurrentLatLng = getLastLocation();
    String startLocationString;/*www  . j  av a  2  s. c  om*/
    String endLocationString;

    Boolean isOriginMyLocation = mPrefs.getBoolean(OTPApp.PREFERENCE_KEY_ORIGIN_IS_MY_LOCATION, false);
    Boolean isDestinationMyLocation = mPrefs.getBoolean(OTPApp.PREFERENCE_KEY_DESTINATION_IS_MY_LOCATION,
            false);

    toggleItinerarySelectionSpinner(false);

    if (mRoute != null) {
        for (Polyline p : mRoute) {
            p.remove();
        }
        mRoute = null;
    }
    if (mModeMarkers != null) {
        for (Marker m : mModeMarkers) {
            m.remove();
        }
        mModeMarkers = null;
    }

    if (isOriginMyLocation && isDestinationMyLocation) {
        Toast.makeText(MainFragment.this.mApplicationContext,
                mApplicationContext.getResources().getString(
                        R.string.toast_tripplanner_origin_destination_are_mylocation),
                Toast.LENGTH_SHORT).show();
        return;
    } else if (isOriginMyLocation || isDestinationMyLocation) {
        if (mCurrentLatLng == null) {
            Toast.makeText(MainFragment.this.mApplicationContext,
                    mApplicationContext.getResources()
                            .getString(R.string.toast_tripplanner_current_location_error),
                    Toast.LENGTH_LONG).show();
            return;
        } else {
            if (isOriginMyLocation) {
                startLocationString = mCurrentLatLng.latitude + "," + mCurrentLatLng.longitude;
                if (mEndMarker == null) {
                    Toast.makeText(MainFragment.this.mApplicationContext,
                            mApplicationContext.getResources().getString(
                                    R.string.toast_tripplanner_need_to_place_markers_before_planning),
                            Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    endLocationString = mEndMarker.getPosition().latitude + ","
                            + mEndMarker.getPosition().longitude;
                }
            } else {
                endLocationString = mCurrentLatLng.latitude + "," + mCurrentLatLng.longitude;
                if (mStartMarker == null) {
                    Toast.makeText(MainFragment.this.mApplicationContext,
                            mApplicationContext.getResources().getString(
                                    R.string.toast_tripplanner_need_to_place_markers_before_planning),
                            Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    startLocationString = mStartMarker.getPosition().latitude + ","
                            + mStartMarker.getPosition().longitude;
                }
            }
        }
    } else {
        if ((mStartMarker == null) || (mEndMarker == null)) {
            Toast.makeText(MainFragment.this.mApplicationContext,
                    mApplicationContext.getResources()
                            .getString(R.string.toast_tripplanner_need_to_place_markers_before_planning),
                    Toast.LENGTH_SHORT).show();
            return;
        } else {
            startLocationString = mStartMarker.getPosition().latitude + ","
                    + mStartMarker.getPosition().longitude;
            endLocationString = mEndMarker.getPosition().latitude + "," + mEndMarker.getPosition().longitude;
        }
    }

    if (!mIsStartLocationGeocodingCompleted && !isOriginMyLocation) {
        Toast.makeText(MainFragment.this.mApplicationContext,
                mApplicationContext.getResources().getString(
                        R.string.toast_tripplanner_need_to_place_markers_before_planning),
                Toast.LENGTH_SHORT).show();
        return;
    } else if (!mIsEndLocationGeocodingCompleted && !isDestinationMyLocation) {
        Toast.makeText(MainFragment.this.mApplicationContext,
                mApplicationContext.getResources().getString(
                        R.string.toast_tripplanner_need_to_place_markers_before_planning),
                Toast.LENGTH_SHORT).show();
        return;
    }

    Request request = new Request();

    try {
        request.setFrom(URLEncoder.encode(startLocationString, OTPApp.URL_ENCODING));
        request.setTo(URLEncoder.encode(endLocationString, OTPApp.URL_ENCODING));
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }

    request.setArriveBy(mArriveBy);

    OptimizeSpinnerItem optimizeSpinnerItem = (OptimizeSpinnerItem) mDdlOptimization
            .getItemAtPosition(mDdlOptimization.getCheckedItemPosition());
    if (optimizeSpinnerItem == null) {
        optimizeSpinnerItem = (OptimizeSpinnerItem) mDdlOptimization.getItemAtPosition(0);
    }

    if (optimizeSpinnerItem != null) {
        request.setOptimize(optimizeSpinnerItem.getOptimizeType());
        if (optimizeSpinnerItem.getOptimizeType().equals(OptimizeType.TRIANGLE)) {
            request.setTriangleTimeFactor(mBikeTriangleMinValue);
            request.setTriangleSlopeFactor(mBikeTriangleMaxValue - mBikeTriangleMinValue);
            request.setTriangleSafetyFactor(1 - mBikeTriangleMaxValue);
        }
    } else {
        Log.e(OTPApp.TAG, "Optimization not found, not possible to add it to the request so, most"
                + "likely results will be incorrect");
    }

    TraverseModeSpinnerItem traverseModeSpinnerItem = (TraverseModeSpinnerItem) mDdlTravelMode
            .getItemAtPosition(mDdlTravelMode.getCheckedItemPosition());
    if (traverseModeSpinnerItem == null) {
        traverseModeSpinnerItem = (TraverseModeSpinnerItem) mDdlTravelMode.getItemAtPosition(0);
    }

    if (traverseModeSpinnerItem != null) {
        request.setModes(traverseModeSpinnerItem.getTraverseModeSet());
    } else {
        Log.e(OTPApp.TAG, "Traverse mode not found, not possible to add it to the request so, most"
                + "likely results will be incorrect");
    }

    Integer defaultMaxWalkInt = mApplicationContext.getResources().getInteger(R.integer.max_walking_distance);

    try {
        Double maxWalk = Double.parseDouble(
                mPrefs.getString(OTPApp.PREFERENCE_KEY_MAX_WALKING_DISTANCE, defaultMaxWalkInt.toString()));
        request.setMaxWalkDistance(maxWalk);
    } catch (NumberFormatException ex) {
        request.setMaxWalkDistance((double) defaultMaxWalkInt);
    }

    request.setWheelchair(mPrefs.getBoolean(OTPApp.PREFERENCE_KEY_WHEEL_ACCESSIBLE, false));

    Date requestTripDate;
    if (mTripDate == null) {
        requestTripDate = Calendar.getInstance().getTime();
    } else {
        requestTripDate = mTripDate;
    }

    request.setDateTime(
            DateFormat.format(OTPApp.FORMAT_OTP_SERVER_DATE_QUERY, requestTripDate.getTime()).toString(),
            DateFormat.format(OTPApp.FORMAT_OTP_SERVER_TIME_QUERY, requestTripDate.getTime()).toString());

    request.setShowIntermediateStops(Boolean.TRUE);

    WeakReference<Activity> weakContext = new WeakReference<Activity>(MainFragment.this.getActivity());

    new TripRequest(weakContext, MainFragment.this.mApplicationContext, mOTPApp.getSelectedServer(),
            MainFragment.this).execute(request);

    InputMethodManager imm = (InputMethodManager) MainFragment.this.getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mTbEndLocation.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(mTbStartLocation.getWindowToken(), 0);

    mTripDate = null;
}