JAVA中獲取系統當前時間
1.通過Util包中的Date獲取
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat(“yyyy-MM-dd :hh:mm:ss”);
System.out.println(dateFormat.format(date));
2.通過Util包的Calendar 獲取
Calendar calendar= Calendar.getInstance();
SimpleDateFormat dateFormat= new SimpleDateFormat(“yyyy-MM-dd :hh:mm:ss”);
System.out.println(dateFormat.format(calendar.getTime()));
3.通過Util包的Calendar 獲取時間,分別獲取年月日時分秒
Calendar cal=Calendar.getInstance();
int y=cal.get(Calendar.YEAR);
int m=cal.get(Calendar.MONTH);
int d=cal.get(Calendar.DATE);
int h=cal.get(Calendar.HOUR_OF_DAY);
int mi=cal.get(Calendar.MINUTE);
int s=cal.get(Calendar.SECOND);
System.out.println(“現在時刻是”+y+”年”+m+”月”+d+”日”+h+”時”+mi+”分”+s+”秒”);
java中時間24小時和12小時設定
Calendar
Calendar date = Calendar.getInstance();
date.get(Calendar.HOUR_OF_DAY );//得到24小時機制的
date.get(Calendar.HOUR);// 得到12小時機制的
SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat(“yyyyMMdd_HH_mm_ss”);//24小時機制
SimpleDateFormat format = new SimpleDateFormat(“yyyyMMdd_hh_mm_ss”);//12小時機制
時間轉為字串型別
java.text.SimpleDateFormat類
字串轉換成日期型別:
方法1:
Date date=new Date(“2008-04-14”);
方法2:
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);//小寫的mm表示的是分鐘
String dstr=”2008-4-24”;
java.util.Date date=sdf.parse(dstr);
日期轉換成字串:
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
java.util.Date date=new java.util.Date();
String str=sdf.format(date);