Calendar Function In Java Language
Calendar :
This class is useful to handle date and time We can create an object to Calendar class as :
Syntax
Calendar cl = CalendargetInstance ();
Method |
Description |
int get(Constant) |
This method returns the value of the given Calendar constant
Examples of Constants are CalendarDATE, CalendarMONTH, CalendarYEAR,
CalendarMINUTE, CalendarSECOND,
CalendarHour |
void set(int field, int value) |
This method sets the given field in Calendar Object to the given value For example, clset(CalendarDATE,15); |
String toString() |
This method returns the String representation of the Calendar object |
boolean equals(Object obj) |
This method compares the Calendar object with another object obj and returns true if they are same, otherwise false |
Program 10 : Write a program to display System time and date //To display system time and date
CODE/PROGRAM/EXAMPLE
import javautil*;
class Cal {
public static void main(String args[]) {
Calendar cl = CalendargetInstance ();
//Retrieve Date
int dd = clget (CalendarDATE);
int mm = clget (CalendarMONTH);
++mm;
int yy = clget (CalendarYEAR);
System.out.println ("Current Date is : " + dd + "-" + mm + "-" + yy );
//Retrieve Time
int hh = clget (CalendarHOUR);
int mi = clget (CalendarMINUTE);
int ss = clget (CalendarSECOND);
System.out.println ("Current Time is : " + hh + ":" + mi + ":" +ss);
}
}
O/P :
Date Class is also useful to handle date and time Once Date class object is created, it should be formatted using the following methods of DateFormat class of javatext packageWe can create an object to Date class as:
Syntax
Date dd = new Date ();
Once Date class object is created, it should be formatted using the methods of DateFormat class of javatext package
DateFormat class Methods :
Syntax
DateFormat fmt = DateFormatgetDateInstance(formatconst, region);
This method is useful to store format information for date value into DateFormat object fmt
Syntax
DateFormat fmt = DateFormatgetTimeInstance(formatconst, region);
This method is useful to store format information for time value into DateFormat object fmt
Syntax
DateFormat fmt = DateFormatgetDateTimeInstance(formatconst, formatconst, region);
This method is useful to store format information for date value into DateFormat object fmt
Formatconst |
Example (region=LocaleUK) |
DateFormatFULL |
03 september 2007
19:43:14 O'Clock GMT + 05:30 |
DateFormatLONG |
03 september 2007
19:43:14 GMT + 05:30 |
DateFormatMEDIUM |
03-sep-07 19:43:14 |
DateFormatSHORT |
03/09/07 19:43 |
Program 11 : Write a program that shows the use of Date class
CODE/PROGRAM/EXAMPLE
//Display System date and time using Date class
import javautil*;
import javatext*;
class MyDate {
public static void main(String args[])
{
Date d = new Date ();
DateFormat fmt = DateFormatgetDateTimeInstance (DateFormatMEDIUM, DateFormatSHORT, LocaleUK);
String str = fmtformat (d); Systemoutprintln (str);
}
}
OutPut