日期:2014-05-20 浏览次数:20831 次
import java.io.BufferedReader; import java.io.InputStreamReader; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class GetCurrentTime { static void useDate() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); System.out.println(sdf.format(date)); } static void useCalendar() { Calendar ca = Calendar.getInstance(); //System.out.println(ca); int year = ca.get(Calendar.YEAR);//获取年份 int month = ca.get(Calendar.MONTH);//获取月份,从0-11开始 int day = ca.get(Calendar.DATE);//获取天数 int minute = ca.get(Calendar.MINUTE);//获取分钟 int hour = ca.get(Calendar.HOUR);//获取小时 int second = ca.get(Calendar.SECOND);//获取秒书 int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);//获取一周的第几天 System.out.println("Date = " + ca.getTime()); System.out.printf("年 = %5d , 月 = %5d , 日 = %5d %n", year, month + 1, day); System.out.printf("时 = %5d , 分 = %5d , 秒 = %5d %n", hour, minute, second); System.out.println("WeekOfYear = " + WeekOfYear);// } static void useDosCommand() { String ls_1; Process process; try { process = Runtime.getRuntime().exec("cmd /c echo %date% %time% "); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); while ((ls_1 = bufferedReader.readLine()) != null) System.out.println(ls_1); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { System.out.println("*************方式1****************"); useDate(); System.out.println("*************方式2****************"); useCalendar(); System.out.println("*************方式3****************"); useDosCommand(); } } /* *************方式1**************** 2011-10-07 13:45:39.328 *************方式2**************** Date = Fri Oct 07 13:45:39 CST 2011 年 = 2011 , 月 = 10 , 日 = 7 时 = 1 , 分 = 45 , 秒 = 39 WeekOfYear = 6 *************方式3**************** 2011-10-07 星期五 13:45:39.37 */
------解决方案--------------------
楼主,这个只要把("yyyy-MM-dd hh:mm:ss")中的hh换成HH就行了,hh表示12时制,HH表示24时制。
------解决方案--------------------
大家都好啊,才刷了一下就成五楼了
------解决方案--------------------
方法很多,这个也行
Calendar c=Calendar.getInstance(); c.setTime(new Date()); c.get(Calendar.HOUR); System.out.println(c.getTime().toString()); c.get(Calendar.HOUR_OF_DAY); System.out.println(c.getTime().toString());