Java Calendar class

Introduction

The abstract Calendar class deals with a date and time values.

The following program demonstrates several Calendar methods:

// Demonstrate Calendar
import java.util.Calendar;

public class Main {
  public static void main(String args[]) {
    String months[] = {//from w w w .j  a  v  a2s  . c o m
             "Jan", "Feb", "Mar", "Apr", 
             "May", "Jun", "Jul", "Aug",
             "Sep", "Oct", "Nov", "Dec"};

    // Create a calendar initialized with the
    // current date and time in the default
    // locale and timezone.
    Calendar calendar = Calendar.getInstance();

    // Display current time and date information.
    System.out.print("Date: ");
    System.out.print(months[calendar.get(Calendar.MONTH)]);
    System.out.print(" " + calendar.get(Calendar.DATE) + " ");
    System.out.println(calendar.get(Calendar.YEAR));

    System.out.print("Time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));

    // Set the time and date information and display it.
    calendar.set(Calendar.HOUR, 10);
    calendar.set(Calendar.MINUTE, 29);
    calendar.set(Calendar.SECOND, 22);

    System.out.print("Updated time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));
  }
}

import java.text.DateFormatSymbols;
import java.util.Calendar;

public class Main {

   public static void main(String[] args) {
      Calendar gCal = Calendar.getInstance();

      // Month is based upon a zero index, January is equal to 0,
      // so we need to add one to the month for it to be in
      // a standard format
      int month = gCal.get(Calendar.MONTH) + 1;
      int day = gCal.get(Calendar.DATE);
      int yr = gCal.get(Calendar.YEAR);

      String dateStr = month + "/" + day + "/" + yr;
      System.out.println(dateStr);

      int dayOfWeek = gCal.get(Calendar.DAY_OF_WEEK);

      // Print out the integer value for the day of the week
      System.out.println(dayOfWeek);

      int hour = gCal.get(Calendar.HOUR);
      int min = gCal.get(Calendar.MINUTE);
      int sec = gCal.get(Calendar.SECOND);

      // Print out the time
      System.out.println(hour + ":" + min + ":" + sec);

      // Create new DateFormatSymbols instance to obtain the String
      // value for dates
      DateFormatSymbols symbols = new DateFormatSymbols();
      String[] days = symbols.getWeekdays();
      System.out.println(days[dayOfWeek]);

      // Get crazy with the date!
      int dayOfYear = gCal.get(Calendar.DAY_OF_YEAR);
      System.out.println(dayOfYear);

      // Print the number of days left in the year
      System.out.println("Days left in " + yr + ": " + (365 - dayOfYear));

      int week = gCal.get(Calendar.WEEK_OF_YEAR);
      // Print the week of the year
      System.out.println(week);/*from w  w  w .  ja  v a  2  s.  c om*/
   }
}



PreviousNext

Related