Tuesday, January 31, 2017

Find start and end date of week from input date in java

// set the date e.g. 12 June 2015
Calendar cal = Calendar.getInstance();
cal.set(2015, 6 – 1, 12);

// “calculate” the start date of the week
Calendar first = (Calendar) cal.clone();
first.add(Calendar.DAY_OF_WEEK, first.getFirstDayOfWeek() – first.get(Calendar.DAY_OF_WEEK));

// and add six days to the end date
Calendar last = (Calendar) first.clone();
last.add(Calendar.DAY_OF_YEAR, 6);

// print the result
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd”);
System.out.println(df.format(first.getTime()) + ” -> ” +
df.format(last.getTime()));

OUTPUT:
2015-06-08 -> 2015-06-14

No comments :

Post a Comment