天天看点

时间日期函数

2.得到当前日期
Date date=new Date();//通过日期转换成字符串输出
	  DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  String time=format.format(date);
   //通过字符串得到日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse("2008-08-08 12:10:12")
            //得到与当前日期相差几天的日期
public static Date getNextDay(Date date,int a) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH,a);
        date = calendar.getTime();
        return date;
    }
3.设置定时器
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
//这个方法貌似不太正确
public class TestTimer {
    static int count = 0;
    
    public static void showTimer() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                ++count;
                System.out.println("时间=" + new Date() + " 执行了" + count + "次"); // 1次
            }
        };
        //设置执行时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
        //定制每天的21:09:00执行,
        calendar.set(year, month, day, 21, 9, 00);
        Date date = calendar.getTime();
        Timer timer = new Timer();
        System.out.println(date);
        
        int period = 2 * 1000;
        //每天的date时刻执行task,每隔2秒重复执行
        timer.schedule(task, date, period);
        //每天的date时刻执行task, 仅执行一次
        //timer.schedule(task, date);
    }
    public static void main(String[] args) {
        showTimer();
    }
}
//这一种比较可以
public static void main(String[] args) {
      myTest a=new myTest();
      Timer timer=new Timer();
      timer.schedule(a,new Date());
      //这里的date可以自定义
}
 public static  class myTest extends TimerTask {
	    	@Override
	    	public void run() {
	    	for (int i = 0; i < 10; i++) {
	    		System.out.println("你好啊...");
			}
	    	}
	    }
4.java得到本机ip
InetAddress ia=null;
	try {
		ia=ia.getLocalHost();
		
		String localname=ia.getHostName();
		String localip=ia.getHostAddress();
		System.out.println("本机名称是:"+ localname);
		System.out.println("本机的ip是 :"+localip);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
5.得到前一天的日期
 //两个日期相差多少天
	public static int getBetweenDays(String date1,String date2) throws ParseException {
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");  
	    Date smdate=sdf.parse(date1);  
	    Date bdate=sdf.parse(date2);  
	    Calendar cal = Calendar.getInstance();    
	    cal.setTime(smdate);    
	    long time1 = cal.getTimeInMillis();                 
	    cal.setTime(bdate);    
	    long time2 = cal.getTimeInMillis();         
	    long between_days=(time2-time1)/(1000*3600*24);  
	   return Integer.parseInt(String.valueOf(between_days));           
	}
	//得到日期,输入日期类型,得到当天日期的字符型
	public static String getTodayDatestr(String type){
		 Date date=new Date();//通过日期转换成字符串输出
		  DateFormat format=new SimpleDateFormat(type);
		  String time=format.format(date);
		return time;
	}
	//通过字符串得到日期
	public static Date getDateBystr(String datestr,String type){
		 SimpleDateFormat sdf = new SimpleDateFormat(type);
		 Date date=null;
		 try {
			 date = sdf.parse(datestr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	//得到当前或之前或之后的日期 字符型
	public static String getBeforeOrAfterDate(String type,int AddorCut){
		Calendar cal = Calendar.getInstance();
	  	cal.add(Calendar.DATE, AddorCut);
	  	String date = new SimpleDateFormat(type).format(cal.getTime());
		return date;
	}