Java Tutorial/Data Type/Gregorian Calendar
Содержание
- 1 Calendar.DAY_OF_WEEK
- 2 Comparing Calendars
- 3 Creating a Calendar object from a locale
- 4 Creating a Date Object for a Particular Date
- 5 Creating Gregorian Calendars
- 6 Determine the day of the week
- 7 Displaying Date by weekday name
- 8 Getting Date and Time Information: get the day of the week
- 9 Integer constants for the third version of set()
- 10 Modifying Dates and Times: adding 14 to the year
- 11 Printing out weekday names
- 12 Setting a GregorianCalendar object to a particular date
- 13 Setting the Date and Time
- 14 Set with GregorianCalendar.YEAR, MONTH and DATE
- 15 Specifying the locale(TimeZone) explicitly for Gregorian Calendar
- 16 To go into the past: making the second argument negative in the "add" method
- 17 To increment or decrement a field of a calendar by 1 using the roll() method
- 18 Using a switch statement on the values for day
Calendar.DAY_OF_WEEK
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
}
}
Comparing Calendars
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar today = new GregorianCalendar();
GregorianCalendar thisDate = new GregorianCalendar();
thisDate.set(Calendar.YEAR, 2000);
if (thisDate.before(today)) {
System.out.println("before");
}
if (today.after(thisDate)) {
System.out.println("after");
}
}
}
Creating a Calendar object from a locale
import java.util.GregorianCalendar;
import java.util.Locale;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar(Locale.UK);
}
}
Creating a Date Object for a Particular Date
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] argv) throws Exception {
Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);
Date date = xmas.getTime();
}
}
Creating Gregorian Calendars
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
Date now = calendar.getTime();
System.out.println(now);
}
}
Tue Jan 16 10:06:13 PST 2007
Determine the day of the week
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] argv) throws Exception {
GregorianCalendar newCal = new GregorianCalendar();
int day = newCal.get(Calendar.DAY_OF_WEEK);
newCal = new GregorianCalendar();
newCal.set(1997, 2, 1, 0, 0, 0);
newCal.setTime(newCal.getTime());
day = newCal.get(Calendar.DAY_OF_WEEK);
}
}
Displaying Date by weekday name
import static java.util.Calendar.DATE;
import static java.util.Calendar.DAY_OF_WEEK;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
import java.text.DateFormatSymbols;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] args) {
GregorianCalendar birthdate = new GregorianCalendar(1999, 1, 1);
GregorianCalendar today = new GregorianCalendar(); // Today"s date
GregorianCalendar birthday = new GregorianCalendar(today.get(YEAR), birthdate.get(MONTH),
birthdate.get(DATE));
int age = today.get(today.YEAR) - birthdate.get(YEAR);
String[] weekdays = new DateFormatSymbols().getWeekdays(); // Get day names
System.out.println("You were born on a " + weekdays[birthdate.get(DAY_OF_WEEK)]);
System.out.println("This year you " + (birthday.after(today) ? " will be " : "are ") + age
+ " years old.");
System.out.println("In " + today.get(YEAR) + " your birthday "
+ (today.before(birthday) ? "will be" : "was") + " on a "
+ weekdays[birthday.get(DAY_OF_WEEK)] + ".");
}
}
You were born on a Monday This year you will be 8 years old. In 2007 your birthday will be on a Thursday.
Getting Date and Time Information: get the day of the week
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
int day = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(day);
}
}
Integer constants for the third version of set()
FieldValueAM_PMAM or PMDAY_OF_WEEKSUNDAY, MONDAY..., through to SATURDAYDAY_OF_YEARSet a value from 1 to 366MONTHSet a value of JANUARY, FEBRUARY, etc., through to DECEMBER, corresponding to values of 0 to 11DAY_OF_MONTH or DATESet a value from 1 to 31WEEK_OF_MONTHSet a value from 1 to 6WEEK_OF_YEARSet a value from 1 to 54HOUR_OF_DAYA value from 0 to 23HOURA value from 1 to 12 representing the current hour in the a.m. or p.m.MINUTEThe current minute in the current hour � a value from 0 to 59SECONDThe second in the current minute, 0 to 59MILLISECONDThe millisecond in the current second, 0 to 999YEARThe current year � for example, 2004ERACan be set to either GregorianCalendar.BC or GregorianCalendar.AD (both values being defined in the GregorianCalendar class)ZONE_OFFSETA millisecond value indicating the offset from GMTDST_OFFSETA millisecond value indicating the offset for daylight saving time in the current time zone
Modifying Dates and Times: adding 14 to the year
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.add(Calendar.YEAR, 14); // 14 years into the future
System.out.println(calendar.get(Calendar.YEAR));
}
}
2021
Printing out weekday names
import java.text.DateFormatSymbols;
public class MainClass {
public static void main(String[] args) {
String[] weekdays = new DateFormatSymbols().getWeekdays(); // Get day names
for(String s: weekdays){
System.out.println(s);
}
}
}
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Setting a GregorianCalendar object to a particular date
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(1995, 10, 29); // Date set to 29th November 1999
}
}
Setting the Date and Time
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Date date = new Date();
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
}
}
Set with GregorianCalendar.YEAR, MONTH and DATE
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] argv) throws Exception {
GregorianCalendar gc = new GregorianCalendar();
gc.setLenient(false);
gc.set(GregorianCalendar.YEAR, 2003);
gc.set(GregorianCalendar.MONTH, 12);
gc.set(GregorianCalendar.DATE, 1);
gc.getTime();
}
}
Specifying the locale(TimeZone) explicitly for Gregorian Calendar
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/Chicago"),
Locale.US);
System.out.println(calendar);
}
}
To go into the past: making the second argument negative in the "add" method
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.add(Calendar.YEAR, -14);
System.out.println(calendar.get(Calendar.YEAR));
}
}
1993
To increment or decrement a field of a calendar by 1 using the roll() method
By +1 or -1, depending on the second argument(true or false).
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.roll(Calendar.MONTH, false); // Go back a month
System.out.println(calendar.get(Calendar.MONTH));
}
}
11
Using a switch statement on the values for day
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
int day = calendar.get(Calendar.DAY_OF_WEEK);
switch (day) {
case Calendar.MONDAY:
System.out.println(Calendar.MONDAY);
break;
case Calendar.TUESDAY:
System.out.println(Calendar.TUESDAY);
break;
default:
System.out.println("others");
}
}
}