Tuesday 3 July 2018

How to use getInstance methods of Java.util.calendar class | Java Date and Time


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

CalendarDemo1.java

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * public static Calendar getInstance(Locale aLocale)
 *
 * Gets a calendar using the default time zone and specified locale.
 * The Calendar returned is based on the current time in the default
 * time zone with the given locale.
 *
 * Parameters:
 *
 * aLocale - the locale for the week data
 *
 * Returns:
 *
 * a Calendar.
 */


public class CalendarDemo1
{
    public static void main(String[] args)
    {

        Calendar cal = Calendar.getInstance(Locale.FRENCH);

        Date date = cal.getTime();
        System.out.print("Date and Time is = " + date);

    }

}

Output.txt

Date and Time is = Tue May 15 09:34:16 IST 2018

CalendarDemo2.java

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * public static Calendar getInstance(TimeZone zone)
 *
 * Gets a calendar using the specified time zone and default locale.
 * The Calendar returned is based on the current time in the given
 * time zone with the default FORMAT locale.
 *
 * Parameters:
 *
 * zone - the time zone to use
 *
 * Returns:
 *
 * a Calendar.
 *
 */

public class CalendarDemo2
{
    public static void main(String[] args)
    {
        TimeZone timeZone = TimeZone.getDefault();
        System.out.println(timeZone.getDisplayName());

        Calendar cal = Calendar.getInstance(timeZone);

        Date date = cal.getTime();
        System.out.print("Date and Time is = " + date);

    }

}

Output.txt

India Standard Time
Date and Time is = Tue May 15 09:34:25 IST 2018

CalendarDemo3.java

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * public static Calendar getInstance(TimeZone zone, Locale aLocale)
 *
 * Gets a calendar with the specified time zone and locale. The
 * Calendar returned is based on the current time in the given time
 * zone with the given locale.
 *
 * Parameters:
 *
 * zone - the time zone to use
 *
 * aLocale - the locale for the week data
 *
 * Returns:
 *
 * a Calendar.
 */


public class CalendarDemo3
{
    public static void main(String[] args)
    {
        Locale locale = Locale.US;
        System.out.println(locale);
   
        TimeZone timeZone = TimeZone.getDefault();
        System.out.println(timeZone.getDisplayName());

        Calendar cal = Calendar.getInstance(timeZone, locale);

        Date date = cal.getTime();
        System.out.print("Date and Time is = " + date);

    }

}

Output.txt

en_US
India Standard Time
Date and Time is = Tue May 15 09:34:35 IST 2018

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/08a35bef4ef43a64446d7182fc58e2891a472c27/BasicJava_2018/CalendarDemo_getInstance/?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