0

I'm working on an Android app where I need to display the days of the week from the calendar. Can I do that using the calendar API ? or there is a library that can I use ? Thanks

yacine benzmane
  • 3,888
  • 4
  • 22
  • 32

4 Answers4

0

For date handling in Android, I recommend 310ABP, a port of the Java 8 new date APIs for Android.

Rafael Toledo
  • 5,599
  • 6
  • 23
  • 33
0

Use Calendar object to get these things done.

Thomas R.
  • 7,988
  • 3
  • 30
  • 39
0

You could use the JodaTime library to display the current day of the week.

LocalDate newDate = new LocalDate();
int dayOfWeek = newDate.getDayOfWeek();
morenoadan22
  • 234
  • 1
  • 9
0

or there is a library that can I use ?

Yes. Use the back-port of the java.time classes. See "Android" item below.

Using java.time

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

TemporalAdjuster

To get the first day of the week, (a) decide what is the first day of the week for you, (b) use a TemporalAdjuster implementation defined in TemporalAdjusters to get the date for a specific [DayOfWeek][6] enum object.

LocalDate ld = today.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) ) ;

To get a week’s worth of dates, add a day at a time.

LocalDate localDate = ld ; // Initialize our looping variable.
List<LocalDate> dates = new ArrayList<>( 7 ) ;
for( int i = 0 , i < 7 , i ++ ) {  // Loop seven times, to cover a week.
   localDate = localDate.plusDays( i ); 
   dates.add( localDate ) ;
}

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154