如何获取 android 中两个日期之间的周间隔?
How to get week interval in between two dates in android?
你好,我正在写一个代码,它可以显示当前时间和登录时间之间的周间隔,
我正在添加一项功能以节省登录时间。
public void setWeek() {
Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateToStr = format.format(today);
editor.putString("week", dateToStr).commit();
}
public String getWeek() {
return pref.getString("week", null);
}
这会帮助你。我使用此条件从输入日期获取天数,并检查天数是否为 运行 我的条件
call conditionForWeek(){
calendarToday = Calendar.getInstance();
formatInstallDate = new SimpleDateFormat(FORMAT_DATE_DD_MMM_YY, Locale.ENGLISH);
currentDate = formatInstallDate.format(calendarToday.getTime());
installDate = PreferencesManager.getInstance().getString(USER_DATE_OF_INSTALL);
int differenceInDate = PregnancyAppUtils.getDaysBetweenDates(currentDate, installDate);
if (differenceInDate > 0 && differenceInDate % 7 == 0) {
CallYourConditionHere();
}
}
//This method will calculate difference in days and return the difference in days
@SuppressLint("SimpleDateFormat")
public static int getDaysBetweenDates(String currentDate, String installDate) {
formatConversion = new SimpleDateFormat(FORMAT_DATE_DD_MMM_YY);
try {
Date dateCurrent = formatConversion.parse(currentDate);
Date dateAfterSevenDays;
//take care of previous app version's date formats
try {
dateAfterSevenDays = formatConversion.parse(installDate);
} catch (ParseException e) {
dateAfterSevenDays = getCalObjFromDefaultDate(installDate).getTime();
}
differenceInDate = dateCurrent.getTime() - dateAfterSevenDays.getTime();
days = (int) (differenceInDate / (1000 * 60 * 60 * 24));
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
以下方法计算两个日期之间的周数。
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = dateFormat.parse("2019-01-01");
Date endDate = dateFormat.parse("2019-01-29");
int noOfWeek = calculateWeekNo(startDate, endDate);
//Option 1
public int calculateWeekNo(Date start, Date end) {
Calendar cal = new GregorianCalendar();
cal.setTime(start);
int weeks = 0;
while (cal.getTime().before(end)) {
cal.add(Calendar.WEEK_OF_YEAR, 1);
weeks++;
}
return weeks;
}
// Option 2
public static int calculateWeekNo(Date start, Date end) {
Calendar a = new GregorianCalendar();
Calendar b = new GregorianCalendar();
a.setTime(start);
b.setTime(end);
return b.get(Calendar.WEEK_OF_YEAR) - a.get(Calendar.WEEK_OF_YEAR);
}
你好,我正在写一个代码,它可以显示当前时间和登录时间之间的周间隔,
我正在添加一项功能以节省登录时间。
public void setWeek() {
Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateToStr = format.format(today);
editor.putString("week", dateToStr).commit();
}
public String getWeek() {
return pref.getString("week", null);
}
这会帮助你。我使用此条件从输入日期获取天数,并检查天数是否为 运行 我的条件
call conditionForWeek(){
calendarToday = Calendar.getInstance();
formatInstallDate = new SimpleDateFormat(FORMAT_DATE_DD_MMM_YY, Locale.ENGLISH);
currentDate = formatInstallDate.format(calendarToday.getTime());
installDate = PreferencesManager.getInstance().getString(USER_DATE_OF_INSTALL);
int differenceInDate = PregnancyAppUtils.getDaysBetweenDates(currentDate, installDate);
if (differenceInDate > 0 && differenceInDate % 7 == 0) {
CallYourConditionHere();
}
}
//This method will calculate difference in days and return the difference in days
@SuppressLint("SimpleDateFormat")
public static int getDaysBetweenDates(String currentDate, String installDate) {
formatConversion = new SimpleDateFormat(FORMAT_DATE_DD_MMM_YY);
try {
Date dateCurrent = formatConversion.parse(currentDate);
Date dateAfterSevenDays;
//take care of previous app version's date formats
try {
dateAfterSevenDays = formatConversion.parse(installDate);
} catch (ParseException e) {
dateAfterSevenDays = getCalObjFromDefaultDate(installDate).getTime();
}
differenceInDate = dateCurrent.getTime() - dateAfterSevenDays.getTime();
days = (int) (differenceInDate / (1000 * 60 * 60 * 24));
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
以下方法计算两个日期之间的周数。
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = dateFormat.parse("2019-01-01");
Date endDate = dateFormat.parse("2019-01-29");
int noOfWeek = calculateWeekNo(startDate, endDate);
//Option 1
public int calculateWeekNo(Date start, Date end) {
Calendar cal = new GregorianCalendar();
cal.setTime(start);
int weeks = 0;
while (cal.getTime().before(end)) {
cal.add(Calendar.WEEK_OF_YEAR, 1);
weeks++;
}
return weeks;
}
// Option 2
public static int calculateWeekNo(Date start, Date end) {
Calendar a = new GregorianCalendar();
Calendar b = new GregorianCalendar();
a.setTime(start);
b.setTime(end);
return b.get(Calendar.WEEK_OF_YEAR) - a.get(Calendar.WEEK_OF_YEAR);
}