日期操作类--GregorianCalendar类
JavaTM Platform Standard Ed. 6
GregorianCalendar类
Calendar类实现了公历日历,GregorianCalendar是Calendar类的一个具体实现。
Calendar 的getInstance()方法返回一个默认用当前的语言环境和时区初始化的GregorianCalendar对象。GregorianCalendar定义了两个字段:AD和BC。这些代表公历定义的两个时代。
下面列出GregorianCalendar对象的几个构造方法:
| 序号 | 构造函数和说明 |
| 1 | GregorianCalendar() 在具有默认语言环境的默认时区内使用当前时间构造一个默认的 GregorianCalendar。 |
| 2 | GregorianCalendar(int year, int month, int date) 在具有默认语言环境的默认时区内构造一个带有给定日期设置的 GregorianCalendar |
| 3 | GregorianCalendar(int year, int month, int date, int hour, int minute) 为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。 |
| 4 | GregorianCalendar(int year, int month, int date, int hour, int minute, int second) 为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。 |
| 5 | GregorianCalendar(Locale aLocale) 在具有给定语言环境的默认时区内构造一个基于当前时间的 GregorianCalendar。 |
| 6 | GregorianCalendar(TimeZone zone) 在具有默认语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。 |
| 7 | GregorianCalendar(TimeZone zone, Locale aLocale) 在具有给定语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。 |
这里是GregorianCalendar 类提供的一些有用的方法列表:
| 序号 | 方法和说明 |
| 1 | void add(int field, int amount) 根据日历规则,将指定的(有符号的)时间量添加到给定的日历字段中。 |
| 2 | protected void computeFields() 转换UTC毫秒值为时间域值 |
| 3 | protected void computeTime() 覆盖Calendar ,转换时间域值为UTC毫秒值 |
| 4 | boolean equals(Object obj) 比较此 GregorianCalendar 与指定的 Object。 |
| 5 | int get(int field) 获取指定字段的时间值 |
| 6 | int getActualMaximum(int field) 返回当前日期,给定字段的最大值 |
| 7 | int getActualMinimum(int field) 返回当前日期,给定字段的最小值 |
| 8 | int getGreatestMinimum(int field) 返回此 GregorianCalendar 实例给定日历字段的最高的最小值。 |
| 9 | Date getGregorianChange() 获得格里高利历的更改日期。 |
| 10 | int getLeastMaximum(int field) 返回此 GregorianCalendar 实例给定日历字段的最低的最大值 |
| 11 | int getMaximum(int field) 返回此 GregorianCalendar 实例的给定日历字段的最大值。 |
| 12 | Date getTime() 获取日历当前时间。 |
| 13 | long getTimeInMillis() 获取用长整型表示的日历的当前时间 |
| 14 | TimeZone getTimeZone() 获取时区。 |
| 15 | int getMinimum(int field) 返回给定字段的最小值。 |
| 16 | int hashCode() 重写hashCode. |
| 17 | boolean isLeapYear(int year) 确定给定的年份是否为闰年。 |
| 18 | void roll(int field, boolean up) 在给定的时间字段上添加或减去(上/下)单个时间单元,不更改更大的字段。 |
| 19 | void set(int field, int value) 用给定的值设置时间字段。 |
| 20 | void set(int year, int month, int date) 设置年、月、日的值。 |
| 21 | void set(int year, int month, int date, int hour, int minute) 设置年、月、日、小时、分钟的值。 |
| 22 | void set(int year, int month, int date, int hour, int minute, int second) 设置年、月、日、小时、分钟、秒的值。 |
| 23 | void setGregorianChange(Date date) 设置 GregorianCalendar 的更改日期。 |
| 24 | void setTime(Date date) 用给定的日期设置Calendar的当前时间。 |
| 25 | void setTimeInMillis(long millis) 用给定的long型毫秒数设置Calendar的当前时间。 |
| 26 | void setTimeZone(TimeZone value) 用给定时区值设置当前时区。 |
| 27 | String toString() 返回代表日历的字符串。 |
实例
import java.util.*;
public class GregorianCalendarDemo {
public static void main(String args[]) {
String months[] = {
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
int year;
// 初始化 Gregorian 日历
// 使用当前时间和日期
// 默认为本地时间和时区
GregorianCalendar gcalendar = new GregorianCalendar();
// 显示当前时间和日期的信息
System.out.print("Date: ");
System.out.print(months[gcalendar.get(Calendar.MONTH)]);
System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
System.out.println(year = gcalendar.get(Calendar.YEAR));
System.out.print("Time: ");
System.out.print(gcalendar.get(Calendar.HOUR) + ":");
System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
System.out.println(gcalendar.get(Calendar.SECOND));
// 测试当前年份是否为闰年
if(gcalendar.isLeapYear(year)) {
System.out.println("当前年份是闰年");
}
else {
System.out.println("当前年份不是闰年");
}
}
}
以上实例编译运行结果如下:
Date: Apr 22 2009
Time: 11:25:27
当前年份不是闰年
日期操作类--GregorianCalendar类的更多相关文章
- 日期操作类--Date类
Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...
- JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/对象的克隆/二叉树
/** * Math类.Random类.Arrays类:具体查JAVA手册...... */ public class Main { public static void main(String[] ...
- 使用日期操作类(Calendar)获得几秒、几分钟、几小时之前的时间
public String dealDate(String case_time){ // 日期操作类 Calendar calendar = Calendar.getInstance(); // 当前 ...
- Java面向对象_常用类库api——日期操作类
Data类 类Data表示特定的瞬间,精确到毫秒,也就是程序运行时的当前时间 Data data=new Data();//实例化Data对象,表示当前时间 Calendar类 日历类,使用此类可以将 ...
- 菜鸡的Java笔记 日期操作类
日期操作类 Date 类与 long 数据类型的转换 SimpleDateFormat 类的使用 Calendar 类的使用 如 ...
- 日期操作类--DateFormat类
简单的DateFormat格式化编码 时间模式字符串用来指定时间格式.在此模式中,所有的ASCII字母被保留为模式字母,定义如下: 字母 描述 示例 G 纪元标记 AD y 四位年份 2001 M 月 ...
- Java8 ,LocalDate,LocalDateTime处理日期和时间工具类,
Java8 ,LocalDate,LocalDateTime处理日期和时间工具类 1.获取今天的日期 2.在Java 8 中获取年.月.日信息 3.在Java 8 中处理特定日期 4.在Java 8 ...
- GregorianCalendar类
Calendar类实现了公历日历,GregorianCalendar是Calendar类的一个具体实现. Calendar 的getInstance()方法返回一个默认用当前的语言环境和时区初始化的G ...
- 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结
1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018 年月日时分秒 CST代表北 ...
随机推荐
- 【Linux】用less查看日志文件
一般程序部署在Linux环境,查看日志时,一般用less满足大部分的需求. 列举.记录最常用的场景,代码以Tomcat日志文件catalna.out为例. > 直接查看文件 less catal ...
- 未能加载文件或程序集“CefSharp, Version=1.25.XXXX”或它的某一个依赖项。试图加载格式不正确的程序。
在使用CefSharp的过程中遇到一个坑爹的错误. 从GitHub的项目主页:https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-qu ...
- LVM逻辑分区
视频:http://v.youku.com/v_show/id_XNTk2NzExMTg0.html?f=27874439&o=1&spm=0.0.playList.5!3~5~A.t ...
- ctrl+z暂停任务
(1) CTRL+Z挂起进程并放入后台 (2) jobs 显示当前暂停的进程 (3) bg %N 使第N个任务在后台运行(%前有空格) (4) fg %N 使第N个任务在前台运行 默认bg,fg不带% ...
- SQL Server 中 RAISERROR 的用法
From : http://www.cnblogs.com/xugang/archive/2011/04/09/2010216.html raiserror 是由单词 raise error 组 ...
- scala打印九九乘法表的5种实现
使用scala打印九九乘法表,可以有多种实现方法,实现的过程充分的体现的scala语言的优势和巨大的简洁性和高效性, 下面我用了5种方法实现九九乘法表. 使用类似于java,c++等指令风格的的编程实 ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- MyEclipse Servers视窗出现“Could not create the view: An unexpected exception was thrown”错误解决办法
打开所在的wordspace文件夹,在下面子文件夹 .metadata\.plugins\org.eclipse.core.runtime\.settings\com.genuitec.eclipse ...
- [转载] 单表60亿记录等大数据场景的MySQL优化和运维之道 | 高可用架构
原文: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=209406532&idx=1&sn=2e9b0cc02bdd ...
- [转载] 构建微服务:使用API Gateway
原文: http://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=206889381&idx=1&sn=478ccb35294c ...