Java Tutorial/Data Type/Calendar

Материал из Java эксперт
Перейти к: навигация, поиск

Change multiple components at the same time

   <source lang="java">

public void set (int year, int month, int date)

    public void set (int year, int month, int date, int hour, int minute, int second)</source>
   
  
 
  



Check for a Leap Year

   <source lang="java">

import java.text.ParseException; public class MainClass {

 public static void main(String[] args) throws ParseException {
   System.out.println(isLeapYear(2000));
 }
 public static boolean isLeapYear(int year) {
   if (year < 0) {
     return false;
   }
   if (year % 400 == 0) {
     return true;
   } else if (year % 100 == 0) {
     return false;
   } else if (year % 4 == 0) {
     return true;
   } else {
     return false;
   }
 }

}</source>





Checking for a Leap Year: using GregorianCalendar

   <source lang="java">

import java.text.ParseException; import java.util.GregorianCalendar; public class MainClass {

 public static void main(String[] args) throws ParseException {
   System.out.println(isLeapYear(2000));
 }
 public static boolean isLeapYear(int year) {
   GregorianCalendar gcal = new GregorianCalendar();
   return gcal.isLeapYear(year);
 }

}</source>





Construct a Calendar object by using the setTime method

   <source lang="java">

import java.util.Calendar; import java.util.Date; public class MainClass {

 public static void main(String[] args) {
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(new Date());
 }

}</source>





Create a Date object using the Calendar class

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   int year = 2009;
   int month = 0; // January
   int date = 1;
   Calendar cal = Calendar.getInstance();
   cal.clear();
   cal.set(Calendar.YEAR, year);
   cal.set(Calendar.MONTH, month);
   cal.set(Calendar.DATE, date);
   java.util.Date utilDate = cal.getTime();
   System.out.println(utilDate);
 }

} //2009-01-01</source>





Create instance of java.sql.Date from Calendar.getTimeInMillis()

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   int year = 2009;
   int month = 0; // January
   int date = 1;
   Calendar cal = Calendar.getInstance();
   cal.clear();
   cal.set(Calendar.YEAR, year);
   cal.set(Calendar.MONTH, month);
   cal.set(Calendar.DATE, date);
   java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());
   System.out.println(sqlDate);
 }

} //2009-01-01</source>





Display Day of Week using Java Calendar

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar now = Calendar.getInstance();
   System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"
       + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
   String[] strDays = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thusday",
       "Friday", "Saturday" };
   // Day_OF_WEEK starts from 1 while array index starts from 0
   System.out.println("Current day is : " + strDays[now.get(Calendar.DAY_OF_WEEK) - 1]);
 }

}</source>





Display full date time

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar now = Calendar.getInstance();
   System.out.println("Current full date time is : " + (now.get(Calendar.MONTH) + 1) + "-"
       + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR) + " "
       + now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":"
       + now.get(Calendar.SECOND) + "." + now.get(Calendar.MILLISECOND));
 }

}</source>





Display Month of year using Java Calendar

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar now = Calendar.getInstance();
   System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"
       + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
   String[] strMonths = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
       "Sep", "Oct", "Nov", "Dec" };
   System.out.println("Current month is : " + strMonths[now.get(Calendar.MONTH)]);
 }

}</source>





Get current date, year and month

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar now = Calendar.getInstance();
   // 
   System.out.println("Current Year is : " + now.get(Calendar.YEAR));
   // month start from 0 to 11
   System.out.println("Current Month is : " + (now.get(Calendar.MONTH) + 1));
   System.out.println("Current Date is : " + now.get(Calendar.DATE));
 }

}</source>





Get current time information

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar now = Calendar.getInstance();
   
   System.out.println("Current Hour in 12 hour format is : " + now.get(Calendar.HOUR));
   System.out.println("Current Hour in 24 hour format is : " + now.get(Calendar.HOUR_OF_DAY));
   System.out.println("Current Minute is : " + now.get(Calendar.MINUTE));
   System.out.println("Current Second is : " + now.get(Calendar.SECOND));
   System.out.println("Current Millisecond is : " + now.get(Calendar.MILLISECOND));
 }

}</source>





Get day of week

   <source lang="java">

import java.util.Calendar;

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar calendar = Calendar.getInstance();
   calendar.set(Calendar.YEAR, 2007);
   calendar.set(Calendar.DAY_OF_YEAR, 180);
   // See the full information of the calendar object.
   System.out.println(calendar.getTime().toString());
   // Get the weekday and print it
   int weekday = calendar.get(Calendar.DAY_OF_WEEK);
   System.out.println("Weekday: " + weekday);
 }

}</source>





Get Days Of The Week for different locale

   <source lang="java">

import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.Locale; public class DaysOfTheWeek {

 public static void main(String argv[]) {
   Locale usersLocale = Locale.getDefault();
   DateFormatSymbols dfs = new DateFormatSymbols(usersLocale);
   String weekdays[] = dfs.getWeekdays();
   Calendar cal = Calendar.getInstance(usersLocale);
   int firstDayOfWeek = cal.getFirstDayOfWeek();
   int dayOfWeek;
   for (dayOfWeek = firstDayOfWeek; dayOfWeek < weekdays.length; dayOfWeek++)
     System.out.println(weekdays[dayOfWeek]);
   for (dayOfWeek = 0; dayOfWeek < firstDayOfWeek; dayOfWeek++)
     System.out.println(weekdays[dayOfWeek]);
 }

}</source>





Get the last date of a month

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar calendar = Calendar.getInstance();
   int lastDate = calendar.getActualMaximum(Calendar.DATE);
   System.out.println("Date     : " + calendar.getTime());
   System.out.println("Last Date: " + lastDate);
 }

}</source>





Get the last day of a month

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar calendar = Calendar.getInstance();
   int lastDate = calendar.getActualMaximum(Calendar.DATE);
   calendar.set(Calendar.DATE, lastDate);
   int lastDay = calendar.get(Calendar.DAY_OF_WEEK);
   System.out.println("Last Date: " + calendar.getTime());
   System.out.println("Last Day : " + lastDay);
 }

}</source>





Get the number of days in that month

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] argv) throws Exception {
   Calendar cal = Calendar.getInstance();
   int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28
 }

}</source>





Get time in milliseconds using Java Calendar

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar now = Calendar.getInstance();
   System.out.println("Current milliseconds since Jan 1, 1970 are :" + now.getTimeInMillis());
 }

}</source>





Get Week of month and year using Java Calendar

   <source lang="java">

import java.util.Calendar; public class Main {

 public static void main(String[] args) {
   Calendar now = Calendar.getInstance();
   System.out.println("Current week of month is : " + now.get(Calendar.WEEK_OF_MONTH));
   System.out.println("Current week of year is : " + now.get(Calendar.WEEK_OF_YEAR));
   now.add(Calendar.WEEK_OF_MONTH, 1);
   System.out.println("date after one year : " + (now.get(Calendar.MONTH) + 1) + "-"
       + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
 }

}</source>





The java.util.Calendar Class

To create a java.util.Calendar object, you have to use its static getInstance method.



   <source lang="java">

public static Calendar getInstance () public static Calendar getInstance (Locale locale)</source>





To change a date/time component, use its set method: public void set (int field, int value)

   <source lang="java">

import java.util.Calendar; public class MainClass {

 public static void main(String[] args) {
   Calendar calendar = Calendar.getInstance();
   calendar.set(Calendar.MONTH, Calendar.DECEMBER);
 }

}</source>





To obtain a date part, such as the hour, the month, or the year, use the get method

   <source lang="java">

public int get (int field)</source>



2007
0
30
8
32
42
805


Try month in a leap year

   <source lang="java">

import java.util.Calendar; import java.util.GregorianCalendar; public class Main {

 public static void main(String[] argv) throws Exception {
   Calendar cal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1);
   int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 29
 }

}</source>





Using the Calendar Class to Display Current Time in Different Time Zones

   <source lang="java">

import java.util.Calendar; import java.util.TimeZone; public class Main {

 public static void main(String[] args) {
   Calendar calNewYork = Calendar.getInstance();
   calNewYork.setTimeZone(TimeZone.getTimeZone("America/New_York"));
   System.out.println("Time in New York: " + calNewYork.get(Calendar.HOUR_OF_DAY) + ":"
       + calNewYork.get(Calendar.MINUTE));
 }

} //Time in New York: 11:51</source>