Data Get By Current Year
List<java.util.Date> disable = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_YEAR, 1);
int month = cal.get(Calendar.YEAR);
Log.v(TAG, "months :"+month);
do {
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 2)
disable.add(cal.getTime());
cal.add(Calendar.DAY_OF_YEAR, 1);
} while (cal.get(Calendar.YEAR) == month);
SimpleDateFormat fmt = new SimpleDateFormat("EEE M/d/yyyy");
for (java.util.Date date : disable)
Log.v(TAG, "dates1 :" + fmt.format(date));
Data Get By Specific Two Dates
dataget();//method call
private void dataget() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
int count = 0;
try {
Date d1 = formatter.parse("2018-11-01 20:16:00");
Date d2 = formatter.parse("2018-12-30 20:16:00");
count = saturdaysundaycount(d1,d2);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Count of Sats & Sundays = "+count);
}
public static int saturdaysundaycount(Date d1, Date d2) {
List<java.util.Date> disable = new ArrayList<>();
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.DAY_OF_YEAR, 1);
int month = c1.get(Calendar.YEAR);
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
int sundays = 0;
int saturday = 0;
while (! c1.after(c2)) {
if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ){
disable.add(c1.getTime());
c1.add(Calendar.DAY_OF_YEAR, 1);
}
if(c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
disable.add(c1.getTime());
c1.add(Calendar.DAY_OF_YEAR, 1);
}
c1.add(Calendar.DATE, 1);
}
SimpleDateFormat fmt = new SimpleDateFormat("EEE M/d/yyyy");
for (java.util.Date date : disable)
Log.v(TAG, "dates :" + fmt.format(date));
System.out.println("Saturday Count = "+saturday);
System.out.println("Sunday Count = "+sundays);
return saturday + sundays;
}
No comments:
Post a Comment