I am making a date picker and when I select the date it starts at 1900, however the time starts at the current time. Is there a way to make the date to choose start at the current date?
Code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
int longitudvector=0;
Context context;
// IP de mi Url
String IP = "http://alquilalo.esy.es";
// Rutas de los Web Services
String GET = IP + "/obtener_vehiculos.php";
String semail;
//String recogerdia;
String rdia,rmes,rano,rhora,rminutos;
String ddia,dmes,dano,dhora,dminutos;
//String recogerdiaentero;
Button bfecha,bhora;
EditText efecha,ehora;
public int dia,mes,ano,hora,minutos;
Button b2fecha,b2hora;
EditText e2fecha,e2hora;
public int dia2,mes2,ano2,hora2,minutos2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
//Toast.makeText(this, semail, Toast.LENGTH_SHORT).show();
bfecha=(Button)findViewById(R.id.bfecha);
bhora=(Button)findViewById(R.id.bhora);
efecha=(EditText)findViewById(R.id.efecha);
ehora=(EditText)findViewById(R.id.ehora);
b2fecha=(Button)findViewById(R.id.b2fecha);
b2hora=(Button)findViewById(R.id.b2hora);
e2fecha=(EditText)findViewById(R.id.e2fecha);
e2hora=(EditText)findViewById(R.id.e2hora);
bfecha.setOnClickListener(this);
bhora.setOnClickListener(this);
b2fecha.setOnClickListener(this);
b2hora.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==bfecha){
final java.util.Calendar c= java.util.Calendar.getInstance();
dia=c.get(java.util.Calendar.DAY_OF_MONTH);
mes=c.get(java.util.Calendar.MONTH);
ano=c.get(java.util.Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
efecha.setText(dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
rdia= String.valueOf(dayOfMonth);
rmes= String.valueOf(monthOfYear+1);
rano= String.valueOf(year);
}
}
,dia,mes,ano);
datePickerDialog.show();
}
if (v==bhora){
final java.util.Calendar c= java.util.Calendar.getInstance();
hora=c.get(java.util.Calendar.HOUR_OF_DAY);
minutos=c.get(java.util.Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
ehora.setText(hourOfDay+":"+minute);
rhora= String.valueOf(hourOfDay);
rminutos= String.valueOf(minute);
}
},hora,minutos,false);
timePickerDialog.show();
}
if(v==b2fecha){
final java.util.Calendar c= java.util.Calendar.getInstance();
dia2=c.get(java.util.Calendar.DAY_OF_MONTH);
mes2=c.get(java.util.Calendar.MONTH);
ano2=c.get(java.util.Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
e2fecha.setText(dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
ddia= String.valueOf(dayOfMonth);
dmes= String.valueOf(monthOfYear+1);
dano= String.valueOf(year);
}
}
,dia2,mes2,ano2);
datePickerDialog.show();
}
if (v==b2hora){
final java.util.Calendar c= java.util.Calendar.getInstance();
hora2=c.get(java.util.Calendar.HOUR_OF_DAY);
minutos2=c.get(java.util.Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
e2hora.setText(hourOfDay+":"+minute);
dhora= String.valueOf(hourOfDay);
dminutos= String.valueOf(minute);
}
},hora2,minutos2,false);
timePickerDialog.show();
}
}
}
My layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:text="Fecha de Recogida"
android:id="@+id/bfecha"
android:textColor="#FFFFFF"
android:background="@drawable/botonshape2"
/>
<Button
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Hora"
android:id="@+id/bhora"
android:textColor="#FFFFFF"
android:background="@drawable/botonshape2"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/efecha"
android:layout_width="200dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content" />
<EditText
android:layout_width="90dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:id="@+id/ehora"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:text="Fecha de Devolución"
android:id="@+id/b2fecha"
android:textColor="#FFFFFF"
android:background="@drawable/botonshape2"
/>
<Button
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Hora"
android:id="@+id/b2hora"
android:textColor="#FFFFFF"
android:background="@drawable/botonshape2"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/e2fecha"
android:layout_width="200dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content" />
<EditText
android:layout_width="90dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:id="@+id/e2hora"
/>
</LinearLayout>
<Button
android:text="continuar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/botoncalendariocontinuar" />