Advertisement
Methods | Description | static Calendar getInstance() | Returns an object of Calendar class with default locale and time zone. |
---|---|
int get(int calendarField) | Gets the specific information regarding the mentioned calendarField. |
Date getTime() | Returns a Date object converted from invoked Calendar object. |
void set(int calenderField, int value) | Sets a calendarField of Calendar object with a specified value |
void set(int year, int month, int dayOfMonth) | Sets a Calendar object with specified year, month, dayOfMonth |
void set(int year, int month, int dayOfMonth, int hours, int minutes) | Sets a Calendar object with specified year, month, dayOfMonth,hours,minutes |
void setTime(Date d) | Sets the invoked Calendar object with the value of a Date object. |
void add(int calendarField, int value) | Adds a value to the specific calendarField of Calendar object. |
import java.util.*;
class A
{
public static void main(String... ar)
{
Calendar c = Calendar.getInstance();
System.out.println(c);
}
}
java.util.GregorianCalendar[time=1500973726436,areFieldsSet=true,areAllFieldsSet
=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=198
00000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek
=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=6,WEEK_OF_YEAR=30,WEEK_OF_MONT
H=5,DAY_OF_MONTH=25,DAY_OF_YEAR=206,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1
,HOUR=2,HOUR_OF_DAY=14,MINUTE=38,SECOND=46,MILLISECOND=436,ZONE_OFFSET=19800000,
DST_OFFSET=0]
Advertisement
import java.util.*;
class A
{
public static void main(String... ar)
{
Calendar c = Calendar.getInstance();
System.out.println(c);
System.out.println("Day of the year : "+c.get(Calendar.DAY_OF_YEAR));
System.out.println("Day of the month : "+ c.get(Calendar.DAY_OF_MONTH));
System.out.println("Day of the week : " +c.get(Calendar.DAY_OF_WEEK));
//Getting Hour and minutes of the day
System.out.println("Getting Hour, minutes, AM/PM of the day");
System.out.println("Hour of the day : "+c.get(Calendar.HOUR));
System.out.println("Minute : "+c.get(Calendar.MINUTE));
System.out.println("AM or PM? : "+c.get(Calendar.AM_PM));
//Getting current date, month and year
System.out.println("Getting the current date, month and year");
System.out.println("Date : "+c.get(Calendar.DATE));
System.out.println("Month : "+c.get(Calendar.MONTH));
System.out.println("Year: "+c.get(Calendar.YEAR));
//Getting the extra information about the week
System.out.println("Getting the extra information about the week");
System.out.println("Week of the month : "+c.get(Calendar.WEEK_OF_MONTH));
System.out.println("Week of the year : "+c.get(Calendar.WEEK_OF_YEAR));
}
}
Day of the year : 206
Day of the month : 25
Day of the week : 3
Getting Hour, minutes, AM/PM of the day
Hour of the day : 2
Minute : 39
AM or PM? : 1
Getting the current date, month and year
Date : 25
Month : 6
Year: 2017
Getting the extra information about the week
Week of the month : 5
Week of the year : 30
import java.util.*;
class A
{
public static void main(String... ar)
{
Calendar c = Calendar.getInstance();
//Getting modified current date, month and year
System.out.println("Getting the current date, month and year");
System.out.println("Date : "+c.get(Calendar.DATE)); //calling get(int calendarField) method
System.out.println("Month : "+c.get(Calendar.MONTH));
System.out.println("Year: "+c.get(Calendar.YEAR));
c.set(2018, 10, 27); //calling set(int year, int month, int dayOfMonth) method
//Getting current date, month and year
System.out.println("Getting the modified current date, month and year");
System.out.println("Modified Date : "+c.get(Calendar.DATE));
System.out.println("Modified Month : "+c.get(Calendar.MONTH));
System.out.println("Modified Year: "+c.get(Calendar.YEAR));
//Another way to set the date in a Calendar object
c.set(Calendar.DATE,10); //calling set(int calendarField, int value) method
c.set(Calendar.MONTH,2);
c.set(Calendar.YEAR,5);
//Getting modified current date, month and year
System.out.println("Getting the modified current date, month and year");
System.out.println("Modified Date : "+c.get(Calendar.DATE));
System.out.println("Modified Month : "+c.get(Calendar.MONTH));
System.out.println("Modified Year: "+c.get(Calendar.YEAR));
//Getting the hour, minutes and seconds of the day
System.out.println("Getting the hour, minutes, second of the day");
System.out.println("Hour of the day : "+c.get(Calendar.HOUR));
System.out.println("Minute : "+c.get(Calendar.MINUTE));
System.out.println("Second : "+c.get(Calendar.SECOND));
//Setting the hour, minutes and seconds of the day
c.set(Calendar.HOUR,7);
c.set(Calendar.MINUTE,50);
c.set(Calendar.SECOND,23);
//Getting the modified hour, minutes and seconds of the day
System.out.println("Getting Modified hour, minutes and second of the day");
System.out.println("Modified Hour of the day : "+c.get(Calendar.HOUR));
System.out.println("Modified Minute : "+c.get(Calendar.MINUTE));
System.out.println("Modified Second : "+c.get(Calendar.SECOND));
}
}
Getting the current date, month and year
Date : 25
Month : 6
Year: 2017
Getting the modified current date, month and year
Modified Date : 27
Modified Month : 10
Modified Year: 2018
Getting the modified current date, month and year
Modified Date : 10
Modified Month : 2
Modified Year: 5
Getting the hour, minutes and second of the day
Hour of the day : 2
Minute : 43
Second : 15
Getting modified hour, minutes and second of the day
Modified Hour of the day : 7
Modified Minute : 50
Modified Second : 23
import java.util.*;
class A
{
public static void main(String... ar)
{
Calendar c = new GregorianCalendar(); //Creating an object of Calendar class by calling construtor of GregorianCalendar
//Getting current date, month and year
System.out.println("Getting the current date, month and year");
System.out.println("Date : "+c.get(Calendar.DATE));
System.out.println("Month : "+c.get(Calendar.MONTH));
System.out.println("Year: "+c.get(Calendar.YEAR));
//Another way to set the date in a Calendar object
c.add(Calendar.DATE,4); //adding 4 days to the current date.
c.add(Calendar.MONTH,-2); //subtracting 2 days from the current month.
c.add(Calendar.YEAR,-5); //subtracting 5 years from the current year.
//Getting current date, month and year
System.out.println("Getting the modified current date, month and year");
System.out.println("Modified Date : "+c.get(Calendar.DATE));
System.out.println("Modified Month : "+c.get(Calendar.MONTH));
System.out.println("Modified Year: "+c.get(Calendar.YEAR));
}
}
Getting the current date, month and year
Date : 25
Month : 6
Year: 2017
Getting the modified current date, month and year
Modified Date : 29
Modified Month : 4
Modified Year: 2012
import java.util.*;
class A
{
public static void main(String... ar)
{
Date d= new Date(1000000000000l);
System.out.println("Date object" + d);
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println("Calendar object initialized to Date object: ")
System.out.println("DATE : "+ c.get(Calendar.DATE));
System.out.println("MONTH : "+c.get(Calendar.MONTH));
System.out.println("YEAR : "+ c.get(Calendar.YEAR));
System.out.println("HOUR : "+ c.get(Calendar.HOUR));
System.out.println("MINUTE : "+ c.get(Calendar.MINUTE));
System.out.println("SECOND : "+ c.get(Calendar.SECOND));
}
}
Date object : Sun Sep 09 07:16:40 IST 2001
Calendar Object:
DATE : 9
MONTH : 8
YEAR : 2001
HOUR : 7
MINUTE : 16
SECOND : 40
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement