如何将“yyyy:MM:dd:HH:mm”格式拆分为单独的日期和时间?

时间:2016-09-28 05:17:02

标签: android

我正在开发一个应用程序,我正在使用日期和时间选择器,选择日期和时间,我在选择时间前6小时设置结果时间。但结果时间和日期以yyyy:MM:dd:HH:mm格式显示,我希望在文本字段中分别显示此日期和时间(例如日期为yyyy:MM:dd,时间为HH:mm)。如何显示此?

//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);
        }
    }
}

6 个答案:

答案 0 :(得分:2)

使用两个DateFormatter,如:

 DateFormat dateFormatter = new SimpleDateFormat("yyyy:MM:dd");
 DateFormat timeFormatter = new SimpleDateFormat("HH:mm");
 dateField.setText(dateFormatter.format(date));
 timeField.setText(timeFormatter.format(date));

答案 1 :(得分:2)

我根据您的问题修改了您的代码我已将您的时间戳转换为毫秒并从中扣除了6个小时,而且我再次转换为6个小时的上一个日期和时间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);
        }

答案 2 :(得分:1)

从听众那里获取日期或时间

  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);

//从此方法获取日期

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

我希望这段代码可以帮助你

答案 3 :(得分:0)

如下所示,你将获得所有单独的字符串而不是操作,

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];

现在,在文本字段中设置值

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

textView2.setText(part3 + " : " + part4);

答案 4 :(得分:0)

我们可以使用下面的代码片段拆分DateTime     SimpleDateFormat simpleDateFormat = new SimpleDateFormat(" yyyy:MM:dd,HH:mm");     String [] time1 = simpleDateFormat.format(new Date())。split(",");     字符串日期= time1 [0];     字符串时间= time1 [1];

答案 5 :(得分:0)

String(例如strDateTime)中获取完整格式化的日期和时间,然后使用如下所示的正则表达式:

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));
}

希望这会有所帮助。