Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

java - How can I add one month to change into the milliseconds?

I have a problem changing the current time into milliseconds, the milliseconds shown one month before the set date and time. I know the computer starts the months at 0, how can I solve the problem? First, I transfer the date and the time into String, then I convert the String into date in SimpleDateFormat, and then I convert the date into Long.

For example: When the user enter the date "2018/2/14 11:18 "(AM), the date convert to long is "1515899940000" .

Here is my code:

private void  setDateField() {

    Calendar c = Calendar.getInstance();

    int yy = c.get(Calendar.YEAR);
    int mm = c.get(Calendar.MONTH);
    int dd = c.get(Calendar.DAY_OF_MONTH);

    c.set(Calendar.MONTH, mm);
    c.set(Calendar.DAY_OF_MONTH, dd);
    c.set(Calendar.YEAR, yy);

    DatePickerDialog datePickerDialog = new DatePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog ,new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDate;
            date_time = year + "/" + (month + 1) + "/" + day;
            timePicker();
        }
    },year, month, day);

    datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
    datePickerDialog.show();

}

private void timePicker(){
    // Get Current Time
    final Calendar c = Calendar.getInstance();
    hours = c.get(Calendar.HOUR_OF_DAY);
    minutes = c.get(Calendar.MINUTE);
    // Launch Time Picker Dialog
    TimePickerDialog timePickerDialog = new TimePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog,new TimePickerDialog.OnTimeSetListener() {

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

                    hours = hourOfDay;
                    minutes = minute;
                    string_date = date_time+" "+format(hours) + ":" +format(minutes) ;
                    addtime.setText(string_date);
                    Log.e("JEJEJEJE",string_date);

                    SimpleDateFormat f = new SimpleDateFormat("yyyy/mm/dd HH:mm");
                    try {
                        Date d = f.parse(string_date);
                        milliseconds = d.getTime();
                        Log.e("LONGGGGGGG", String.valueOf(milliseconds));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }, hours, minutes, true);
    timePickerDialog.show();

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...