java如何在现有时间加上20s

比如我现在的时间是2013-04-28 21:00:00,我如何得到2013-04-28 21:00:20

第1个回答  2013-04-28
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {
public static void main(String[] args) {
SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = getDate();
System.out.println(dateFormat.format(date));
}

private static Date getDate() {
Calendar calendar = Calendar.getInstance();//获得当前时间
calendar.add(Calendar.SECOND, 20);//加20秒
return calendar.getTime();//返回Date类
}
}
第2个回答  2013-04-28
public static void main(String[] args) {
SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr="2013-04-28 21:00:00";
Date d=null;
try {
formater.setLenient(false);
d=formater.parse(dateStr);
} catch(Exception e) {
d=null;
}
Calendar canlender=Calendar.getInstance();
canlender.setTime(d);
canlender.add(Calendar.SECOND, 20);
String result="";
try {
result=formater.format(canlender.getTime());
} catch(Exception e) {
}
System.out.println("增加之后:"+result);
}
第3个回答  2013-04-28
楼上的有点麻烦 我写个简单点的

Date date = new Date();
long time=date.getTime()+20000; //这是毫秒数
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
System.out.print(sdf.format(time));
第4个回答  2013-04-28
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 15);
Date d = cal.getTime();
第5个回答  2013-04-28
Date d = new Date();
Thread.sleep(20000);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.format(d);
System.out.println(d);

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网