how do i put date orderly on each button?

不羁岁月 提交于 2020-01-25 05:27:05

问题


I have 7 buttons. On the first button I display the current date, on the second button I want it to display the date of tomorrow, on the third one the date after that, and so on.

I tried several times using Calendar but the app close when this activity open. can someone show me how to use Calendar on my case ? or how to solve this ?

public class OrderActivity extends AppCompatActivity {

Button dateButton1, dateButton2;

Calendar calender = Calendar.getInstance();
Date today = new Date();


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

    dateButton1 = (Button)findViewById(R.id.button5);
    dateButton2 = (Button)findViewById(R.id.button6);


    int amount = 1; // amount of days you want to add to the current date

    SimpleDateFormat formattedDate = new SimpleDateFormat("MM");

    today.setTime(System.currentTimeMillis()); //set to current date
    dateButton1.setText(formattedDate.format(today));


    //this code below cause app stoped when this activity start
    calender.add(Calendar.DATE, amount);
    String newDate = (String)(formattedDate.format(calender.getTime()));

    dateButton2.setText(formattedDate.format(newDate));

}}

This was my last build, how can I apply the next date on dateButton2, the date after that on dateButton3 and so on?

so far when enter this activity app closed and this is on the logcat

> java.lang.IllegalArgumentException: Cannot format given Object as a Date
                                                                       at java.text.DateFormat.format(DateFormat.java:306)
                                                                       at java.text.Format.format(Format.java:157)

PS: sorry for bad English


回答1:


This should work:

public class OrderActivity extends AppCompatActivity {

Button dateButton1, dateButton2;

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

    Date date = new Date();

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");  

    LocalDateTime today = LocalDateTime.now(); 
    LocalDate tomorrow = LocalDate.now().plusDays(1);

    // LocalDate dayAfterTomorrow = LocalDate.now().plusDays(2);
    // continue like this

    dateButton1 = (Button)findViewById(R.id.button5);
    dateButton2 = (Button)findViewById(R.id.button6);

    dateButton1.setText(dtf.format(now)));
    dateButton2.setText(dtf.format(tomorrow)));
}

}



回答2:


java.time and ThreeTenABP

can you help me usung the threetenABP but using my case ?

This should get you in the right direction:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("M-d");
    ZoneId zone = ZoneId.of("Asia/Jayapura");
    LocalDate date = LocalDate.now(zone);
    int amount = 1; // amount of days you want to add to the current date
    int buttonCount = 7;
    for (int i = 0; i < buttonCount; i++) {
        System.out.println(date.format(dateFormatter));
        date = date.plusDays(amount);
    }

Output when I ran the code just now:

10-17
10-18
10-19
10-20
10-21
10-22
10-23

My imports were:

import org.threeten.bp.LocalDate;
import org.threeten.bp.ZoneId;
import org.threeten.bp.format.DateTimeFormatter;

What went wrong in your code?

You tried this:

    String newDate = (String)(formattedDate.format(calender.getTime()));

    dateButton2.setText(formattedDate.format(newDate));

newDate is already formatted into a String, so it doesn’t make sense to do it once more. The format method accepts a Date or a Long, but not a String.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.



回答3:


You're using wrong format. your format will have months only. however you can increment days by using the following code.

Date today = new Date();
int amount = 1; // amount of days you want to add to the current date          
SimpleDateFormat formattedDate = new SimpleDateFormat("yyyy-MM-dd");            
Calendar calender = Calendar.getInstance();
// add to the current date
calender.add(Calendar.DATE, amount);    
String newDate = (String)(formattedDate.format(calender.getTime()));
System.out.println("newDate date is " + newDate);


来源:https://stackoverflow.com/questions/52833276/how-do-i-put-date-orderly-on-each-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!