Calendar Class in Java With Examples
Calendar Class in Java With Examples
Calendar Class in Java With Examples
import java.util.*;
public class Calendar1 {
public static void main(String args[])
{
Calendar c = Calendar.getInstance();
System.out.println("The Current Date is:" + c.getTime());
}
}
METHOD DESCRIPTION
abstract int It is used to return the minimum value for the given
getMinimum(int field) calendar field of this Calendar instance.
import java.util.*;
public class Calendar2 {
public static void main(String[] args)
{
// creating Calendar object
Calendar calendar = Calendar.getInstance();
// Demonstrate Calendar's get()method
System.out.println("Current Calendar's Year: " +
calendar.get(Calendar.YEAR));
System.out.println("Current Calendar's Day: " +
calendar.get(Calendar.DATE));
System.out.println("Current MINUTE: " +
calendar.get(Calendar.MINUTE));
System.out.println("Current SECOND: " +
calendar.get(Calendar.SECOND));
}
}
import java.util.*;
public class Calendar3 {
public static void main(String[] args)
{
// creating calendar object
Calendar calendar = Calendar.getInstance();
int max = calendar.getMaximum(Calendar.DAY_OF_WEEK);
System.out.println("Maximum number of days in a week: " + max);
max = calendar.getMaximum(Calendar.WEEK_OF_YEAR);
System.out.println("Maximum number of weeks in a year: " + max);
}
}
public class Calendar5 {
public static void main(String[] args)
{
// creating calendar object
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -15);
System.out.println("15 days ago: " + calendar.getTime());
calendar.add(Calendar.MONTH, 4);
System.out.println("4 months later: " + calendar.getTime());
calendar.add(Calendar.YEAR, 2);
System.out.println("2 years later: " + calendar.getTime());
}
}
Java SimpleDateFormat
The java.text.SimpleDateFormat class is used to both parse and
format dates according to a formatting pattern you specify yourself. When
parsing dates, the Java SimpleDateFormat typically parses the date
from a Java String. When formatting dates,
the SimpleDateFormat typically formats a Date object into a String,
although it can also format the date into a StringBuffer.
Creating a SimpleDateFormat
The pattern parameter passed to
the SimpleDateFormat constructor is the pattern to use for parsing
and formatting of dates. The pattern syntax is covered later in this text. The
pattern is just a regular Java String.
Formatting Dates
In java, converting date into string is called formatting and vice-versa parsing. In
other words, formatting means date to string and parsing means string to date.
import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println("Current Date: "+currentDate); Wed Jul 14 11:52:….
String dateToStr = DateFormat.getInstance().format(currentDate);
System.out.println("Date Format using getInstance(): "+dateToStr);
}
}
import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample3 {
public static void main(String[] args)throws Exception {
Date d = DateFormat.getDateInstance().parse("31 July, 2021");
System.out.println("Date is: "+d);
}
}