java中的日期和时间由一个名为java.util.Date类的类来表示,该类提供了处理java中的时间和日期的方法和构造函数以及Serialized、Comparable
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)); } }
输出:
说明:在上面的程序中,定义了一个名为 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()); } }
输出:
说明:在上面的程序中,创建了日期类的实例。然后在日期类实例上调用克隆方法来创建给定日期的克隆。
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)); } }
输出:
说明:在上面的程序中,创建了一个日期类的实例来存储一个日期。然后创建日期类的实例来存储另一个日期。然后使用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()); } }
输出:
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:
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中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

WebStorm Mac版
好用的JavaScript开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。