Tuesday 31 July 2018

How to get current month and day using Java.util.calendar class


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=5nMojKZe4Iw&list=UUhwKlOVR041tngjerWxVccw

CalendarDemo1.java

import java.util.Calendar;

public class CalendarDemo1
{
    public static void main(String[] args)
    {
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.getTime());
       
        int month = cal.get(Calendar.MONTH);
        if (month == Calendar.JANUARY)
        {
            System.out.println("JANUARY");
        }
        if (month == Calendar.FEBRUARY)
        {
            System.out.println("FEBRUARY");
        }
        if (month == Calendar.MARCH)
        {
            System.out.println("MARCH");
        }
        if (month == Calendar.APRIL)
        {
            System.out.println("APRIL");
        }
        if (month == Calendar.MAY)
        {
            System.out.println("MAY");
        }
        if (month == Calendar.JUNE)
        {
            System.out.println("JUNE");
        }
        if (month == Calendar.JULY)
        {
            System.out.println("JULY");
        }
        if (month == Calendar.AUGUST)
        {
            System.out.println("AUGUST");
        }
        if (month == Calendar.SEPTEMBER)
        {
            System.out.println("SEPTEMBER");
        }
        if (month == Calendar.OCTOBER)
        {
            System.out.println("OCTOBER");
        }
        if (month == Calendar.NOVEMBER)
        {
            System.out.println("NOVEMBER");
        }
        if (month == Calendar.DECEMBER)
        {
            System.out.println("DECEMBER");
        }
    }
}

Output

Sat Jul 07 09:18:04 IST 2018
JULY

CalendarDemo2.java

import java.util.Calendar;

public class CalendarDemo2
{
    public static void main(String[] args)
    {
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.getTime());

        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (Calendar.SUNDAY == dayOfWeek)
        {
            System.out.println("SUNDAY");
        }
        if (Calendar.MONDAY == dayOfWeek)
        {
            System.out.println("MONDAY");
        }
        if (Calendar.TUESDAY == dayOfWeek)
        {
            System.out.println("TUESDAY");
        }
        if (Calendar.WEDNESDAY == dayOfWeek)
        {
            System.out.println("WEDNESDAY");
        }
        if (Calendar.THURSDAY == dayOfWeek)
        {
            System.out.println("THURSDAY");
        }
        if (Calendar.FRIDAY == dayOfWeek)
        {
            System.out.println("FRIDAY");
        }
        if (Calendar.SATURDAY == dayOfWeek)
        {
            System.out.println("SATURDAY");
        }
    }

}

Output

Sat Jul 07 09:18:11 IST 2018
SATURDAY

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/CalendarDemo_display_month_dayofweek.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava_2018/CalendarDemo_display_month_dayofweek

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/b1e669f4abac38a0eb65bfeb213dca88c58230cc/BasicJava_2018/CalendarDemo_display_month_dayofweek/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment