java.time是jdk1.8才用的
时间管理
package com.test.time; import java.time.*; /** * Created by MY on 2017/8/7. */ public class TimeTest { public static void main(String[] args) { //当前日期 输出为2017-08-07 LocalDate date = LocalDate.now(); //三天后 2017-08-10 LocalDate days = date.plusDays(3); //当前时间 12:03:13.276 LocalTime time = LocalTime.now(); //五分钟前 11:59:32.276 LocalTime time1 = time.minusMinutes(5); //生日 MonthDay monthDay = MonthDay.of(8, 7); //获取当前的月和日 MonthDay day = MonthDay.from(date); //获取月 AUGUST Month month = Month.from(date); //获取周几 Monday DayOfWeek week = DayOfWeek.from(date); if (monthDay.equals(day)){ System.out.println("生日"); }else{ System.out.println("不是"); } /*System.out.println(month); System.out.println(day);*/ LocalDateTime now = LocalDateTime.now(); System.out.println(now); } }
任务管理
package com.test.time; import java.util.Timer; import java.util.TimerTask; /** * Created by MY on 2017/8/7. */ public class MyTask { public static void main(String[] args) { //计时器 Timer timer=new Timer(); //执行任务 timer.schedule(new TimerTask() { @Override public void run() { System.out.println("yj"); } //延迟三秒,每秒执行一次 },3000,1000); } }