Download API_project/Date_module/src/Date_package/Date_demo2.java from zxy0307/Java: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/zxy0307/Java/resolve/main/API_project/Date_module/src/Date_package/Date_demo2.java
- Command line
-
hf download hf://zxy0307/Java/API_project/Date_module/src/Date_package/Date_demo2.java
-
curl -L -o Date_demo2.java https://huggingface.co/zxy0307/Java/resolve/main/API_project/Date_module/src/Date_package/Date_demo2.java
1.48 kB
| package Date_package; | |
| import java.util.Date; | |
| import java.util.Random; | |
| public class Date_demo2 { | |
| public static void main(String[] args) { | |
| /* | |
| 需求1:打印时间原点开始一年后的时间 | |
| 需求2:定义任意两个Date对象,比较一下哪个时间在前,哪个时间在后 | |
| */ | |
| //需求1:打印时间原点开始1年之后的时间 | |
| //1、创建一个对象,表示时间原点 | |
| Date d1 = new Date(0L); | |
| //2、获取d1时间的毫秒值 | |
| long time = d1.getTime(); | |
| //3、在这个基础上我们要加一年的毫秒值 | |
| time += 1000L * 60 * 60 * 24 * 365; | |
| //4、把计算之后的时间毫秒值,再设置回di当中 | |
| d1.setTime(time); | |
| //5、打印d1 | |
| System.out.println(d1); | |
| //需求2:定义任意两个Date对象,比较一下哪个时间在前,哪个时间在后 | |
| Random r = new Random(); | |
| //创建两个时间对象 | |
| Date d2 = new Date(Math.abs(r.nextInt())); | |
| Date d3 = new Date(Math.abs(r.nextInt())); | |
| long time2 = d2.getTime(); | |
| long time3 = d3.getTime(); | |
| if (time2 > time3) { | |
| System.out.println("第一个时间在后,第二个时间在前"); | |
| } else if (time2 < time3) { | |
| System.out.println("第一个时间在前,第二个时间在后"); | |
| } else { | |
| System.out.println("两个时间一样"); | |
| } | |
| } | |
| } | |