1

in my app i can set an alarm for any the days of the week, i want to display a toast with the time until the next alarm, i set a value for every day:

String[] str = new String[7];
str[0] = getResources().getString(R.string.weeks_sunday);
str[1] = getResources().getString(R.string.weeks_monday);
str[2] = getResources().getString(R.string.weeks_tuesday);
str[3] = getResources().getString(R.string.weeks_wednesday);
str[4] = getResources().getString(R.string.weeks_thursday);
str[5] = getResources().getString(R.string.weeks_friday);
str[6] = getResources().getString(R.string.weeks_Saturday);
weeks = str;

This what i do to calculate the time to the next alarm, but i dont know how to know the close day

    String substr = ":";
    String before =  aTime.substring(0, aTime.indexOf(substr));
    int bef = Integer.parseInt(before);
    String after = aTime.substring(aTime.indexOf(substr) + substr.length());
    int aft = Integer.parseInt(after);

    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int dayy = c.get(Calendar.DAY_OF_MONTH);

    GregorianCalendar date = new GregorianCalendar();
    long currentTime = date.getTimeInMillis();
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(year, month, 22, bef, aft, 0);
    long difference = calendar.getTimeInMillis()-currentTime;

    long x = difference / 1000;
    long seconds = x % 60;
    x /= 60;
    long minutes = x % 60;
    x /= 60;
    long hours = x % 24;
    x /= 24;
    long days = x;      



    if(hours == 1){
            Toast.makeText(AddAlarmActivity.this, "Alarm set for " + days + " days, " + hours + " hour and " + minutes + " minutes from now", Toast.LENGTH_LONG).show();
    }else if(hours >= 2){
            Toast.makeText(AddAlarmActivity.this, "Alarm set for " + days + " days, " + hours + " hours and " + minutes + " minutes from now", Toast.LENGTH_LONG).show();
MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72
GusDev
  • 255
  • 6
  • 20
  • I don't understand the question. It looks like you were able to calculate the day just fine? Are you getting incorrect values? The TimeUnit class might clear things up: http://stackoverflow.com/a/7829642/3131147 – AdamMc331 Sep 18 '15 at 16:42
  • i want to know the date for the nearest day, ex, if i select sunday i will get only [0] i want to know the date and set it instead of "22" – GusDev Sep 18 '15 at 16:48

2 Answers2

1

To get the day, you can do

final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
final int day = calendar.get(Calendar.DAY_OF_WEEK);

switch (day) {
    case Calendar.SUNDAY:

}

// or you can just match the day to your day name string.
Jin
  • 6,055
  • 2
  • 39
  • 72
0
Date date = calendar.add(Date, 1);

This give you the next day in the cal. For getting dayname and number:

 String dayname = date.toString().substring(0, 3);
 String temp = date.toString().substring(8, 10);
 Integer day = Integer.parseInt(temp);
XxGoliathusxX
  • 922
  • 13
  • 34