FORUMS
Remove All Ads from XDA

2 editboxes 2 date pickers 2 times pickers

1 posts
Thanks Meter: 0
 
By datamedic2013, Junior Member on 10th March 2017, 05:45 PM
Post Reply Email Thread
Hello guys

im trying to find out what im doing wrong in this case

Im trying to have 2 edittext and every time they get tapped to display date picker and time picker and get print of the date on this format "dd/MM/yyyy HH:mm"

instead of this i get "dd/MM/yyyy - HH:mm - HH:mm""

what the heck im doing wrong ?
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import static com.example.neo.myapplication.R.id.currenttext;


public class MainActivity extends FragmentActivity {
static EditText DateEdit;
static EditText DateEdit2;
static TextView DateFinalText;
Button buttoncalc;
Button FinalCalc;
static String DesFinalDate;
static String DVRFinalDate;


@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView tv = (TextView) findViewById(currenttext);
final String datecur = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(Calendar.getInstance().getTime());
tv.setText(datecur);



FinalCalc = (Button) findViewById(R.id.buttoncalc);
FinalCalc.setOnClickListener(new View.OnClickListener(){
@override
public void onClick(View v) {
try {

//Dates to compare
String CurrentDate= "10/4/2017 10:21";
String FinalDate= "11/03/2016 11:21";

Date date1;
Date date2;

SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy HH:mm");

//Setting dates
date1 = dates.parse(CurrentDate);
date2 = dates.parse(FinalDate);

//Comparing dates
long difference = Math.abs(date1.getTime() - date2.getTime());
long differenceDates = difference / (24 * 60 * 60 * 1000);

//Convert long to String
String dayDifference = Long.toString(differenceDates);


Log.e("HERE","HERE: " + dayDifference);


} catch (Exception exception) {
Log.e("DIDN'T WORK", "exception " + exception);
}


}

});

DateEdit = (EditText) findViewById(R.id.editText1);
DateEdit.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
showTimePickerDialog(v);
showDatePickerDialog(v);
}
});

DateEdit2 = (EditText) findViewById(R.id.editText2);
DateEdit2.setOnClickListener(
new View.OnClickListener() {
@override
public void onClick(View v) {
showTimePickerDialog1(v);
showDatePickerDialog1(v);

}

});

}

public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public void showDatePickerDialog1(View v) {
DialogFragment newFragment = new DatePickerFragment1();
newFragment.show(getSupportFragmentManager(), "datePicker");
}

//DATE PICKER FRAGMENT

public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {

@override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user

DateEdit.setText(day + "/" + (month + 1) + "/" + year);
}

}

//DATE PICKER FRAGMENT 1


public static class DatePickerFragment1 extends DialogFragment implements
DatePickerDialog.OnDateSetListener {

@override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user

DateEdit2.setText(day + "/" + (month + 1) + "/" + year);
}
}

//TIME PICKER FRAGMENT


public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");

}


public void showTimePickerDialog1(View v) {
DialogFragment newFragment = new TimePickerFragment1();
newFragment.show(getSupportFragmentManager(), "timePicker");

}


public static class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {

@override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
DateEdit.setText(DateEdit.getText() + " -" + hourOfDay + ":" + minute);
DVRFinalDate = DateEdit.toString();
}
}
//TIME PICKER FRAGMENT1


public static class TimePickerFragment1 extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {

@override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user

DateEdit2.setText(DateEdit2.getText() + " -" + hourOfDay + ":" + minute) ;


DesFinalDate = DateEdit2.toString();

}
}
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes