-1

I am developing an app,I am using Date and time picker, To pick an date and time and I set result time 6 hours before picked time. but the result time and date shows in yyyy:MM:dd:HH:mm this format and I want to show this date and time separately in textfields (like date is yyyy:MM:dd and time is HH:mm).How do I show this?

//java
public class DateTime extends AppCompatActivity implements View.OnClickListener {

    Button btnDatePicker, btnTimePicker;
    EditText txtDate, txtTime;
    TextView dt, dt2;
    private int mYear, mMonth, mDay, mHour, mMinute;
    String selected_date="",selected_time="",new_date_time="";


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

        btnDatePicker=(Button)findViewById(R.id.btn_date);
        btnTimePicker=(Button)findViewById(R.id.btn_time);
        txtDate=(EditText)findViewById(R.id.in_date);
        txtTime=(EditText)findViewById(R.id.in_time);
        dt=(TextView)findViewById(R.id.textView_dt);
        dt2=(TextView)findViewById(R.id.textView_dt1);

        btnDatePicker.setOnClickListener(this);
        btnTimePicker.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v == btnDatePicker) {

            // Get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {

                            txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                            selected_date=year+":"+(monthOfYear + 1)+":"+dayOfMonth;
                        }
                    }, mYear, mMonth, mDay);

            datePickerDialog.show();
        }
        if (v == btnTimePicker) {

            // Get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {

                            txtTime.setText(hourOfDay + ":" + minute);
                            selected_time=hourOfDay+":"+minute;
                            setBeforeSixTime();
                        }
                    }, mHour, mMinute, false);

            timePickerDialog.show();

        }

    }

    private void setBeforeSixTime() {
        try {
            Log.d("Time here ", "hiii");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(selected_date + ":" + selected_time);
            String my_date = simpleDateFormat.format(new Date());
            DateFormat formatter = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
         //   DateFormat formatter = new SimpleDateFormat("dd:MM:yyyy:HH:mm");
            Date date = (Date) formatter.parse(my_date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR, -6);

            Log.d("Time here ", formatter.format(calendar.getTime()));
            String s=formatter.format(calendar.getTime());
            dt.setText(s);
        } catch (Exception e) {
            Log.d("Exception", "" + e);
        }
    }
}
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
rohiH
  • 99
  • 3
  • 14

6 Answers6

2

use two DateFormatter like:

 DateFormat dateFormatter = new SimpleDateFormat("yyyy:MM:dd");
 DateFormat timeFormatter = new SimpleDateFormat("HH:mm");
 dateField.setText(dateFormatter.format(date));
 timeField.setText(timeFormatter.format(date));
Jens
  • 67,715
  • 15
  • 98
  • 113
2

I have modified your code as per your question i have converted your time-stamp into milliseconds and subtracted 6 hours from it and than again i converted into 6 hours previous date and time Reference

try {
            Log.d("Time here ", "hiii");
            Date date;
            DateFormat formatter = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
            date = formatter.parse(selected_date + ":" + selected_time);
            //Subtracting 6 hours from selected time
            long time = date.getTime()-6*60*60*1000;

            String mDate = null;
            SimpleDateFormat formatterFullDate = new SimpleDateFormat("yyyy:MM:dd");
            mDate = formatterFullDate.format(time);
            Log.e("Date ",mDate );
            String mTime = null;
            SimpleDateFormat formatterTime = new SimpleDateFormat("HH:mm");
            mTime = formatterTime.format(time);
            Log.e("Time ",mTime );

        } catch (Exception e) {
            Log.d("Exception", "" + e);
        }
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
  • how i convert "yyyy:MM:dd" format to "dd:MM:yyyy" format using this code – rohiH Sep 29 '16 at 04:36
  • Just change this `SimpleDateFormat formatterFullDate = new SimpleDateFormat("yyyy:MM:dd");` to this `SimpleDateFormat formatterFullDate = new SimpleDateFormat("dd:MM:yyyy");` – Burhanuddin Rashid Sep 29 '16 at 04:38
1

Just do it as below, you will get all as separate strings than do operations,

String date = "yyyy:MM:dd:HH:mm";

String[] time1 = date.split(":");
    String part1 = parts[0];
    String part2 = parts[1];
    String part3 = parts[2];
    String part3 = parts[3];
    String part4 = parts[4];

now, to set values in textfield,

textView1.setText(part0 + " : " + part1 + " : " + part2);

textView2.setText(part3 + " : " + part4);
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
1

Get Date or time from on listener

  DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {

                                txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                   Calendar calendar = Calendar.getInstance(Locale.getDefault());

                    calendar.set(year, monthOfYear, dayOfMonth);
String selectedDate=getSelectedDate(calendar.getTime());
                            }
                        }, mYear, mMonth, mDay);

// getting date from this methods

private String getSelectedDate(Date selectedDate){
DateFormat dateFormatter = new SimpleDateFormat("yyyy:MM:dd");
return dateFormatter.format(selectedDate)
}

You can do same for time I hope this code help you

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
0

We can split DateTime by using the below code snippet SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd,HH:mm"); String[] time1 = simpleDateFormat.format(new Date()).split(","); String date=time1[0]; String time=time1[1];

Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

Take the complete formatted date and time in an String (say strDateTime) and then use Regular expression shown below:

Matcher matcher = Pattern.compile("(\\d{4}:\\d{2}:\\d{2}):(\\d{2}:\\d{2})").matcher(strDateTime);
if(matcher.matches()){
    textView1.setText(matcher.group(1));
    textView2.setText(matcher.group(2));
}

Hope this will help.

lRadha
  • 641
  • 6
  • 16