Java Tutorial/Data Type/Calendar
Содержание
- 1 Change multiple components at the same time
- 2 Check for a Leap Year
- 3 Checking for a Leap Year: using GregorianCalendar
- 4 Construct a Calendar object by using the setTime method
- 5 Create a Date object using the Calendar class
- 6 Create instance of java.sql.Date from Calendar.getTimeInMillis()
- 7 Display Day of Week using Java Calendar
- 8 Display full date time
- 9 Display Month of year using Java Calendar
- 10 Get current date, year and month
- 11 Get current time information
- 12 Get day of week
- 13 Get Days Of The Week for different locale
- 14 Get the last date of a month
- 15 Get the last day of a month
- 16 Get the number of days in that month
- 17 Get time in milliseconds using Java Calendar
- 18 Get Week of month and year using Java Calendar
- 19 The java.util.Calendar Class
- 20 To change a date/time component, use its set method: public void set (int field, int value)
- 21 To obtain a date part, such as the hour, the month, or the year, use the get method
- 22 Try month in a leap year
- 23 Using the Calendar Class to Display Current Time in Different Time Zones
Change multiple components at the same time
public void set (int year, int month, int date)
public void set (int year, int month, int date, int hour, int minute, int second)
Check for a Leap Year
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;
}
}
}
Checking for a Leap Year: using GregorianCalendar
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);
}
}
Construct a Calendar object by using the setTime method
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());
}
}
Create a Date object using the Calendar class
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
Create instance of java.sql.Date from Calendar.getTimeInMillis()
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
Display Day of Week using Java Calendar
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]);
}
}
Display full date time
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));
}
}
Display Month of year using Java Calendar
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)]);
}
}
Get current date, year and month
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));
}
}
Get current time information
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));
}
}
Get day of week
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);
}
}
Get Days Of The Week for different locale
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]);
}
}
Get the last date of a month
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);
}
}
Get the last day of a month
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);
}
}
Get the number of days in that month
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
}
}
Get time in milliseconds using Java Calendar
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());
}
}
Get Week of month and year using Java Calendar
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));
}
}
The java.util.Calendar Class
To create a java.util.Calendar object, you have to use its static getInstance method.
public static Calendar getInstance ()
public static Calendar getInstance (Locale locale)
To change a date/time component, use its set method: public void set (int field, int value)
import java.util.Calendar;
public class MainClass {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.DECEMBER);
}
}
To obtain a date part, such as the hour, the month, or the year, use the get method
public int get (int field)
2007 0 30 8 32 42 805
Try month in a leap year
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
}
}
Using the Calendar Class to Display Current Time in Different Time Zones
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