How to display day names and number from current date in android

1k views Asked by At

Hi I am implementing a feature called calendar. Now From current month name and current year from this how to display day names and day number .

Code:

cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            Date startDate = cal.getTime();
            cal.add(Calendar.DATE, 6);
            Date endDate = cal.getTime();

            Log.d(TAG, "Start Date = " + startDate);
            Log.d(TAG, "End Date = " + endDate);

Example: Dec 2020

Expected output :

Wed -9
Thu -10
Fri -11
Sat- 12
Sun- 13

Can any one help me How to achieve my goal

3

There are 3 answers

4
Arvind Kumar Avinash On BEST ANSWER

You can do it with a for loop declared with

  1. Initial value as today
  2. Terminating value as the end of the month
  3. Increment value of one day

and inside the body of the loop, print the 3-letter weekday name (i.e. EEE) and the day-of-month (i.e. d) of the date formatted with DateTimeFormatter.ofPattern("EEE - d", Locale.ENGLISH).

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE - d", Locale.ENGLISH);
        LocalDate today = LocalDate.now();
        for (LocalDate date = today; !date.isAfter(today.with(TemporalAdjusters.lastDayOfMonth())); date = date
                .plusDays(1)) {
            System.out.println(date.format(formatter));
        }
    }
}

Output:

Fri - 11
Sat - 12
Sun - 13
...
...
...
Wed - 30
Thu - 31

Note: all the dates of this month, starting from today, are of two digits and therefore it doesn't matter whether you use d or dd for day-of-month. However, if your requirement is to print the day-of-month always in two digits (e.g. 01 instead of 1), use dd in the DateTimeFormatter pattern.


Some useful notes:

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.

For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.

If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

4
Anonymous On

java.time and desugaring

Consider using java.time, the modern Java date and time API, for your date and calendar work.

    DateTimeFormatter dayFormatter
            = DateTimeFormatter.ofPattern("EEE - ppd", Locale.ENGLISH);
    
    LocalDate currentDay = YearMonth.now(ZoneId.systemDefault()).atDay(1);
    LocalDate firstInNextMonth = currentDay.plusMonths(1);
    while (currentDay.isBefore(firstInNextMonth)) {
        System.out.println(currentDay.format(dayFormatter));
        currentDay = currentDay.plusDays(1);
    }

Output when run in December 2020 (excerpt):

Tue -  1
Wed -  2
Thu -  3
Fri -  4
Sat -  5
Sun -  6
Mon -  7
Tue -  8
Wed -  9
Thu - 10
Fri - 11
(--- cut ---)
Tue - 29
Wed - 30
Thu - 31

For the right-aligned day of the month in the print-out, I have used ppd in the format pattern string. For display in a phone app, you may prefer just d.

Edit: For the days of the coming week initialize the dates like this:

    LocalDate currentDay = LocalDate.now(ZoneId.systemDefault());
    LocalDate endDateExclusive = currentDay.plusWeeks(1);

When running the code in the last week of the month you may have days of the following month included, which doesn’t seem to agree with what you said: Now From current month name and current year …. I hope you’ll find a satisfactory solution.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with sub-packages.

Links

1
akhil nair On

This would do the work.

public void printDaysOfMonth(int year, int month) {

    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("EEE-d", Locale.ENGLISH);
    calendar.set(year, month, 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    while (calendar.get(Calendar.MONTH) == month) {
        String fEndDate = formatter.format(calendar.getTime());
        System.out.println(fEndDate);
        calendar.add(Calendar.DATE, 1);
    }

}

Note: January-0 and December - 11