第九章第五题(使用GregorianCalendar类)(Use GregorianCalendar class)
*9.5(使用GregorianCalendar类)Java API中有一个位于包java.util中的类GregorianCalendar,可以使用它获得某个日期的年、月、日。他的无参构造方法构建一个当前日期的实例,get(GregorianCalendar.YEAR)、get(GregorianCalendar.MONTH)和get(GregorianCalendar.DAY_OF_MONTH) 方法返回年、月和日。编写一个程序完成两个任务:
- 显示当前的年、月和日。
- GregorianCalendar类包含setTimeInMills(long)方法,可以用于设置从1970年1月1日算起的一个特定的流逝时间值。将这个值设置为1234567898765L,然后显示对应的年、月和日。
*9.5(Use GregorianCalendar class)The Java API has a java.util You can use it to get the year, month, and day of a date. His nonparametric construction method builds an instance of the current date, get( GregorianCalendar.YEAR )、get( GregorianCalendar.MONTH )And get( GregorianCalendar.DAY_ OF_ The month) method returns the year, month, and day. Write a program to complete two tasks
- Displays the current year, month, and day.
- The gregoriancandar class contains the settimeinmills (long) method that can be used to set a specific elapsed time value from January 1, 1970. Set this value to 12345678988765l, and then display the corresponding year, month, and day.
参考代码:
package chapter09;
import java.util.GregorianCalendar;
public class Code_05 {
public static void main(String[] args){
GregorianCalendar calendar = new GregorianCalendar();
System.out.println(calendar.get(calendar.YEAR) + ":" + calendar.get(calendar.MONTH) + ":" + calendar.get(calendar.DAY_OF_MONTH));
calendar.setTimeInMillis(1234567898765L);
System.out.println(calendar.get(calendar.YEAR) + ":" + calendar.get(calendar.MONTH) + ":" + calendar.get(calendar.DAY_OF_MONTH));
}
}
- 结果显示:
2020:9:30
2009:1:14
Process finished with exit code 0
版权声明:本文为jxh1025_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。