0

I'm making an android app and I'm trying to get the day before yesterday each time that I click a button.

If I click the button it should bring me yesterday, if click it again it should bring me the day before yesterday, if I do it again, the day before that, and so on.

This is what I'm using to get yesterday:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.add(Calendar.DATE, -1);
long yesterdayEnd = cal.getTimeInMillis() / 1000;
Zong
  • 6,160
  • 5
  • 32
  • 46
arielgpe
  • 19
  • 1
  • 1
  • 4

2 Answers2

1

Here's 2 ways to achieve what you want.

private Calendar yesterday = null;
...
public long getYesterday() {
  if (this.yesterday == null) {
    this.yesterday = Calendar.getInstance();
    this.yesterday.set(Calendar.HOUR_OF_DAY, 23);
    this.yesterday.set(Calendar.MINUTE, 59);
    this.yesterday.set(Calendar.SECOND, 59);
    this.yesterday.set(Calendar.MILLISECOND, 0);
  }

  this.yesterday.add(Calendar.DATE, -1);
  return this.yesterday.getTimeInMillis() / 1000;
}

or

private int backDays = 0;
...
public long getYesterday() {
  Calendar yesterday = Calendar.getInstance();
  yesterday.set(Calendar.HOUR_OF_DAY, 23);
  yesterday.set(Calendar.MINUTE, 59);
  yesterday.set(Calendar.SECOND, 59);
  yesterday.set(Calendar.MILLISECOND, 0);

  yesterday.add(Calendar.DATE, -backDays);
  return yesterday.getTimeInMillis() / 1000;
}

In the second one, you need to increase backDays each time your button is pushed while it isn't necessary in the first one. The method need to be called when your button is pushed.

If you want to reset the value to start from scratch, in the first one, use yesterday = null; and backDays = 0; in the second one.

Also, I would suggest you use a Calendar, or a Date, variable instead of a long one. Changing the methods to public Calendar getYesterday() {or even public Date getYesterday() { would be cleaner than using a long.

Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
  • Unsure why he's using a `long` but since that what he seemed to need, that's what I used to return. Added that `Calendar` or `Date` would be better. – Jonathan Drapeau May 14 '14 at 16:47
  • Just to be clear what I mean, in the first method you are doing a getAndSetYesterday and in the second you construct everytime a new `Calender` object with a new reference and both solutions could be error prone in the future. Besides he mentioned a button which should decrement the date and if we have the android lifecycle, we can do it the "android"-way like I suggested in my answer. Maybe it's just taste ;) – seb May 14 '14 at 16:52
  • 1
    Indeed, not used to the android lifecycle, my answer is purely java oriented. – Jonathan Drapeau May 14 '14 at 17:04
  • @JonathanDrapeau sorry for the long wait, i tried both of your suggestions, but it doesn't matter how many times i click the button, its only giving me yesterday's value. – arielgpe May 14 '14 at 18:56
0

Make it a field in your Activity:

private Calendar cal;

in onCreate:

cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);

in the Button onClickListener as an inner class of Activity:

cal.add(Calendar.DATE, -1);
seb
  • 4,279
  • 2
  • 25
  • 36
  • Your suggestion is good but i forgot to say that i'm doing this on a Widget, i think the java way is more appropriated on this case. isn't? – arielgpe May 14 '14 at 18:57
  • No, it's not making a difference. How to use Buttons in a widget is described here: http://stackoverflow.com/questions/6361962/android-widget-button making a widget is harder than creating a normal application, I would not suggest a beginner to code widgets – seb May 14 '14 at 19:11
  • Actually, the widget is completely working with no problems, i just felt more comfortable with the java way. – arielgpe May 14 '14 at 19:38
  • 1
    Just an FYI, you're not setting the global variable for cal there – Cruceo May 14 '14 at 21:10