3

I'm using the java.time package of java to display week dates based on the current date in Android. I want Sunday through Saturday.

But the 'Time package link: ' was added from API 26. So, it is not supported for below API 26. So, I need an alternate solution for this. So that, I can run in below API 26.

Please provide me an alternate solution to display week dates based on the current date in Android.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
user3359125
  • 141
  • 1
  • 1
  • 6
  • Do you want dates of past 7 days (If today is Wednesday then last Thursday to today) or this Sunday to upcoming Saturday even if today is Tuesday. – Aaditya Brahmbhatt Sep 26 '18 at 06:52
  • Sunday to upcoming Saturday – user3359125 Sep 26 '18 at 07:20
  • 2
    Possible duplicate of [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). *ThreeTenABP* is the backport of java.time to earlier Android (before API level 26). – Ole V.V. Sep 26 '18 at 07:24
  • 2
    Post your clarifications as edits to the Question, not Comment. – Basil Bourque Sep 26 '18 at 15:16

3 Answers3

2

Try something like this.

    int NUM_DAYS = 7; // You can get as many dates as you want.

    Calendar calendar = Calendar.getInstance();

    calendar.setFirstDayOfWeek(Calendar.SUNDAY); // The first day you want dates from.

    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

    for (int i=0; i < NUM_DAYS; i++){ 

        Date date = calendar.getTime();

        System.out.println(new SimpleDateFormat("EEEE dd/MM/yyyy").format(date));

        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
2

So, it is not supported for below API 26.

You can use the java.time functionality in earlier Android. Use the back-port.

So, I need an alternate solution for this.

No, you don’t.

Keep your code as-is. The back-port carries nearly the same API. So you’ll need do little more than swap your import statements.

ThreeTen-Backport

Most of the java.time functionality is back-ported to Java 6 and Java 7 in the ThreeTen-Backport project.

This project is led by the same man, Stephen Colebourne, who leads the JSR 310 spec, the java.time implementation, and Joda-Time.

ThreeTenABP

The ThreeTen-Backport project is further adapted to Android specifically in the ThreeTenABP project.

Code

ZoneId z = ZoneId.systemDefault() ;  // Or ZoneId.of( "Africa/Tunis" )
LocalDate today = LocalDate.now( z ) ;

LocalDate localDate = today.with( org.threeten.bp.temporal.TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) ) ;
List< LocalDate > dates = new ArrayList<>( 7 ) ;
for( int i = 0 ; i < 7 ; i ++ ) {
    localDate = localDate.plusDays( i ) ;
    dates.add( localDate ) ;
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you for your answer. I have implemented your code in my app but I am getting some conflicts in dates. Please check this link: https://photos.app.goo.gl/bMrupWd47QSyAidN6. – user3359125 Oct 16 '18 at 17:59
1

Here is two Calendar objects.

Calendar calendarCurrent = Calendar.getInstance();
calendarCurrent.getTime(); // To get current date-time.

Calendar calendarWeek = Calendar.getInstance();
calendarWeek.add(Calendar.DAY_OF_MONTH, 7);
calendarWeek.getTime(); // To get after 7 day's date-time.

If you want to display it in calendar-view then you can set maxDate as below.

calendarView.setMinDate(calendarCurrent.getTimeInMillis());
calendarView.setMaxDate(calendarWeek.getTimeInMillis());
Akash Patel
  • 3,091
  • 3
  • 15
  • 23