简体   繁体   English

如何从日期选择器获取的当前日期增加40周

[英]How can I add 40 weeks to the current date I get from the date picker

I have this code, I want to add 40 weeks to the date I get from the date Picker and get the new date after the 40 weeks (280 days) has been added to the date from the date picker. 我有这段代码,我想在从日期选择器获得的日期中添加40周,并在将40周(280天)添加到日期选择器中的日期后获得新日期。

Code: 码:

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

Joda time is a very convenient library for handling such cases. Joda time是处理此类情况的非常方便的库。 Add this to your project: 将此添加到您的项目:

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

And then you can manipulate the dates like this: 然后您可以像这样操作日期:

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

Remember that in JodaTime date objects are immutable (which is a very good idea), so each manipulation produces a new object. 请记住,在JodaTime日期对象是不可变的(这是一个好主意),因此每次操作都会产生一个新对象。

First, convert the current format to milliseconds and then add specific days milliseconds and then again get it in the desired format. 首先,将当前格式转换为毫秒,然后添加特定天数毫秒,然后再次将其转换为所需格式。 Like this way: 像这样:

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

通过这种方式使用add(Calendar.DAY_OF_MONTH,int)函数:

cldr.add(Calendar.DAY_OF_MONTH, 280);

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在材料日期选择器 Android 中设置当前日期(今天的日期)与所选日期相同 - How can I set current date (Today's date) same as selected date in Material Date Picker Android 如何计算给定日期列表中几周的日期数 - how do i count no of dates from weeks on the given date list 如何在Java中将当前日期添加到工作日? - How can I add business days to the current date in Java? 如何在Android中获取当前系统日期? - how can i get current system date in Android? 如何在 Java 中获取 UTC 或 GMT 的当前日期和时间? - How can I get the current date and time in UTC or GMT in Java? 如何从 MaterialDatePicker 获取日期? - How can I get the date from MaterialDatePicker? 我想获得一年中的所有周,以及从当前日期开始的 Java 中每周的开始和结束工作日日期? - I want to get all the weeks in a year with the start and end working day date for each week in java starting at current date? 将日期时间选择器对话框中的日期增加39周 - Incrementing the date taken from a date time picker dialog by 39 weeks 如何添加日期,然后将其与Hibernate中的当前日期进行比较? - How do I add to a date and then compare it to the current date in Hibernate? 如何在robomongo中插入当前日期? - How can i insert current date in robomongo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM