【问题标题】:How can I add 40 weeks to the current date I get from the date picker如何将 40 周添加到从日期选择器获得的当前日期
【发布时间】:2019-05-14 09:06:40
【问题描述】:

我有此代码,我想将 40 周添加到我从日期选择器获得的日期,并在将 40 周(280 天)添加到日期选择器的日期后获取新日期。

代码:

public class MainActivity extends AppCompatActivity {

    DatePickerDialog picker;
    EditText eText;
    Button btnGet;
    TextView tvw;

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

        tvw=(TextView)findViewById(R.id.textView1);
        eText=(EditText) findViewById(R.id.editText1);
        eText.setInputType(InputType.TYPE_NULL);
        eText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar cldr = Calendar.getInstance();
                int day = cldr.get(Calendar.DAY_OF_MONTH);
                int month = cldr.get(Calendar.MONTH);
                int year = cldr.get(Calendar.YEAR);
                // date picker dialog
                picker = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                eText.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                            }
                        }, year, month, day);
                picker.show();
            }
        });
        btnGet=(Button)findViewById(R.id.button1);
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvw.setText("Selected Date: "+ eText.getText());
            }
        });
    }
}

【问题讨论】:

  • 你可能想看看这个。在您的onDateSet 处理程序中,根据年、月和日构造一个Calendar 对象,并根据需要添加天数。看看这个答案。 stackoverflow.com/questions/9670648/…

标签: java android android-datepicker


【解决方案1】:

Joda time 是处理此类情况的非常方便的库。将此添加到您的项目中:

dependencies {    
    compile 'joda-time:joda-time:2.10.2'
}

然后你可以像这样操作日期:

DateTime dt = DateTime.now();
DateTime laterDate = dt.withYear(2020)
    .withMonthOfYear(3)
    .withDayOfMonth(14)
    .plusWeeks(40);

请记住,JodaTime 中的日期对象是不可变的(这是一个非常好的主意),因此每次操作都会产生一个新对象。

【讨论】:

    【解决方案2】:

    首先,将当前格式转换为毫秒,然后添加特定天数毫秒,然后再次获得所需格式。像这样:

    new DatePickerDialog(MainActivity.this,
                            new DatePickerDialog.OnDateSetListener() {
                                @Override
                                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    
            Calendar calendar = Calendar.getInstance();
            calendar.set(year,monthOfYear + 1,dayOfMonth);
    
            long timeInMilliseconds = 
            calendar.getTimeInMillis()+TimeUnit.DAYS.toMillis(280);
    
            calendar.setTimeInMillis(timeInMilliseconds);
    
            int mYear = calendar.get(Calendar.YEAR);
            int mMonth = calendar.get(Calendar.MONTH);
            int mDay = calendar.get(Calendar.DAY_OF_MONTH);
                                    eText.setText(mDay + "/" + mMonth + "/" + mYear);
                                }
                            }, year, month, day);
                    picker.show();
                }
            });
    

    【讨论】:

    • 我收到错误无法解析此行上的符号 timeStamp calendar.setTimeInMillis(timeStamp);
    【解决方案3】:

    这样使用 add(Calendar.DAY_OF_MONTH, int) 函数:

    cldr.add(Calendar.DAY_OF_MONTH, 280);
    

    【讨论】:

      【解决方案4】:

      从逻辑上讲,您不必每次都添加40 weeks,一周有特定的天数,因此您可以在从日期选择器中选取的当天添加 40*7 = 280 天。

      [current date] + TimeUnit.DAYS.toMillis(280)
      

      【讨论】:

        猜你喜欢
        • 2022-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多