Permalink
Browse files

Change the Date

  • Loading branch information...
dragosholban committed Mar 10, 2018
1 parent 7c5f9d6 commit 551db67fc6488754e29d4c1201b7cc78c003e5ef
@@ -0,0 +1,28 @@
package dragosholban.com.timezoneconverter;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;
import java.util.Calendar;
import java.util.Date;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
((MainActivity) getActivity()).setLocalDate(new Date(year - 1900, month, day));
}
}
@@ -1,14 +1,19 @@
package dragosholban.com.timezoneconverter;
import android.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
Date localDate = new Date();
Button dateBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -39,5 +44,24 @@ public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar.setProgress(localDate.getHours());
dateBtn = findViewById(R.id.dateButton);
dateBtn.setText(DateFormat.getDateInstance().format(localDate));
}
public void showDatePicker(View view) {
DialogFragment dialog = new DatePickerFragment();
dialog.show(getFragmentManager(), "datePicker");
}
public void setLocalDate(Date date) {
// we need to keep the time on the date unchanged
int hours = localDate.getHours();
int minutes = localDate.getMinutes();
localDate = date;
localDate.setHours(hours);
localDate.setMinutes(minutes);
Button dateBtn = findViewById(R.id.dateButton);
dateBtn.setText(DateFormat.getDateInstance().format(localDate));
}
}
@@ -27,6 +27,7 @@
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:onClick="showDatePicker"
android:text="Date"
app:layout_constraintBottom_toTopOf="@+id/timeZoneButton"
app:layout_constraintEnd_toEndOf="parent"

0 comments on commit 551db67

Please sign in to comment.