2

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?

user2291423
  • 121
  • 2
  • 2
  • 14

2 Answers2

3

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

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • thanx :).. but can u tell me when i click again the next button i want to get the next week start and end of week like 06 May >> 12 May,13 May >> 19 May.... ? – user2291423 May 08 '13 at 06:47
  • @user2291423 see my update here.. If you have any confusion ask me – Pankaj Kumar May 08 '13 at 06:55
  • okey am using swipe and here's the implementation if((e2.getX() - e1.getX()) > sensitvity){ weekNumber = weekNumber + 1; System.out.println("weekNumber"+weekNumber); gestureEvent.setText(getNextWeek(weekNumber)); } – user2291423 May 08 '13 at 07:11
  • Check if value of weekNUmber does increments or not... Its hard to say without viewing your code.. You should make sure that value of weekNUmber must change on each call of getNextWeek() – Pankaj Kumar May 08 '13 at 07:18
  • its incrementing..i think this line mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); is making the date to start again..when comment this line an getting some output but its not correct – user2291423 May 08 '13 at 07:24
  • are u using the updated method getNextWeek()? I don't know whats going on your side... I checked the method in loop and its working fine... and yes no need to change in method... – Pankaj Kumar May 08 '13 at 07:30
  • ok @Pankaj let me check what wrong on me anyway thanks for the help friend – user2291423 May 08 '13 at 07:35
  • Its my pleasure... we all are here for helping each other. right? :) – Pankaj Kumar May 08 '13 at 07:39
  • Hey @pankaj i have interchanged 3 and 4 th lines and its working great !!thanks buddy..and its possible to implement previous week with this code right ? please change the code and accepting this as answer thanks buddy – user2291423 May 08 '13 at 07:53
  • Verify the update... and yes the same code will work for previous weeks but you have to pass weekNumber as negative value.. – Pankaj Kumar May 08 '13 at 07:59
0

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
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154