日期:2014-05-20 浏览次数:20991 次
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date = format.parse("2011-10-18 15:24:25"); System.out.print(date.getTime()); } catch (ParseException e) { e.printStackTrace(); }
------解决方案--------------------
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; public class CalendarDemo1 { public static void main(String[] args) { String timeStr="2011-10-18 15:24:25"; DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=null; long timeLong=0; try{ date=df.parse(timeStr); timeLong=date.getTime(); System.out.println("long:"+timeLong); }catch(ParseException e){ e.printStackTrace(); } } }
------解决方案--------------------
下面是有时间相加的,其实不是一定要用Calendar的
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; import java.util.Calendar; public class CalendarDemo1 { public static void main(String[] args) { String timeStr="2011-10-18 15:24:25"; DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=null; long timeLong=0; long timeAdded=0; try{ date=df.parse(timeStr); timeLong=date.getTime(); System.out.println("long:"+timeLong); Calendar c=Calendar.getInstance(); c.setTime(date); c.add(Calendar.MINUTE,40); timeAdded=c.getTimeInMillis(); System.out.println("Added time:"+c.getTime()); System.out.println("Added 40 minutes:"+timeAdded); }catch(ParseException e){ e.printStackTrace(); } } }
------解决方案--------------------
顶4楼 正解
------解决方案--------------------
如果再添加10小时,加上这一行
c.add(Calendar.HOUR_OF_DAY,10);
------解决方案--------------------
public static void main(String[] args) { try { DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = format.parse("2011-10-18 15:24:25"); Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.HOUR_OF_DAY, 10); c.add(Calendar.MINUTE, 40); String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c .getTime()); System.out.println(s); } catch (Exception e) { e.printStackTrace(); } }