论坛首页 入门技术论坛

日期工具类

浏览 1872 次
锁定老帖子 主题:日期工具类
该帖已经被评为新手帖
作者 正文
   发表时间:2009-02-01  
/**
 * 日期工具类
 * @author wangzb
 */
import java.util.*;
import java.text.SimpleDateFormat;
public class DateUtil {
	/**
	* 根据当前日期及增加天数得到相应日期
	* @param s
	* @param n
	* @return
	* @throws Exception
	*/
	public static String addDay(String s,int n) {
  	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Calendar cd = Calendar.getInstance();
	try{	
	cd.setTime(sdf.parse(s));
	cd.add(Calendar.DATE, n);
	}catch(Exception e){}
	return sdf.format(cd.getTime());
	}
	/**
	 * 得到当前日期yyyy-MM-dd;
	 * @return String 
	 */
	public static String getSystemDate() {
			java.util.Date date = new java.util.Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String mDateTime1 = formatter.format(date);
			return mDateTime1;
	}


				Date date = new Date();
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String startTime = df.format(System.currentTimeMillis()- 3600000*24*7);
				Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(df.format(date));//系统当前时间
				Date date2 = new SimpleDateFormat("yyyy-mm-dd").parse(timer);//另外娶到的时间
				long ln = date1.getTime()-date2.getTime();
				if((ln-3600000*24)<0){//两个时间差与一天的时间坐比较
					sb.append(ln+"\r\n");
				}

}

String time =(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).toString();



//时间相减
Calendar cl=Calendar.getInstance(); 
		Date d = cl.getTime();
		System.out.println("当前时间为:"+(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d)).toString());
		long t1 = cl.getTimeInMillis();//当前时间
		long t2 = t1 - 20*1000;//runtime秒钟之前的时间
		cl.setTimeInMillis(t2); 
		Date date=cl.getTime();
		String time =(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date)).toString();//runtime秒钟之前的时间,格式为yyyy-MM-dd HH:mm:ss
		System.out.println("20秒钟时间为:"+time);
 
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics