Java by API/java.util/Calendar
Содержание
- 1 Calendar: add(int field, int amount)
- 2 Calendar: after(Object when)
- 3 Calendar.AM_PM
- 4 Calendar: before(Object when)
- 5 Calendar.DATE
- 6 Calendar: DAY_OF_WEEK
- 7 Calendar.DAY_OF_WEEK_IN_MONTH
- 8 Calendar.DAY_OF_YEAR
- 9 Calendar.DST_OFFSET
- 10 Calendar.ERA
- 11 Calendar: getActualMaximum(int field)
- 12 Calendar: getDisplayNames(int field, int style, Locale locale)
- 13 Calendar: getFirstDayOfWeek()
- 14 Calendar: getInstance()
- 15 Calendar: getTime()
- 16 Calendar.HOUR
- 17 Calendar.HOUR_OF_DAY
- 18 Calendar: int get(int field)
- 19 Calendar.MILLISECOND
- 20 Calendar.MINUTE
- 21 Calendar.MONDAY
- 22 Calendar.SECOND
- 23 Calendar: set(int field, int value)
- 24 Calendar: set(int year, int month, int date)
- 25 Calendar: setTime(Date date)
- 26 Calendar: setTimeInMillis(long millis)
- 27 Calendar.SUNDAY
- 28 Calendar: TUESDAY
- 29 Calendar.WEEK_OF_MONTH
- 30 Calendar.WEEK_OF_YEAR
- 31 Calendar.YEAR
- 32 Calendar.ZONE_OFFSET
Calendar: add(int field, int amount)
/*
It is now Thu 2006/05/11 at 02:11:22 PM PDT
Two years ago was Tue 2004/05/11 at 02:11:22 PM PDT
* */
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainClass {
public static void main(String[] a) throws Exception {
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy/MM/dd "at" hh:mm:ss a zzz");
System.out.println("It is now " + formatter.format(now.getTime()));
now.add(Calendar.YEAR, -2);
System.out.println("Two years ago was " + formatter.format(now.getTime()));
}
}
Calendar: after(Object when)
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
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");
}
}
}
Calendar.AM_PM
/*
ERA: 1
YEAR: 2006
MONTH: 4
WEEK_OF_YEAR: 19
WEEK_OF_MONTH: 2
DATE: 11
DAY_OF_MONTH: 11
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 2
AM_PM: 1
MILLISECOND: 16
ZONE_OFFSET: -8
DST_OFFSET: 1
* */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
}
}
Calendar: before(Object when)
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
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");
}
}
}
Calendar.DATE
/*
* Output:
Date: Apr 25 2006
Time: 4:36:28
Updated time: 10:29:22
*/
import java.util.Calendar;
public class MainClass {
public static void main(String args[]) {
String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
Calendar calendar = Calendar.getInstance();
System.out.print("Date: ");
System.out.print(months[calendar.get(Calendar.MONTH)]);
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.println(calendar.get(Calendar.YEAR));
System.out.print("Time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 29);
calendar.set(Calendar.SECOND, 22);
System.out.print("Updated time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
}
}
Calendar: DAY_OF_WEEK
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
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");
}
}
}
Calendar.DAY_OF_WEEK_IN_MONTH
/*
ERA: 1
YEAR: 2006
MONTH: 4
WEEK_OF_YEAR: 19
WEEK_OF_MONTH: 2
DATE: 11
DAY_OF_MONTH: 11
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 2
AM_PM: 1
MILLISECOND: 16
ZONE_OFFSET: -8
DST_OFFSET: 1
* */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
}
}
Calendar.DAY_OF_YEAR
/*
* Output:
250 days remain in current year
*/
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String args[]) {
Calendar calendar1 = Calendar.getInstance();
int doy1 = calendar1.get(Calendar.DAY_OF_YEAR);
int year = calendar1.get(Calendar.YEAR);
Calendar calendar2 = new GregorianCalendar(year, 11, 31);
int doy2 = calendar2.get(Calendar.DAY_OF_YEAR);
int days = doy2 - doy1;
System.out.println(days + " days remain in current year");
}
}
Calendar.DST_OFFSET
/*
ERA: 1
YEAR: 2006
MONTH: 4
WEEK_OF_YEAR: 19
WEEK_OF_MONTH: 2
DATE: 11
DAY_OF_MONTH: 11
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 2
AM_PM: 1
MILLISECOND: 16
ZONE_OFFSET: -8
DST_OFFSET: 1
* */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
}
}
Calendar.ERA
/*
ERA: 1
YEAR: 2006
MONTH: 4
WEEK_OF_YEAR: 19
WEEK_OF_MONTH: 2
DATE: 11
DAY_OF_MONTH: 11
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 2
AM_PM: 1
MILLISECOND: 16
ZONE_OFFSET: -8
DST_OFFSET: 1
* */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
}
}
Calendar: getActualMaximum(int field)
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
}
}
Calendar: getDisplayNames(int field, int style, Locale locale)
import java.util.Calendar;
import java.util.Locale;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class Main {
public static void main(String args[]) {
Calendar now = Calendar.getInstance();
Locale locale = Locale.getDefault();
Map<String, Integer> names = now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
NavigableMap<String, Integer> nav = new TreeMap<String, Integer>(names);
System.out.printf("Whole list:%n%s%n", nav);
}
}
Calendar: getFirstDayOfWeek()
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Locale;
public class MainClass {
public static void main(String[] argv) {
Calendar JO_cal = Calendar.getInstance(new Locale("ar", "JO"));
Calendar FR_cal = Calendar.getInstance(Locale.FRANCE);
Calendar CA_cal = Calendar.getInstance(Locale.CANADA);
DateFormatSymbols dfs = new DateFormatSymbols();
String weekdays[] = dfs.getWeekdays();
System.out.println(weekdays[JO_cal.getFirstDayOfWeek()]);
System.out.println(weekdays[FR_cal.getFirstDayOfWeek()]);
System.out.println(weekdays[CA_cal.getFirstDayOfWeek()]);
}
}
Calendar: getInstance()
/*
* Output:
2006
1
13
21
*/
import java.util.Calendar;
public class MainClass {
public static void main(String args[]) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(calendar.get(Calendar.MINUTE));
}
}
Calendar: getTime()
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);
}
}
Calendar.HOUR
/*
* Output:
2006
1
13
21
*/
import java.util.Calendar;
public class MainClass {
public static void main(String args[]) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(calendar.get(Calendar.MINUTE));
}
}
Calendar.HOUR_OF_DAY
/*
* Output:
2006
1
13
21
*/
import java.util.Calendar;
public class MainClass {
public static void main(String args[]) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(calendar.get(Calendar.MINUTE));
}
}
Calendar: int get(int field)
import java.util.Calendar;
public class Main{
public static void main(String[] args){
Calendar calendar = Calendar.getInstance ();
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.DATE));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.MINUTE));
System.out.println(calendar.get(Calendar.SECOND));
System.out.println(calendar.get(Calendar.MILLISECOND));
}
}
Calendar.MILLISECOND
/*
ERA: 1
YEAR: 2006
MONTH: 4
WEEK_OF_YEAR: 19
WEEK_OF_MONTH: 2
DATE: 11
DAY_OF_MONTH: 11
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 2
AM_PM: 1
MILLISECOND: 16
ZONE_OFFSET: -8
DST_OFFSET: 1
* */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
}
}
Calendar.MINUTE
/*
* Output:
2006
1
13
21
*/
import java.util.Calendar;
public class MainClass {
public static void main(String args[]) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(calendar.get(Calendar.MINUTE));
}
}
Calendar.MONDAY
/**
*Output:
THURSDAY
*/
import java.util.Calendar;
import java.util.Date;
public class MainClass {
public static void main(String args[]) {
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == Calendar.SUNDAY)
System.out.println("SUNDAY");
if (dayofweek == Calendar.MONDAY)
System.out.println("MONDAY");
if (dayofweek == Calendar.TUESDAY)
System.out.println("TUESDAY");
if (dayofweek == Calendar.WEDNESDAY)
System.out.println("WEDNESDAY");
if (dayofweek == Calendar.THURSDAY)
System.out.println("THURSDAY");
if (dayofweek == Calendar.FRIDAY)
System.out.println("FRIDAY");
if (dayofweek == Calendar.SATURDAY) {
System.out.println("SATURDAY");
}
}
}
Calendar.SECOND
/*
* Output:
Date: Apr 25 2006
Time: 4:36:28
Updated time: 10:29:22
*/
import java.util.Calendar;
public class MainClass {
public static void main(String args[]) {
String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
Calendar calendar = Calendar.getInstance();
System.out.print("Date: ");
System.out.print(months[calendar.get(Calendar.MONTH)]);
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.println(calendar.get(Calendar.YEAR));
System.out.print("Time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 29);
calendar.set(Calendar.SECOND, 22);
System.out.print("Updated time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
}
}
Calendar: set(int field, int value)
/*
* Output:
Date: Apr 25 2006
Time: 4:36:28
Updated time: 10:29:22
*/
import java.util.Calendar;
public class MainClass {
public static void main(String args[]) {
String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
Calendar calendar = Calendar.getInstance();
System.out.print("Date: ");
System.out.print(months[calendar.get(Calendar.MONTH)]);
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.println(calendar.get(Calendar.YEAR));
System.out.print("Time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 29);
calendar.set(Calendar.SECOND, 22);
System.out.print("Updated time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
}
}
Calendar: set(int year, int month, int date)
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(2001, 1, 1);
}
}
Calendar: setTime(Date date)
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
}
}
Calendar: setTimeInMillis(long millis)
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
System.out.println(now + " = " + formatter.format(calendar.getTime()));
}
}
Calendar.SUNDAY
/**
*Output:
THURSDAY
*/
import java.util.Calendar;
import java.util.Date;
public class MainClass {
public static void main(String args[]) {
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == Calendar.SUNDAY)
System.out.println("SUNDAY");
if (dayofweek == Calendar.MONDAY)
System.out.println("MONDAY");
if (dayofweek == Calendar.TUESDAY)
System.out.println("TUESDAY");
if (dayofweek == Calendar.WEDNESDAY)
System.out.println("WEDNESDAY");
if (dayofweek == Calendar.THURSDAY)
System.out.println("THURSDAY");
if (dayofweek == Calendar.FRIDAY)
System.out.println("FRIDAY");
if (dayofweek == Calendar.SATURDAY) {
System.out.println("SATURDAY");
}
}
}
Calendar: TUESDAY
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
}
}
Calendar.WEEK_OF_MONTH
/*
ERA: 1
YEAR: 2006
MONTH: 4
WEEK_OF_YEAR: 19
WEEK_OF_MONTH: 2
DATE: 11
DAY_OF_MONTH: 11
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 2
AM_PM: 1
MILLISECOND: 16
ZONE_OFFSET: -8
DST_OFFSET: 1
* */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
}
}
Calendar.WEEK_OF_YEAR
/*
ERA: 1
YEAR: 2006
MONTH: 4
WEEK_OF_YEAR: 19
WEEK_OF_MONTH: 2
DATE: 11
DAY_OF_MONTH: 11
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 2
AM_PM: 1
MILLISECOND: 16
ZONE_OFFSET: -8
DST_OFFSET: 1
* */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
}
}
Calendar.YEAR
/*
* Output:
2006
1
13
21
*/
import java.util.Calendar;
public class MainClass {
public static void main(String args[]) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(calendar.get(Calendar.MINUTE));
}
}
Calendar.ZONE_OFFSET
/*
ERA: 1
YEAR: 2006
MONTH: 4
WEEK_OF_YEAR: 19
WEEK_OF_MONTH: 2
DATE: 11
DAY_OF_MONTH: 11
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 2
AM_PM: 1
MILLISECOND: 16
ZONE_OFFSET: -8
DST_OFFSET: 1
* */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] a) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
}
}