JAVA SE: Using Array to display or list days of the week

 


JAVA SE: Using Array to display or list days of the week

  METHOD 1:


//How to display or get list of week days names in Java in full or short format.

//Using Array Method

//www.frenartechweb.com

 

package weekdayslist.java;

import java.text.DateFormatSymbols;

public class WeekDaysListJava {

    public static void main(String[] args) {

 

                        // Create a DateFormatSymbols instance

                        DateFormatSymbols dfs = new DateFormatSymbols();

 

                        // DateFormatSymbols instance has a method by name

                        // getWeekdays() which returns back an array of week days name

                        String[] arrayOfWeekDaysNames = dfs.getWeekdays();

 

                        // loop over each day name and printing on the console

                        for( String dayName : arrayOfWeekDaysNames ) {

                                    System.out.println(dayName);

                        }


                        dfs = new DateFormatSymbols();

 

                        // DateFormatSymbols instance has a method by name

                        // getShortWeekdays() which returns back an array of week days name in short forms

                        String[] arrayOfShortWeekDaysNames = dfs.getShortWeekdays();

 

                        // loop over each day name and printing on the console

                        for( String dayName : arrayOfShortWeekDaysNames ) {

                                    System.out.println(dayName);

                        }

    }

}

 

Output of Program:

run:

 

Sunday

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

 

Sun

Mon

Tue

Wed

Thu

Fri

Sat

BUILD SUCCESSFUL (total time: 0 seconds)


METHOD 2:

To list weekday names, use the getWeekdays () from the DateFormatSymbols class in Java.

DateFormatSymbols is a class for encapsulating localizable date-time formatting data.

Get weekday month names in an array −

String[] days = new DateFormatSymbols().getWeekdays ();

Display the weekdays −

for (int i = 0; i < days.length; i++) {

   String weekday = days[i];

   System.out.println(weekday);

}


The following is an example −

EXAMPLE;


package daysoftheweek;

/**

 *

 * @author FRENARTECHWEB

 */


    /**

     * @param args the command line arguments

     */

        // TODO code application logic here

        import java.text.DateFormatSymbols;

public class DAYSOFTHEWEEK {

   public static void main(String[] args) {

      String[] days = new DateFormatSymbols().getWeekdays();

      for (int i = 0; i < days.length; i++) {

         String weekday = days[i];

         System.out.println(weekday);

      }

   }

}


OUTPUT

run:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
BUILD SUCCESSFUL (total time: 0 seconds)


METHOD 3:

Using Array to display or list days of the week in order.


package weekdaysposition;


/**

 *

 * @author FRENARTECH

 */


    /**

     * @param args the command line arguments

     */

           // TODO code application logic here

        

// Importing generic Classes/Files

import java.io.*;

 

public class WEEKDAYSPOSITION {

 

    // Main driver method

    public static void main(String[] args)

    {

        // Creating a list of weekdays

        String[] weekdays

            = { "Monday", "Tuesday", "Wednesday",

                "Thursday", "Friday" };

        // Iterating 5 times only

        // since there are 5 weekdays

        for (int d = 1; d < 6; d++) {

            // Message printing weekdays in calendar year

            System.out.println(d + "th weekday - " + weekdays[d - 1]);

        }

    }

}

OUTPUT

run:

1th weekday - Monday
2th weekday - Tuesday
3th weekday - Wednesday
4th weekday - Thursday
5th weekday - Friday
BUILD SUCCESSFUL (total time: 0 seconds)




---------------------------------------------------------------------------------------------------------------------------- 

We appreciate you visiting our Quizzes and Tutorial Blog! I sincerely hope the details were useful. If you have any other information or believe I may have missed something crucial, kindly let us know in the comments area below. As always, feel free to share the link to this article on your social media platforms. Also, don't forget to subscribe to our Telegram channel and like our Facebook and other media pages to show your support!


If you have any questions, then you should join our Telegram chat group. We will help you solve your problems with blogging and SEO.

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.