lesson6.8与日期和事件有关的类
来源:互联网 发布:小米抢购软件手机版 编辑:程序博客网 时间:2024/06/05 04:29
Date和DateFormat类SimpleDateFormat类
Date类表示时间,DateFormat类用来格式化解释日期和时间,是抽象类,通常使用它的子类SimpleDateFormat。
使用示例:
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");//以类似2003-03-15的方 //式解释日期和时间
Date d = s1.parse("2003-03-15");//以s1的格式存入d,解析字符串文本生成Date
SimpleDateFormat s2 = new SimpleDateFormat("yyyy年MM月dd日");//另一种格式
System.out.println("s2.format(d)");//将给定的 Date
格式化为日期/时间字符串
Calendar类主要用于完成时期时间字段之间的操作,也是个抽象基类
常用Calendar gc = Calendar.getInstance();//获得日历
static void add(int field, int amount);//在field的字段上增加amount,field可以是Calendar.YEAR/MONTH/DAY_OF_MONTH等等。
int get(int field);//获取某字段的值,field同上
void set(int field, int amount);//将给定的日历字段设为给定值。
java.util.Timer类
一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。
schedule方法会启动一个线程,执行TimerTask里的run方法,TimerTask实现Runnable接口,所以TimerTask必须覆盖run方法。
void schedule(timerTask task, long delay);//延迟delay执行task
void schedule(timerTask task, Date time);//在指定时间time执行task
void schedule(timerTask task,Date firsttime, lont period);//在制定的时间重复执行task,间隔period
注意:该方法启动的现成是前台线程,要结束有两种方法
Timer.cancel()//结束线程
TimerTask.cancel()//取消任务
习题:
1.将2003-03-15以2003年03月15号打印
2. 输出当前日期和315天以后的日期
3.五秒后启动计算器程序,并且把程序关闭
import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class TestDate {public static void main(String [] args){//计算距当前日期315天后的日期Calendar gc = Calendar.getInstance();System.out.println(gc.get(Calendar.YEAR)+"年"+gc.get(Calendar.MONTH)+"月"+gc.get(Calendar.DAY_OF_MONTH)+"日"+gc.get(Calendar.HOUR_OF_DAY)+"时"+gc.get(Calendar.MINUTE)+"分"+gc.get(Calendar.SECOND)+"秒");gc.add(Calendar.DAY_OF_YEAR, 315);System.out.println(gc.get(Calendar.YEAR)+"年"+gc.get(Calendar.MONTH)+"月"+gc.get(Calendar.DAY_OF_MONTH)+"日"+gc.get(Calendar.HOUR_OF_DAY)+"时"+gc.get(Calendar.MINUTE)+"分"+gc.get(Calendar.SECOND)+"秒");//将2003-03-15存入Date以另一种形式打印SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat s1 = new SimpleDateFormat("yyyy年MM月dd日");try{Date d = s.parse("2003-03-15");System.out.println(s1.format(d));}catch(Exception e){e.printStackTrace();}//程序启动五秒后启动计算器程序Timer t = new Timer();MyTask mt = new MyTask(t);t.schedule(mt, 5000);//默认是前台线程,打开了计算器程序不会执行完毕//结束程序的方法//Timer.cancel()//结束线程//TimerTask.cancel()//取消任务}}class MyTask extends TimerTask{private Timer t = null;MyTask(Timer t){this.t =t; }public void run(){try{Runtime.getRuntime().exec("calc.exe");}catch(Exception e){e.printStackTrace();}t.cancel();}}
- lesson6.8与日期和事件有关的类
- 与日期和时间有关的类
- Java与日期和时间有关的类
- 时间和日期有关的类
- 和日期有关的sql
- JAVA高级01_08 与日期和时间有关的类 2011-4-21
- lesson6.6PipedInputStream和ByteArrayInputStream类
- NeHe的OpenGL教程 lesson6-8 学习心得
- 和日期有关的类、Math类、Random类
- 一些与日历,日期有关的用法
- JSP中有关时间和日期类的使用与JSP中的字符替换函数 str_replace()实现
- html事件的使用与什么有关
- 与时间有关的类和方法
- Grid事件解析与有关ItemCreated和ItemDataBound与ItemCommand的理解
- lesson6.7Properties类
- DateUtil 有关时间、日期的公用类
- 有关日期类的成员函数
- 日期有关的查询
- : “m_RichEdit”: 未知重写说明符解决办法
- Problem A: 递归基础之放苹果
- 读书计划
- mysql的搭建
- php+js+mysql+ajax实现简单留言板功能,新手教程
- lesson6.8与日期和事件有关的类
- JS事件冒泡代码
- java的keyAdapter总结
- android 使用ContentObserve对数据库的值变化进行监听
- Logistic回归与梯度上升算法
- 列表、元组、字典、集合详解
- python学习一(编码问题)
- Unity AssetBundle加载错误(已解决)
- Kubernetes 1.6高可用详细部署流程