例如
String endTime = "2010-10-14 00:41:58";int seconds = 3661;3661 等于 01:01:01如何将字符串转化为 "2010-10-14 01:42:59";求方法!
String endTime = "2010-10-14 00:41:58";int seconds = 3661;3661 等于 01:01:01如何将字符串转化为 "2010-10-14 01:42:59";求方法!
try {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse("2010-10-14 00:41:58");
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.SECOND, 3661);
String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
System.out.println(s);
} catch (Exception e) {
}
int seconds = 3661;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(endTime));
calendar.add(Calendar.SECOND, seconds);
String dateStr = sdf.format(calendar.getTime());
System.out.println("dateStr:" + dateStr);
c.setTime(Timestamp.valueOf("2010-10-14 00:41:58"));
c.add(Calendar.SECOND, 3661);
Date date = c.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(date);
System.out.println(sdf.format(date));
它们都转为long型的毫秒再相加,
把结果再次转为Date型
String endTime = "2010-10-14 00:41:58"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(endTime));
int seconds = 3661;
c.add(Calendar.SECOND, seconds);
String newTime = sdf.format(c.getTime());
System.out.println(newTime);
public static void main(String[] args) throws Exception {
String endTime = "2010-10-14 00:41:58";
int seconds = 3661;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(endTime));
c.add(Calendar.SECOND , seconds); System.out.print(sdf.format(c.getTime()));
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String endTime = "2010-10-14 00:41:58";
java.util.Date d = sdf.parse(endTime);
c.setTime(d);
int seconds = 3661;
c.setTimeInMillis(c.getTimeInMillis()+seconds*1000);
System.out.println(sdf.format(c.getTime()));
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TimeTest
{
public static void main(String[] args)
{
String endTime = "2010-10-14 00:41:58";
int seconds = 3661;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar gc=new GregorianCalendar(2010,10-1,14,00,41,58);
gc.add(Calendar.SECOND,seconds);
System.out.println(sdf.format(gc.getTime()));
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TimeTest
{
public static void main(String[] args)
{
// String endTime = "2010-10-14 00:41:58";
int seconds = 3661;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar gc=new GregorianCalendar(2010,10-1,14,00,41,58);
// gc.add(Calendar.SECOND,seconds);
// System.out.println(sdf.format(gc.getTime()));
long l1=gc.getTimeInMillis()+seconds*1000;
System.out.println(sdf.format(l1));
}
}
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
public class TimeTest
{
public static void main(String[] args)
{
// String endTime = "2010-10-14 00:41:58";
int seconds = 3661;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar gc=new GregorianCalendar(2010,10-1,14,00,41,58);
// gc.add(Calendar.SECOND,seconds);
// System.out.println(sdf.format(gc.getTime()));
long l1=gc.getTimeInMillis()+seconds*1000;
System.out.println(sdf.format(new Date(l1)));
}
}