Results 1 to 5 of 5

Thread: How to get the current month name using java program?

  1. #1
    Join Date
    Nov 2009
    Posts
    67

    How to get the current month name using java program?

    Hello to all,
    I am last year B.Sc.I.T. student. We got assignment from our sir. In that assignment there is one question How to get the current month name using java program? I tried various method but none of them worked out. Anyone know how to solve this program. Please help me.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to get the current month name using java program?

    We have to use ava.util.Calendar class to get the current month name.
    Let's see the code below:


    Code:
    import java.util.Calendar;
     
    public class GetMonthNameExample
    {
        public static void main(String[] args)
        {
            String[] mName = {"January", "February",
                "March", "April", "May", "June", "July",
                "August", "September", "October", "November",
                "December"};
     
            Calendar c = Calendar.getInstance();
            String m = mName[c.get(Calendar.MONTH)];
     
            System.out.println(" Current Month name: " + m);
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to get the current month name using java program?

    Hey it is very easy process to get current month name using java program,. Just go through following code.


    Code:
    import java.text.DateFormat;
    import java.util.Date;
    
    public class DateFormatExample1 {
    
        public static void main(String[] args) {
            
            Date n = new Date();
     System.out.println(" 1. " + n.toString());
    
       System.out.println(" 2. " + DateFormat.getInstance().format(n));
    
          
            System.out.println(" 3. " + DateFormat.getTimeInstance().format(n));
            System.out.println(" 4. " +
                DateFormat.getDateTimeInstance().format(n));
    
           
            System.out.println(" 5. " +
                DateFormat.getTimeInstance(DateFormat.SHORT).format(n));
            System.out.println(" 6. " +
                DateFormat.getTimeInstance(DateFormat.MEDIUM).format(n));
            System.out.println(" 7. " +
                DateFormat.getTimeInstance(DateFormat.LONG).format(n));
    
          
            System.out.println(" 8. " + DateFormat.getDateTimeInstance(
                DateFormat.SHORT, DateFormat.SHORT).format(n));
            System.out.println(" 9. " + DateFormat.getDateTimeInstance(
                DateFormat.MEDIUM, DateFormat.SHORT).format(n));
            System.out.println("10. " + DateFormat.getDateTimeInstance(
                DateFormat.LONG, DateFormat.LONG).format(n));
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to get the current month name using java program?

    I have written following program to get current month name using java program. Just go through it.

    Code:
     1: import javax.swing.*; 
     import java.awt.*; 
    import java.util.*; 
    
     public class ClockPanel extends JPanel { 
         public ClockPanel() { 
             super(); 
             String currentTime = getTime(); 
             JLabel t = new JLabel("Time: "); 
             JLabel c = new JLabel(currentTime); 
             add(t); 
             add(c); 
        } 
     
         String getTime() { 
             String time; 
             
             Calendar n = Calendar.getInstance(); 
             int h = now.get(Calendar.HOUR_OF_DAY); 
            int min = now.get(Calendar.MINUTE); 
            int mon = now.get(Calendar.MONTH) + 1; 
           int d = now.get(Calendar.DAY_OF_MONTH); 
             int y = now.get(Calendar.YEAR); 
     
             String monthName = ""; 
             switch (mon) { 
                 case (1): 
                     monthName = "January"; 
                     break; 
                 case (2): 
                     monthName = "February"; 
                     break; 
                 case (3): 
                    monthName = "March"; 
                     break; 
                 case (4): 
                     monthName = "April"; 
                     break; 
                 case (5):  
    
                    monthName = "May"; 
                     break; 
                 case (6): 
                   monthName = "June"; 
                    break; 
                 case (7): 
                     monthName = "July"; 
                     break; 
                 case (8): 
                     monthName = "August"; 
                     break; 
                 case (9): 
                     monthName = "September"; 
                     break; 
                 case (10): 
                     monthName = "October"; 
                     break; 
                 case (11): 
                     monthName = "November"; 
                    break; 
                 case (12): 
                     monthName = "December"; 
             } 
            time = monthName + " " + d + ", " + y + " " 
                 + h + ":" + min; 
             return t; 
         } 
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to get the current month name using java program?

    Code:
    import java.util.*;
    
    public class GetMonthNameExample {
    
        public static void main(String args[]) {
    
            String[] currentmonthName = {"January", "February",
                "March", "April", "May", "June", "July",
                "August", "September", "October", "November",
                "December"
            };
    
            Calendar cal1 = Calendar.getInstance();
            String month1 = currentmonthName[cal1.get(Calendar.MONTH)];
            System.out.println("Month name: " + month1);
        }
    }

Similar Threads

  1. How to disable Current Java for Mac OS X 10.6 update 8
    By Ataa in forum Operating Systems
    Replies: 6
    Last Post: 25-04-2012, 05:19 AM
  2. Replies: 5
    Last Post: 23-04-2011, 10:19 PM
  3. Current version plugin java deployment toolkit 6.0.170
    By fragranz in forum Networking & Security
    Replies: 6
    Last Post: 15-07-2010, 06:14 AM
  4. Getting Current Date in Java
    By Level8 in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 05:26 PM
  5. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,270,042.93396 seconds with 17 queries