时间选择器以毫秒为单位,以毫秒为单位取错值

时间:2017-05-31 09:38:09

标签: android firebase timer firebase-realtime-database

我正在使用时间选择器将时间存储在firebase中 但时间选择器以毫秒为单位选择错误的值。 如果我存储时间03:02 pm它将存储06:49:00。 如何使用时间选择器获得精确的毫秒?

public class EditTimeTable extends BaseActivity {

private EditText editTextaddress;
private TextView textviewday;
String day;
//private Spinner spinnerDay;
private Spinner spinnerSlot;
private Button buttonsave;
private TimePicker timePicker;
private static final String REQUIRED = "Required";
private static final String TAG = "time table activity";
//a list to store all the dr time data from firebase database
List<drtime> drtimes;

//our database reference object
private DatabaseReference databasedrtime;
//firebase auth object
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_time_table);


    Bundle bundle = getIntent().getExtras();
     day = bundle.getString("key", "");
    TextView TextViewday = (TextView) findViewById(R.id.post_day);
    TextViewday.setText(day);


    //getting the reference of drtime node
    databasedrtime = FirebaseDatabase.getInstance().getReference();
    //getting views

    editTextaddress = (EditText) findViewById(R.id.post_body);
    textviewday = (TextView) findViewById(R.id.post_day);
    //spinnerDay = (Spinner) findViewById(R.id.spinner2);
    spinnerSlot = (Spinner) findViewById(R.id.spinner);
    timePicker = (TimePicker) findViewById(R.id.timePicker);

    // listViewArtists = (ListView) findViewById(R.id.listViewArtists);

    buttonsave = (Button) findViewById(R.id.button4);


    //adding an onclicklistener to button
    buttonsave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            if (day.contentEquals("Monday") ) {
                adddrtime0();

        }
    });
}

private void adddrtime0() {
    //getting the values to save
    final String address = editTextaddress.getText().toString();
    final String day = textviewday.getText().toString();
    //final String day = spinnerDay.getSelectedItem().toString();
    final String slot = spinnerSlot.getSelectedItem().toString();
    final long time = timePicker.getDrawingTime();

    // address is required
    if (TextUtils.isEmpty(address)) {
        editTextaddress.setError(REQUIRED);
        return;
    }
    final String userId = getUid();
    databasedrtime.child("users").child(userId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            UserTime userTime = dataSnapshot.getValue(UserTime.class);
            if (userTime == null) {
                // User is null, error out
                Log.e(TAG, "User " + userId + " is unexpectedly null");
                Toast.makeText(EditTimeTable.this,
                        "Error: could not fetch user.",
                        Toast.LENGTH_SHORT).show();

            } else {
                // Write new post
                writetimetable(userId, userTime.username, address, day, slot, time);

                finish();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
        }
    });


}


private void writetimetable(String userId, String username, String address, String day, String slot, long time) {
    // Create new post at /user-posts/$userid/$postid and at
    // /posts/$postid simultaneously
    String key = databasedrtime.child("Monday").getKey();
    drPost dr = new drPost(userId, username, address, day, slot, time);
    Map<String, Object> postValues = dr.toMap();

    Map<String, Object> childUpdates = new HashMap<>();


    childUpdates.put("/user_time_table/" + userId + "/" + key, postValues);

    databasedrtime.updateChildren(childUpdates);
}

1 个答案:

答案 0 :(得分:1)

在adddrtime0()

输入此代码
Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    final int days = calendar.get(Calendar.DAY_OF_MONTH);
    int hour = 0;
    int minute = 0;

    int currentApiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentApiVersion > android.os.Build.VERSION_CODES.LOLLIPOP_MR1){
        hour = timePicker.getHour();
        minute = timePicker.getMinute();
    } else {
        hour = timePicker.getCurrentHour();
        minute = timePicker.getCurrentMinute();
    }
    calendar.set(year, month, days, hour, minute);
    final long time = calendar.getTimeInMillis();

我正在获取我精确选择的时间选择器值。