日期操作类--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代表北 ...
随机推荐
- 年轻的团队Mono玩转Dalvik
真的很羡慕这样的一个团队,年轻充满斗志. 甲骨文和谷歌正就谷歌在Android中使用Java一案展开一场10亿美元的大较量.但是Java并非在Android中建立本地应用的唯一方法.事实上,它甚至不是 ...
- 双4G LTE
我们即将上市的Xplay3S将支持双4G LTE(TDD-LTE+FDD-LTE),那什么是4G LTE呢,今天来给大家科普下4G LTE网络知识. 什么是TDD-LTE/FDD-LTE? LTE是通 ...
- WinCE开机Logo的实现(USB下载图片到nandflash)
WinCE开机启动Logo使用Eboot读取NandFlash中的图片数据,然后显示的方式.对于开机logo的方式网友http://jazka.blog.51cto.com/809003/664131 ...
- .Net连接数据库(AOD.Net)
用C#实现用户名登录---连接数据库,查询并判断是否存在用户名 一.链接类 1.添加DBConnection类 2构造 public class DBConnection { private stat ...
- 【leetcode❤python】83. Remove Duplicates from Sorted List
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- WWDC2016 Session笔记 - Xcode 8 Auto Layout新特性
目录 1.Incrementally Adopting Auto Layout 2.Design and Runtime Constraints 3.NSGridView 4.Layout Feedb ...
- Python使用requirements.txt安装类库
摘要:我们为何要应用requirements.txt呢? 首要应用目标: 任何 运用顺序 平常 须要设置装置 所需并 依附 一组类库去知足 事情请求 . 请求 文件 是 指定 战 一次性 装置 包 的 ...
- oracle PL/SQL基础编程
PL/SQL(Procedural Language/SQL)是oracle中引入的一种过程化编程语言 PLS-00103:出现符号"declare"在需要下列之一时 符号&quo ...
- HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)
HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...
- LTE Module User Documentation(翻译5)——Mobility Model with Buildings
LTE用户文档 (如有不当的地方,欢迎指正!) 8 Mobility Model with Buildings 我们现在通过例子解释如何在 ns-3 仿真程序中使用 buildings 模型(特别 ...