android 2.3系统闹钟中 重复项的思路

时间:2021-10-16 16:45:12

http://download.csdn.net/download/glzlaohuai/5979097(别人的提供的源码)

1.

int count = 0;
for (int i = 0; i < 7; i++) {
int c = 1 << i;//2的n次方
count += c;
System.out.print(i + " " + c);
System.out.format(" %x ", c);
System.out.println(count);
}

可以得到不重复-到每天重复的对应的值   每天的值是所有单天值的总和

 /*
     * Days of week code as a single int.
     * 0x00: no day
     * 0x01: Monday
     * 0x02: Tuesday
     * 0x04: Wednesday
     * 0x08: Thursday
     * 0x10: Friday
     * 0x20: Saturday
     * 0x40: Sunday
     */


  public void set(int day, boolean set) {
            if (set) {
                mDays |= (1 << day);//将被选中的值加到总数中相当于count += c; |相当于加
            } else {
                mDays &= ~(1 << day);/ / 减的时候需要先取反再&
            }
        }


package file;


import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Test {
private static int[] DAY_MAP = new int[] { Calendar.MONDAY,
Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY,
Calendar.FRIDAY, Calendar.SATURDAY, Calendar.SUNDAY, };


public static void main(String[] args) {
// DaysOfWeek daysOfWeek = new DaysOfWeek(0);
// daysOfWeek.set(3, true);
// // daysOfWeek.set(4, true);
// // System.out.println(daysOfWeek.getCoded());
// calculateAlarm(2, 3, daysOfWeek);
dd(4, 30);
// for (int i = 0; i < DAY_MAP.length; i++) {
// System.out.println(DAY_MAP[i]);
// }




}



public int addMonth(int day){
Calendar cal = Calendar.getInstance();
int max=cal.getActualMaximum(Calendar.DATE);
return 0;
}

public static void dd(int hour, int minute) {
Calendar cal = Calendar.getInstance();
/****/


cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);


cal.set(Calendar.MILLISECOND, 0);
///System.out.println(cal.getTimeInMillis());
long x = cal.getTimeInMillis();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEEE");


// System.out.println(df.format(cal.getTime()));
// System.out.println(df.format(x));


String start = "2015-12-30";
long xx = getDtstart(start);
System.out.println(xx);
// calculateAlarm(getDtstart(start),13,0);
System.out.println(dtLongToString(xx));
}


public static long getDtstart(String start) {
Calendar cal = Calendar.getInstance();
long dtStart = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dBegin;
try {
dBegin = sdf.parse(start);
cal.setTime(dBegin);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH,13);
int day=cal.getActualMaximum(Calendar.DATE);
//System.out.println("max = "+cal.get(Calendar.DAY_OF_MONTH));
dtStart = +cal.getTimeInMillis();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return dtStart;
}


public static String dtLongToString(long time) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String dt = df.format(time);
return dt;
}


public static Calendar calculateAlarm(long date, int hour, int minute) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(date);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);


Calendar cal2 = Calendar.getInstance();


if (cal.compareTo(cal2) > 0) {// 当前时间比闹钟时间小
System.out.println("............");
} else {
cal.add(Calendar.MONTH, 1);//
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss EEEE");


System.out.println(df.format(cal.getTime()));
}


return cal;
}


static Calendar calculateAlarm(int hour, int minute, DaysOfWeek daysOfWeek) {


// start with now
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
/****/
System.out.println(c.getTimeInMillis());
// 使用默认时区和语言环境获得一个日历
// 将Calendar类型转换成Date类型
Date tasktime = c.getTime();
// 设置日期输出的格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEEE");
// 格式化输出
System.out.println(df.format(tasktime));


/***/
int nowDay = c.get(Calendar.DAY_OF_MONTH);
int nowHour = c.get(Calendar.HOUR_OF_DAY);
int nowMinute = c.get(Calendar.MINUTE);
int week = c.get(Calendar.DAY_OF_WEEK);
System.out.println(nowHour + " " + nowMinute + " " + nowDay + " "
+ week);


// if alarm is behind current time, advance one day
if (hour < nowHour || hour == nowHour && minute <= nowMinute) {// 比当前时间小则增加一天
c.add(Calendar.DAY_OF_YEAR, 1);// 往后一天
int nowDay2 = c.get(Calendar.DAY_OF_MONTH);


}
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);


int nowDay2 = c.get(Calendar.DAY_OF_MONTH);
int nowHour2 = c.get(Calendar.HOUR_OF_DAY);
int nowMinute2 = c.get(Calendar.MINUTE);
int week2 = c.get(Calendar.DAY_OF_WEEK);


Date tasktime2 = c.getTime();
// 设置日期输出的格式
// 格式化输出
System.out.println(df.format(tasktime2));
System.out.println(nowHour2 + " " + nowMinute2 + " " + nowDay2 + " "
+ week2);


int addDays = daysOfWeek.getNextAlarm(c);
if (addDays > 0 && daysOfWeek.getCoded() < 128) {
c.add(Calendar.DAY_OF_WEEK, addDays);// 星期往后两天
int week3 = c.get(Calendar.DAY_OF_WEEK);
Date tasktime3 = c.getTime();
// 设置日期输出的格式
// 格式化输出
System.out.println(df.format(tasktime3));
System.out.println(week3);
}
return c;
}


/*
* Days of week code as a single int. 0x00: no day 0x01: Monday 0x02:
* Tuesday 0x04: Wednesday 0x08: Thursday 0x10: Friday 0x20: Saturday 0x40:
* Sunday
*/
static final class DaysOfWeek {


private static int[] DAY_MAP = new int[] { Calendar.MONDAY,
Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY,
Calendar.FRIDAY, Calendar.SATURDAY, Calendar.SUNDAY, };


// Bitmask of all repeating days
private int mDays;


DaysOfWeek(int days) {
mDays = days;
}


public String toString(boolean showNever) {
StringBuilder ret = new StringBuilder();


// no days
if (mDays == 0) {
return "never";
}


// every day
if (mDays == 0x7f) {
return "every";
}


// count selected days
int dayCount = 0, days = mDays;
while (days > 0) {
if ((days & 1) == 1)
dayCount++;
days >>= 1;
}


// short or long form?
DateFormatSymbols dfs = new DateFormatSymbols();
String[] dayList = (dayCount > 1) ? dfs.getShortWeekdays() : dfs
.getWeekdays();


// selected days
for (int i = 0; i < 7; i++) {
if ((mDays & (1 << i)) != 0) {
ret.append(dayList[DAY_MAP[i]]);
dayCount -= 1;
if (dayCount > 0)
ret.append(",");
}
}
return ret.toString();
}


private boolean isSet(int day) {
return ((mDays & (1 << day)) > 0);
}


public void set(int day, boolean set) {
if (set) {
mDays |= (1 << day);
} else {
mDays &= ~(1 << day);
}
}


public void set(DaysOfWeek dow) {
mDays = dow.mDays;
}


public int getCoded() {
return mDays;
}


// Returns days of week encoded in an array of booleans.
public boolean[] getBooleanArray() {
boolean[] ret = new boolean[7];
for (int i = 0; i < 7; i++) {
ret[i] = isSet(i);
}
return ret;
}


public boolean isRepeatSet() {
return mDays != 0;
}


/**
* returns number of days from today until next alarm

* @param c
*            must be set to today
*/
public int getNextAlarm(Calendar c) {
if (mDays == 0) {
return -1;
}


int today = (c.get(Calendar.DAY_OF_WEEK) + 5) % 7;
System.out.println(today);
int day = 0;
int dayCount = 0;
for (; dayCount < 7; dayCount++) {
day = (today + dayCount) % 7;
System.out.println(day + " isSet " + isSet(day));
if (isSet(day)) {
break;
}
}
System.out.println(" dayCount " + dayCount);


return dayCount;
}
}
}