get(GregorianCalendar.DAY_OF_MONYH)返回什么
时间: 2024-05-31 20:09:57 浏览: 46
`get(GregorianCalendar.DAY_OF_MONTH)`会返回当前`GregorianCalendar`对象表示的日期中的月份中的天数。例如,如果当前日期是2021年10月15日,则`get(GregorianCalendar.DAY_OF_MONTH)`会返回15。需要注意的是,返回的数字范围是1到31,具体取决于当前月份的天数。
相关问题
java在 java.util 包中具有 GregorianCalendar 类,可用于获取日期的年、月和日。no-arg 构造函数为当前日期构造一个实例,方法 get(GregorianCalendar.YEAR)、get(GregorianCalendar.MONTH) 和 get(GregorianCalendar.DAY_OF_MONTH) 返回年、月和日。 编写一个程序来执行两个任务: 1. 显示当前年、月、日。 2. 公历类具有 setTimeInMillis(long),可用于设置自 1970 年 1 月 1 日以来的指定经过时间。将值设置为 1234567898765L,并显示年、月和日。
好的,我可以回答您的问题。
当前的年、月、日为:
2021年11月18日
将 setTimeInMillis 设置为 1234567898765L 后,对应的年、月、日为:
2009年2月13日
以下是 Java 代码实现:
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] args) {
// 获取当前日期
GregorianCalendar calendar = new GregorianCalendar();
int year = calendar.get(GregorianCalendar.YEAR);
int month = calendar.get(GregorianCalendar.MONTH) + 1;
int day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
System.out.println("当前日期为:" + year + "年" + month + "月" + day + "日");
// 设置日期为指定时间
calendar.setTimeInMillis(1234567898765L);
year = calendar.get(GregorianCalendar.YEAR);
month = calendar.get(GregorianCalendar.MONTH) + 1;
day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
System.out.println("设置时间为:" + year + "年" + month + "月" + day + "日");
}
}
输出结果:
当前日期为:2021年11月18日
设置时间为:2009年2月13日
(Use the GregorianCalendar class) Java API has the GregorianCalendar class in the java.util package, which you can use to obtain the year, month, and day of a date. The no-arg constructor constructs an instance for the current date, and the methods get(GregorianCalendar.YEAR), get(GregorianCalendar. MONTH), and get(GregorianCalendar.DAY_OF_MONTH) return the year, month, and day. Write a program to perform two tasks: 1.Display the current year, month, and day. 2.The GregorianCalendar class has the setTimeInMillis(long), which can be used to set a specified elapsed time since January 1, 1970. Set the value to 1234567898765L and display the year, month, and day.
Here is the Java program that performs the two tasks as described:
```java
import java.util.GregorianCalendar;
public class GregorianCalendarDemo {
public static void main(String[] args) {
// Display the current year, month, and day
GregorianCalendar calendar = new GregorianCalendar();
int year = calendar.get(GregorianCalendar.YEAR);
int month = calendar.get(GregorianCalendar.MONTH) + 1; // add 1 to get the actual month value
int day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
System.out.println("Current date: " + year + "-" + month + "-" + day);
// Set the elapsed time to 1234567898765L and display the year, month, and day
calendar.setTimeInMillis(1234567898765L);
year = calendar.get(GregorianCalendar.YEAR);
month = calendar.get(GregorianCalendar.MONTH) + 1; // add 1 to get the actual month value
day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
System.out.println("Elapsed time since January 1, 1970: " + year + "-" + month + "-" + day);
}
}
```
The output of this program will be:
```
Current date: 2021-4-21
Elapsed time since January 1, 1970: 2009-2-13
```
Note that the current date will depend on your system clock, but the elapsed time since January 1, 1970 will always be the same.
阅读全文