In my sample project i have to implement next week Monday to sunday in a text view (like 6 May >> 12 My). on click of next button it must show next week start date and end d
Here is your solution.
Calendar mCalendar = new GregorianCalendar();
mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat mDF = new SimpleDateFormat("dd MMMM");
String printDate = mDF.format(mCalendar.getTime());
mCalendar.add(Calendar.DAY_OF_MONTH, 6);
String printDate2 = mDF.format(mCalendar.getTime());
System.out.println(printDate + " >> " + printDate2);
gestureEvent.setText(printDate + " >> " + printDate2);
Update for implementation on button
Write a method, which will take weekNumber as params..
private static String getNextWeek(int weekFromToday) {
Calendar mCalendar = new GregorianCalendar();
mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
mCalendar.set(Calendar.WEEK_OF_YEAR,
mCalendar.get(Calendar.WEEK_OF_YEAR) + weekFromToday);
SimpleDateFormat mDF = new SimpleDateFormat("dd MMMM");
String printDate = mDF.format(mCalendar.getTime());
System.out.println(printDate);
//gestureEvent.setText(reportDate);
mCalendar.add(Calendar.DAY_OF_MONTH, 6);
String printDate2 = mDF.format(mCalendar.getTime());
System.out.println(printDate + " >> " + printDate2);
return printDate + " >> " + printDate2;
}
Now declaire a static filed as
private static int weekNumber = -1;
and write below code on button click
weekNumber = weekNumber + 1;
gestureEvent.setText(getNextWeek(weekNumber));
This will work.
Happy coding :)
This kind of date-time work is easier with the Joda-Time 2.3 library.
If you truly want date only without time component, modify this code to use LocalDate class rather than DateTime.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
DateTime today = new DateTime().withTimeAtStartOfDay();
// Monday
DateTime monday = today.withDayOfWeek( DateTimeConstants.MONDAY ).withTimeAtStartOfDay();
if ( !(monday.isAfter( today )) ) {
// If monday is today or earlier, move forward to future.
monday = monday.plusWeeks( 1 );
}
// Sunday
DateTime sunday = today.withDayOfWeek( DateTimeConstants.SUNDAY ).withTimeAtStartOfDay();
if ( !(sunday.isAfter( today )) ) {
// If sunday is today or earlier, move forward to future.
sunday = sunday.plusWeeks( 1 );
}
System.out.println( "today: " + today );
System.out.println( "Monday: " + monday );
System.out.println( "Sunday: " + sunday );
When run…
today: 2013-12-08T00:00:00.000-08:00
Monday: 2013-12-09T00:00:00.000-08:00
Sunday: 2013-12-15T00:00:00.000-08:00