How do I get a list of month names?

The example code below helps you to get all month names as an array of String. The first method, getMonths() return the full name string while the second method getShortMonths() return the short name of the month.

package org.kodejava.text;

import java.text.DateFormatSymbols;
import java.util.Locale;

public class MonthNames {
    public static void main(String[] args) {
        String[] months = new DateFormatSymbols().getMonths();
        for (String month : months) {
            System.out.println("month = " + month);
        }

        String[] shortMonths = new DateFormatSymbols().getShortMonths();
        for (String shortMonth : shortMonths) {
            System.out.println("shortMonth = " + shortMonth);
        }

        DateFormatSymbols dfs = new DateFormatSymbols(Locale.GERMANY);
        String[] germanyMonths = dfs.getMonths();
        for (String germanyMonth : germanyMonths) {
            System.out.println("germanyMonth = " + germanyMonth);
        }

        String[] germanyShortMonths = dfs.getShortMonths();
        for (String germanyShortMonth : germanyShortMonths) {
            System.out.println("germanyShortMonth = "
                    + germanyShortMonth);
        }
    }
}

The results of the code above are:

month = January
month = February
...
month = December
month = 
shortMonth = Jan
shortMonth = Feb
...
shortMonth = Dec
shortMonth = 
germanyMonth = Januar
germanyMonth = Februar
...
germanyMonth = Dezember
germanyMonth = 
germanyShortMonth = Jan
germanyShortMonth = Feb
...
germanyShortMonth = Dez
germanyShortMonth = 
Wayan

6 Comments

    • The DateFormatSymbols’s methods such as getMonths() or getShortMonths() shown in the snippet above returns an array of 13 strings. It’s because some calendars system can have 13 months.

      Because we are working with a system that have 12 months calendars system, it gives us an empty element at the end of the array.

      Reply
      • I see. One would think that the implementation of those methods would take into account the calendar system for the default locale. Apparently that’s not the case. It seems like an intern must have written those methods.

    • Hi Venkat,

      What about copying the first 12 elements of the array into another array. Something like:

      String[] newMonths = java.util.Arrays.copyOfRange(months, 0, 12);
      
      Reply
  1. public class FileDAO extends DaoBase implements ITreeDao {
    
        File rootDirectory = null;
    
        public FileDAO(File rootDirectory) {
            if (!rootDirectory.exists()) {
                throw new IllegalArgumentException("Directory " + rootDirectory.getAbsolutePath() + " doesn't exist");
            }
            this.rootDirectory = rootDirectory;
        }
    
        // it returns root directory(ADF)
        protected ITreeNode readRoot(ITree tree) {
            tree.setRoot(readNode(this.rootDirectory));
            TreeSorter.sortById(tree.getRoot());
            return tree.getRoot();
        }
    
        // Here readChildren(years) will appear
        protected Set<ITreeNode> readChildren(ITreeNode parentNode) {
            Set<ITreeNode> children = new HashSet<ITreeNode>();
    
            File parentDir = (File) parentNode.getObject();
            String[] files = parentDir.list();
            if (files == null)
                return children;
            for (int i = 0; i < files.length; i++) {
                File childFile = new File(parentDir.getAbsolutePath()
                        + File.separator + files[i]);
                ITreeNode child = readNode(childFile);
    
                child.setParentId(parentNode.getId());
                if (!childFile.exists())
                    continue;
                children.add(child);
            }
    
            // Sort here
            TreeSorter.sortByIdDescending(parentNode);
            return children;
        }
    
        // Here readGrandChildrens(Months) will Appear
        protected Set<ITreeNode> readGrandChildren(ITreeNode parentNode) {
    
            Set<ITreeNode> grandChildren = new HashSet<ITreeNode>();
            Iterator<ITreeNode> children = parentNode.getChildren().iterator();
            while (children.hasNext()) {
                ITreeNode child = (ITreeNode) children.next();
                grandChildren.addAll(readChildren(child));
                System.out.println("grandChildren......."+grandChildren);
            }
    
            TreeSorter.sortByName(parentNode);
            return grandChildren;
        }
    
        // Here to Reports will appear
        protected ITreeNode readNode(File file) {
            if (!file.exists())
                return null;
            ITreeNode node = null;
            String childType = file.isDirectory() ? "directory" : "file";
            if (childType.equals("file")) {
                node = new TreeNode(file.getAbsolutePath(), "<a href=\"openPdf.jsp?fileName=" + file.getAbsolutePath() + "\" target=_blank>" + file.getName() + "</a>", childType);
            } else {
                node = new TreeNode(file.getAbsolutePath(), file.getName(), childType);
            }
            node.setObject(file);
            return node;
        }
    
    }
    

    Here am getting months (readGrandChildren method) ascending order but I want calendar order like Jan, Feb, Mar, …., Dec.

    Please can anyone help me?

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.