`
j1a1v1a1
  • 浏览: 28589 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

日期转换

 
阅读更多
/**
* 日期工具类
* @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