首页 > Java > java教程 > java.util.Date

java.util.Date

王林
发布: 2024-08-30 15:49:14
原创
970 人浏览过

java中的日期和时间由一个名为java.util.Date类的类来表示,该类提供了处理java中的时间和日期的方法和构造函数以及Serialized、Comparable等。和 Cloneable 接口由 java.util.Date 类实现,java.sql.Date、java.sql.Time 和 java.sql.Timestamp 接口是 java.util.Date 类的基类。

java.util.Date 的构造函数

java.util.Date 类的构造函数是:

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

1。 Date(): 使用 Date() 构造函数创建一个日期对象,该对象表示当前时间和日期。

2。 Date(long milliseconds): 使用 Date(long milliseconds) 构造函数创建自 1st 1970 年 1 月 00:00:00 GMT 起指定毫秒数的日期对象.

java.util.Date 的方法

java.util.Date 类的方法有:

1。 booleanafter(Date date): Boolean after(Date date) 方法用于检查当前日期是否晚于指定日期作为参数。

2。 booleanbefore(Date date): Boolean before(Date date) 方法用于检查当前日期是否早于指定日期作为 qparameter。

以下是 java.util.Date 的示例:

代码:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
// a variable called dat1 is defined to store one date
Date dat1=new Date(2020,6,01);
//a variable called dat2 is defined to store another date
Date dat2=new Date(2021,6,01);
//boolean after(Date date) method is used to check if dat1 comes after dat2 date
System.out.println("The date represented by the variable dat1 is after the date represented by the variable dat2 : "+dat1.after(dat2));
//boolean before(Date date) method is used to check if dat1 comes before dat2 date
System.out.println("The date represented by the variable dat1 is before the date represented by the variable dat2 : "+dat1.before(dat2));
}
}
登录后复制

输出:

java.util.Date

说明:在上面的程序中,定义了一个名为 dat1 的变量来存储一个日期。然后定义一个名为 dat2 的变量来存储另一个日期。然后使用 boolean after(Date date) 方法检查 dat1 是否在 dat2 日期之后,返回的输出是 true 或 false。然后使用 boolean before(Date date) 方法检查 dat1 是否早于 dat2 日期,返回的输出是 true 或 false。

3。对象clone():对象clone()方法用于返回当前日期的克隆对象。

下面是 java.util.Date 的示例:

代码:

import java.util.Date;
public class Main
{
public static void main(String[] args)
{
//an instance of the date class is created
Date dat=new Date(2020,6,01);
//clone method is called on the instance of the date class to create a clone of the given date
System.out.println("The clone of the given date is : "+dat.clone());
}
}
登录后复制

输出:

java.util.Date

说明:在上面的程序中,创建了日期类的实例。然后在日期类实例上调用克隆方法来创建给定日期的克隆。

4。 intcompareTo(Date date): intcompareTo(Date date) 方法用于将当前日期与指定为参数的日期进行比较。如果当前日期与指定为参数的日期相同,则输出返回零。如果当前日期早于指定为参数的日期,则返回的输出小于零。如果当前日期晚于参数指定的日期,则返回的输出大于零。

5。 booleanquals(Date date): boolean equals(Date date) 方法用于比较当前日期与参数指定的日期是否相等。如果当前日期与参数指定的日期相同,则输出返回 true,否则输出返回 false。

下面是 java.util.Date 的示例:

代码:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created to store one date
Date dat1=new Date(2020,6,01);
//an instance of the date class is created to store another date
Date dat2=new Date(2021,6,01);
//compareTo method is used to compare the two dates stored using the two variables defined before
int comp=dat1.compareTo(dat2);
//the result is displayed after comparing the two dates
System.out.println("The result after comparing the two values is : "+comp);
//Equals() method of date class is used to check if the two dates specified are equal
System.out.println("The dates stored in the two variables are equal : "+dat1.equals(dat2));
}
}
登录后复制

输出:

java.util.Date

说明:在上面的程序中,创建了一个日期类的实例来存储一个日期。然后创建日期类的实例来存储另一个日期。然后使用compareTo方法来比较使用之前定义的两个变量存储的两个日期。然后比较两个日期后显示结果。然后使用 equals 方法检查使用之前定义的两个变量存储的两个日期是否相等。

6。 static Date from(Instant instant): static Date from(Instant instant) 方法用于从即时日期返回日期对象的实例。

7。 long getTime(): long getTime() 方法用于返回日期对象表示的时间。

8。 inthashCode():int hashCode()方法用于返回日期对象表示的哈希码值。

9。 Instant toInstant():Instant toInstant()方法用于将当前日期转换为即时对象。

10。 String toString():字符串 toString() 方法用于将给定日期转换为即时对象。

下面是 java.util.Date 的示例:

代码:

import java.time.Instant;
import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created which stores the present date
Date dat=new Date();
//instant date and time is obtained using instant.now() method
Instant inst= Instant.now();
//the instant date and time is displayed by using from() method
System.out.println("The instant date is  : "+dat.from(inst));
//getTime() method is used to obtain the number of milliseconds since January 1st 1970
System.out.println("The number of milliseconds since January 1, 1970, 00:00:00 GTM : "+dat.getTime());
//hashcode() method is used to obtain the hash code of the given date
System.out.println("The hash code for the given date is : "+dat.hashCode());
//toInstant() method is called to convert the present date to the instant object
System.out.println("The instant object after converting the present date : "+dat.toInstant());
//toString() method is used to convert the date to string
System.out.println("The date after conversion to string is : "+dat.toString());
}
}
登录后复制

输出:

java.util.Date

Explanations: In the above program, an instance of the date class is created to store the present date. Then instant date and time are obtained using instant.now() method. Then the instant date and time are displayed by using from the () method. Then getTime() method is used to obtain the number of milliseconds since January 1st, 1970. Then hashcode() method is used to obtain the hash code of the given date. Then toInstant() method is called to convert the present date to the instant object. Then toString() method is used to convert the date to string.

11. void setTime(long time): void setTime(long time) method is used to set the present date and time to the time specified as a parameter.

Below are the examples of java.util.Date:

Code:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created to store one date
Date dat=new Date(2020,6,01);
long lil=2000;
//settime() method is used to set the time to the given time specified
dat.setTime(lil);
System.out.println("The time after setting the time to given time is : "+dat.toString());
}
}
登录后复制

Output:

java.util.Date

Explanations: In the above program, an instance of the date class is created to store one date. Then setTime() method id used to set the present time to the given date.

Conclusion

In this tutorial, we understand the concept of java.util.Date in Java through definition, its constructors and methods through programming examples and their outputs.

以上是java.util.Date的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板