2

在我的示例项目中,我必须在下周周一至周日在文本视图中实施(如 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()); 


}

请告诉我如何显示下周的开始和结束日期?

4

2 回答 2

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 作为参数..

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));

这将起作用。

快乐编码:)

于 2013-05-08T06:41:57.547 回答
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 );

运行时……</p>

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
于 2013-12-09T00:03:25.770 回答