在我的示例项目中,我必须在星期一下周星期日实施文本视图(如5月6日>> 12 My)。点击下一个按钮,它必须显示下周开始日期和结束日期(如5月13日>> 5月19日)。我已使用以下代码实现了初始周视图
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);
点击下周按钮我已经完成了这个但是静态它只是一次尝试尝试 :)
onclick将调用此函数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());
}
请告诉我如何显示下周的开始和结束日期?
答案 0 :(得分:3)
这是您的解决方案。
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);
按钮上的实施更新
编写一个方法,将weekNumber视为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;
}
现在宣布静态归档为
private static int weekNumber = -1;
并在按钮点击下面写下代码
weekNumber = weekNumber + 1;
gestureEvent.setText(getNextWeek(weekNumber));
这样可行。
快乐编码:)
答案 1 :(得分:0)
使用Joda-Time 2.3库可以更轻松地完成这种日期工作。
如果您确实只想要没有时间组件的日期,请修改此代码以使用LocalDate类而不是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 );
跑步时......
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