问题
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 date (like 13 May >> 19 May). I have implemented the intial week view with the following code
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat df = new SimpleDateFormat("dd MMMM");
String printDate = df.format(c.getTime());
//gestureEvent.setText(reportDate);
c.add(Calendar.DAY_OF_WEEK, 6);
String printDate2 = df2.format(c.getTime());
gestureEvent.setText(reportDate +" >> "+reportDate2);
on click of next week button i have done this but it static it was just an attempt attempt :)
onclick will call this function goNextWeek()
public void goNextWeek()
{
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
c.add(Calendar.DAY_OF_WEEK, 6);
System.out.println("End Date : " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd MMMM");
String reportDate = df.format(c.getTime());
gestureEvent.setText(reportDate);
c.add(Calendar.DAY_OF_WEEK, dates);
c.add(Calendar.DAY_OF_WEEK, 1);
System.out.println("End Date asdfadf: " + c.getTime());
}
please tell me how to show next week start and end date?
回答1:
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 :)
回答2:
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
来源:https://stackoverflow.com/questions/16434108/next-week-implementation-in-android