1

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

  • Post the crash logs please. – danypata Oct 16 '18 at 10:34
  • Consider throwing out the long outdated classes `Date`, `Calendar` and in particular the notoriously troublesome `SimpleDateFormat` and adding [the ThreeTenABP library](https://github.com/JakeWharton/ThreeTenABP) to your Andoird project in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Oct 16 '18 at 19:01
  • can you help me usung the threetenABP but using my case ? – avid _ kupit Oct 17 '18 at 00:12

3 Answers3

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

}
Audrius
  • 64
  • 1
  • 5
1

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • haloo, i use your suggestion and it works. but the output got 26 27 28 etc. which is wrong because today is 20 not 26. did you know why ? i use ZoneId "Asia/Jakarta" btw – avid _ kupit Jan 20 '20 at 17:19
  • In case other readers are curious: @avid_kupit’s issue is [answered here](https://stackoverflow.com/questions/59849138/why-localdate-value-didnt-set-on-today-date) – Ole V.V. Jan 22 '20 at 05:18
  • thanks [Ole.vv](https://stackoverflow.com/users/5772882/ole-v-v) for hellp, really appreciated sir – avid _ kupit Jan 22 '20 at 07:17
  • No need to mention it, @avid_kupit, I don’t think I really contributed anything in this round. – Ole V.V. Jan 22 '20 at 14:51
0

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);
Mohamed Nageh
  • 1,963
  • 1
  • 19
  • 27
  • java.lang.IllegalArgumentException: Cannot format given Object as a Date .........app stoped, and this on debug console, any idea why ? – avid _ kupit Oct 16 '18 at 11:33