Home > Java > javaTutorial > body text

java abstract class Calendar

巴扎黑
Release: 2016-12-02 09:23:33
Original
1432 people have browsed it

In actual programming, we usually need to edit the time, sometimes we need the current time, sometimes we need a few days later, then we need to use the abstract class Calendar to complete it,
First we need to call the getInstance() method of Calendar. Returns an instance of Calendar subclass. Then you can call the Calendar method,
Code:

import java.util.Calendar;
public class CalendarTeST {
public static void main(String[] args) {
//因为Calendar是个抽象类,所以不能创建实例,需要用Calendar的getInstance()方法来创建出Calendar的一个子类。
Calendar c=Calendar.getInstance();
c.set(1995, 01, 26, 23, 30, 47);//定义c的时间为1995年1月26日23点30分47秒
System.out.println("返回定义的时间:"+c.getTime());
long day=c.getTimeInMillis();//将时间以微秒为单位赋值给day。
day+=1000*60*60*24;//将day加上一天。day的原本单位是微秒。
c.setTimeInMillis(day);//将day的数据传入c。
System.out.println("将day加一天后的时间:"+c.getTime());
c.add(c.DATE, 3);//将天数加3,
System.out.println("天数加了三天:"+c.getTime());
c.setTimeInMillis(day);//初始化c,让天数回到加3前的状态
c.roll(c.DATE, 3);//滚动3天,滚动的话只有天数会变,月份是不变的。
System.out.println("roll方法,滚动三天:"+c.getTime());
c.set(c.DATE, 12);//直接设定天数的值。
System.out.println("设定天数为12:"+c.getTime());
c.set(c.MONTH,0);//直接改变月份,
System.out.println("设定月份为0:"+c.getTime());
//不知道为啥月份是零基的。
}
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!