1 SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 2 java.util.Date currentTime = new java.util.Date();//得到当前系统时间 3 String str_date = timeformat.format(currentTime); //将日期时间格式化
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int date = c.get(Calendar.DATE); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second); } }
System.currentTimeMillis()
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()));
Date date=new Date();//取时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 date=calendar.getTime(); //这个时间就是日期往后推一天的结果 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(date); System.out.println(dateString); 这里的Date是 import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; 如果要用到jdbc,java.sql.*中也有Date java.sql.Connection java.sql.ResultSet
Date d = new Date();
SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小时制
System.out.println(ss.format(d));
Date date = new Date();
SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
String LgTime = sdformat.format(date);
System.out.println(LgTime);
结果为
2008-05-28 01:32:54
2008-05-28 13:32:54
Date类,已经很少用了。更多使用的是Calendar
Calendar date = Calendar.getInstance();
date.get(Calendar.HOUR_OF_DAY );//得到24小时机制的
date.get(Calendar.HOUR);// 得到12小时机制的