- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- /**
- * 读取当前系统时间
- */
- public class ReadSystemTime {
- public static void main(String[] args) {
- getTime_1();
- getTime_2();
- getTime_3();
- }
- /**
- * 方法1
- */
- public static void getTime_1(){
- //此处字符串的格式可以修改
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date currentTime = new Date();//得到当前系统时间
- String timeStr = formatter.format(currentTime); //将日期时间格式化
- System.out.println("[1] " timeStr);
- //输出:[1] 2009-08-18 20:06:13
- }
- /**
- * 方法2
- */
- public static void getTime_2(){
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
- System.out.println("[2] " formatter.format(System.currentTimeMillis()));
- //输出:[2] 2009/08/18 20:18:48
- }
- /**
- * 方法3
- */
- public static void getTime_3(){
- Calendar todaysDate = new GregorianCalendar();
- int year = todaysDate.get(Calendar.YEAR);
- int month = todaysDate.get(Calendar.MONTH) 1;
- int day = todaysDate.get(Calendar.DAY_OF_MONTH);
- int hourOfDay = todaysDate.get(Calendar.HOUR_OF_DAY);//24小时制
- //int hour = todaysDate.get(Calendar.HOUR); //12小时制
- int minute = todaysDate.get(Calendar.MINUTE);
- int second = todaysDate.get(Calendar.SECOND);
- System.out.println("[3] " year "." month "." day
- " " hourOfDay ":" minute ":" second);
- //输出:[3] 2009.8.18 20:23:51
- }
- }