java 多种实现获取时间的方法

实现Java获取时间的方法主要有一下几种方法:

1.获取当前时间

(1)  
Java代码
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "hh:mm:ss");  
String datetime = tempDate.format(new java.util.Date());
(2)
Java代码
Calendar now=Calendar.getInstance();  
String time=now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-"+now.get(Calendar.DAY_OF_MONTH)+" "+now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE)+":"+now.get(Calendar.SECOND);
(3)
Java代码
Date curDate= new Date(System.currentTimeMillis());
 
2.获取年,月,日
(1)
Java代码
String year=String.valueOf(c.get(Calendar.YEAR));  //年份获取
String month=String.valueOf(c.get(Calendar.MONTH)+1);  //月获取      
String day=String.valueOf(c.get(Calendar.DAY_OF_MONTH)); // 日获取
(2)计算某一天是这一年的星期几!
Java代码
Calendar cal=Calendar.getInstance();  
cal.set(Calendar.YEAR, 2006);  
cal.set(Calendar.MONTH,1);  
cal.set(Calendar.DAY_OF_MONTH, 3);  
int weekno=cal.get(Calendar.WEEK_OF_YEAR); 
(3) 计算一年中的第几星期是几号
Java代码
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
Calendar cal=Calendar.getInstance();  
cal.set(Calendar.YEAR, 2006);  
cal.set(Calendar.WEEK_OF_YEAR, 1);  
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
System.out.println(df.format(cal.getTime()));
输出:  
2006 - 01 - 02  







你可能感兴趣的:(java 多种实现获取时间的方法)