java的时间处理

时间:2022-03-25 00:41:52

导言:即上一篇文章继续提下java的时间处理类和函数;

1.java.util包提供了Date类来封装当前的日期和函数;Date类提供了两个构造函数来实例化Date对象;

Date()第二个是构造函数接受一个参数,该参数是从1970年1月1日起的微秒数。

Date(long millisec)

Date对象创建以后,可以调用下面方法:

序号 方法和描述
1 boolean after(Date data) 若调用此方法的Date对象在指定日期之后返回True否则返回False
2 boolean before(Date data) 若调用此方法的Date对象在指定日期之前返回True否则返回False
3 Object clone() 返回此对象的副本
4 int compareTo(Date data) 比较当调用此方法的Date对象和指定日期相等返回0;调用对象在指定日期之前返回负数;之后返回正数
5 int compareTo(Object obj)调用对象和指定对象相等;否则抛出ClassCastException
6 boolean equals(Object Date) 调用此方法的Date对象和指定日期相等时候返回True,否则返回False
7 long getTime()返回自1970-01-01 00:00:00GMT以来此Date对象表示的毫秒数
8 int hashCode() 返回此对象的hash码
9 void setTime(long time) 用自1970-01-01 00:00:00GMT以后time毫秒数设置时间和日期
10 String toString()转换Date对象为String表示形式,并返回该字符
获取当前日期和时间

import java.util.Date
public class DateDemo{
Date date = new Date();
System.out.println(date.toString);
}

2.日期比较

使用以下三种方法来比较两个日期:
使用getTime( ) 方法获取两个日期(自1970年1月1日经历的微妙数值),然后比较这两个值。
使用方法before(),after()和equals()。例如,一个月的12号比18号早,则new Date(99, 2, 12).before(new Date (99, 2, 18))返回true。
使用compareTo()方法,它是由Comparable接口定义的,Date类实现了这个接口。

3.使用SimpleDateFormat格式化日期:

SimpleDateFormat是一个以语言环境敏感的方式来格式化和分析日期的类;SimpleDateFormat允许用户选择任意形式自定义日期格式来使用。

import java.util.*;import java.text.*;
public class DateDemo {
public static void main(String args[]) {
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Current Date: " + ft.format(dNow));
}
}
运行结果如下:
Current Date: Sun 2004.07.18 at 04:14:09 PM PDT

3.简单的DateFormat格式化编码:

G     纪元标记        AD
y      四位年份        2001
M    月份 July or    07
d     一个月的日期    10
h    A.M./P.M. (1~12)格式小时     12
H    一天中的小时 (0~23)         22
m    分钟数        30
s     秒数     55
S    微妙数     234
E    星期几     Tuesday
D    一年中的日子  360
F    一个月中第几周的周几    2 (second Wed. in July)
w    一年中第几周     40
W   一个月中第几周   1
a     A.M./P.M. 标记    PM
k    一天中的小时(1~24)     24
K    A.M./P.M. (0~11)格式小时   10
z     时区 Eastern Standard Time
'     文字定界符 Delimiter
"     单引号 `
4.测量时间

import java.util.*; 
public class DiffDemo {
public static void main(String args[]) {
try {
long start = System.currentTimeMillis( );
System.out.println(new Date( ) + "
");
Thread.sleep(5*60*10);
System.out.println(new Date( ) + "
");
long end = System.currentTimeMillis( );
long diff = end - start;
System.out.println("Difference is : " + diff);
} catch (Exception e) {
System.out.println("Got an exception!");
}
}
}
运行结果如下:
Sun May 03 18:16:51 GMT 2009
Sun May 03 18:16:57 GMT 2009
Difference is : 5993

5.GregorianCalendar类:

Calendar类实现了公历日历,GregorianCalendar是Calendar类的一个具体实现。在本节中,我们没有讨论Calendar类,你可以在标准的Java文档查看。
Calendar 的getInstance()方法返回一个默认用当前的语言环境和时区初始化的GregorianCalendar对象。GregorianCalendar定义了两个字段:AD和BC。这些代表公历定义的两个时代。
下面列出GregorianCalendar对象的几个构造方法:
5.1 GregorianCalendar()
在具有默认语言环境的默认时区内使用当前时间构造一个默认的 GregorianCalendar。

5.2 GregorianCalendar(int year, int month, int date)
在具有默认语言环境的默认时区内构造一个带有给定日期设置的 GregorianCalendar

5.3 GregorianCalendar(int year, int month, int date, int hour, int minute)
为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。

5.4 GregorianCalendar(int year, int month, int date, int hour, int minute, int second)
为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。

5.5 GregorianCalendar(Locale aLocale)
在具有给定语言环境的默认时区内构造一个基于当前时间的 GregorianCalendar。

5.6 GregorianCalendar(TimeZone zone)
在具有默认语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。

5.7 GregorianCalendar(TimeZone zone, Locale aLocale)
在具有给定语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。

6.Calendar方法

序号 方法和说明
1 void add(int field, int amount)  根据日历规则指定时间量添加给定的日历中。
2 protected void computeFields() 转换UTC毫秒值为时间值域
3 protected void computeTime() 转换UTC毫秒值
4 Boolean equals(Object obj) 比较Calendar与指定的object
5 int get(int field) 获取指定字段的时间值
6 int getActualMaximum(int field) 返回当前日期,给定字段最大值
7 int getActualMinimum(int field) 返回当前日期,给定字段最小值
8 int getGreatestMinimum(int field) 返回到期
9 Date getGreatestChange() 获得格里高利历更改日期
10 int getLeastMaximum(int field) 返回日历字段最低最大值
11 int getMaximum(int field) 日历字段最大值
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, boolean up) 在给定的时间上添加或减去当个时间单元,不更改更大的字段
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 data) 设置setGregorianChange的更改日期
24 void setTime(Date data) 用给定的日期设置Calendar的时间
25 void setTimeInMillis(long millis)
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;
// Create a Gregorian calendar initialized
// with the current date and time in the
// default locale and timezone.
GregorianCalendar gcalendar = new GregorianCalendar();
// Display current time and date information.
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));
// Test if the current year is a leap year
if(gcalendar.isLeapYear(year)) {
System.out.println("The current year is a leap year");
} else {
System.out.println("The current year is not a leap year");
}
}
}

运行结果如下:

Date: Apr 22 2009Time: 11:25:27The current year is not a leap year