java.util.Calendar
1 java.util.Calendar介绍
Java Calendar类是一个抽象类,它提供用于在特定时间点和一组日历字段(如MONTH,YEAR,HOUR等)之间转换日期的方法。它继承Object类并实现Comparable接口。
2 java.util.Calendar声明
让我们看看java.util.Calendar类的声明。
public abstract class Calendar extends Object
implements Serializable, Cloneable, Comparable<Calendar>
3 java.util.Calendar方法
4 java.util.Calendar案例1
/**
* www.yiidian.com 一点教程网
*/
import java.util.Calendar;
public class CalendarExample1 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("The current date is : " + calendar.getTime());
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());
}
}
输出结果为:
The current date is : Thu Jan 19 18:47:02 IST 2017
15 days ago: Wed Jan 04 18:47:02 IST 2017
4 months later: Thu May 04 18:47:02 IST 2017
2 years later: Sat May 04 18:47:02 IST 2019
5 java.util.Calendar案例2
/**
* www.yiidian.com 一点教程网
*/
import java.util.*;
public class CalendarExample2{
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("At present Calendar's Year: " + calendar.get(Calendar.YEAR));
System.out.println("At present Calendar's Day: " + calendar.get(Calendar.DATE));
}
}
输出结果为:
At present Calendar's Year: 2017
At present Calendar's Day: 20
6 java.util.Calendar案例3
/**
* www.yiidian.com 一点教程网
*/
import java.util.*;
public class CalendarExample3{
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.print("At present Date And Time Is: " + calendar.getTime());
}
}
输出结果为:
At present Date And Time Is: Fri Jan 20 14:26:19 IST 2017
7 java.util.Calendar案例4
/**
* www.yiidian.com 一点教程网
*/
import java.util.*;
public class CalendarExample4 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int maximum = calendar.getMaximum(Calendar.DAY_OF_WEEK);
System.out.println("Maximum number of days in week: " + maximum);
maximum = calendar.getMaximum(Calendar.WEEK_OF_YEAR);
System.out.println("Maximum number of weeks in year: " + maximum);
}
}
输出结果为:
Maximum number of days in week: 7
Maximum number of weeks in year: 53
8 java.util.Calendar案例5
/**
* www.yiidian.com 一点教程网
*/
import java.util.*;
public class CalendarExample5 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
int maximum = cal.getMinimum(Calendar.DAY_OF_WEEK);
System.out.println("Minimum number of days in week: " + maximum);
maximum = cal.getMinimum(Calendar.WEEK_OF_YEAR);
System.out.println("Minimum number of weeks in year: " + maximum);
}
}
输出结果为:
Minimum number of days in week: 1
Minimum number of weeks in year: 1
热门文章
优秀文章