本文实例讲述了Java常用的时间工具类。分享给大家供大家参考,具体如下:
				?
			
	
						
						
						
						
						
						
						
																		
    
        
    
        
                        
                
                    
                
                
                
                    
                
                
                
                    
                
                
                
                    
                
                        
    
 																		
						
																		
    
        
 												
						
																		
	
	
		
				
			
																		
						
						
					
				
				                | 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
 
								48
 
								49
 
								50
 
								51
 
								52
 
								53
 
								54
 
								55
 
								56
 
								57
 
								58
 
								59
 
								60
 
								61
 
								62
 
								63
 
								64
 
								65
 
								66
 
								67
 
								68
 
								69
 
								70
 
								71
 
								72
 
								73
 
								74
 
								75
 
								76
 
								77
 
								78
 
								79
 
								80
 
								81
 
								82
 
								83
 
								84
 
								85
 
								86
 
								87
 
								88
 
								89
 
								90
 
								91
 
								92
 
								93
 
								94
 
								95
 
								96
 
								97
 
								98
 
								99
 
								100
 
								101
 
								102
 
								103
 
								104
 
								105
 
								106
 
								107
 
								108
 
								109
 
								110
 
								111
 
								112
 
								113
 
								114
 
								115
 
								116
 
								117
 
								118
 
								119
 
								120
 
								121
 
								122
 
								123
 
								124
 
								125
 
								126
 
								127
 
								128
 
								129
 
								130
 
								131
 
								132
 
								133
 
								134
 
								135
 
								136
 
								137
						  | 
package org.zhy.date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 时间类型工具类
*
* @author zhengyi
*
*/
public class DateUtils {
// 时间格式定义
public static final String DATE_PATTERN_YYYY_MM_DD = "yyyy-MM-dd"; // 2011-10-09
public static final String yyyyMMddhhmmss = "yyyyMMddhhmmss";// 20111009100155
public static final String yyyy_MM_ddhhMMss = "yyyy-MM-dd hh:MM:ss";// 2011-10-09
// 10:01:55
// 时间格式:年月日时分秒
public static final int YEAR = 1;// 年
public static final int MONTH = 2;// 月
public static final int DAY = 3; // 日
public static final int HOUROFDAY = 4;// 时
public static final int MINUTE = 5;// 分
public static final int SECOND = 6;// 秒
/**
* 将时间转换为字符串
*
* @param date
* :需要转换的时间
* @param date_fomat
* :时间格式
* @return String:转换后的格式
*/
public static String DateToString(java.util.Date date, String date_fomat) {
DateFormat df = new SimpleDateFormat(date_fomat);
return df.format(date);
}
/**
* 根据年月日时分秒生成Date并返回
*
* @param year
* :年
* @param month
* :月
* @param dayOfMonth
* :日
* @param hourOfDay
* :时
* @param minute
* :分
* @param second
* :秒
* @return
*/
public static Date stringToDate(int year, int month, int dayOfMonth,
int hourOfDay, int minute, int second) {
GregorianCalendar gc = new GregorianCalendar(year, month, dayOfMonth,
hourOfDay, minute, second);
Date dt = gc.getTime();
return dt;
}
/**
* 根据年月日生成Date并返回
*
* @param year
* :年
* @param month
* :月
* @param dayOfMonth
* :日
* @return Date:返回的Date对象
*/
public static Date stringToDate(int year, int month, int dayOfMonth) {
GregorianCalendar gc = new GregorianCalendar(year, month, dayOfMonth);
Date dt = gc.getTime();
return dt;
}
/**
* 是否为闰年
*
* @param date
* @return
*/
public static boolean isLeapYear(Date date) {
GregorianCalendar gc = gcToDate(date);
return gc.isLeapYear(findYearByDate(date, YEAR));
}
/**
* 获得日期中的年月日时分秒
*
* @param date
* :需要获取的时间
* @param type
* :获取的类型,类内常量
* @return
*/
public static int findYearByDate(Date date, int type) {
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int number=0;
switch (type) {
case YEAR :
number= cd.get(Calendar.YEAR);
break;
case MONTH :
number= cd.get(Calendar.MONTH);
break;
case DAY :
number= cd.get(Calendar.DAY_OF_MONTH);
break;
case HOUROFDAY :
number= cd.get(Calendar.HOUR_OF_DAY);
break;
case MINUTE :
number= cd.get(Calendar.MINUTE);
break;
case SECOND :
number= cd.get(Calendar.SECOND);
break;
default :
number= 0;
}
return number;
}
/**
* 私有函数,将Date类型转换为GregorianCalendar类型以便类内使用
*
* @param date
* @return
*/
private static GregorianCalendar gcToDate(Date date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc;
}
}
 | 
希望本文所述对大家java程序设计有所帮助。
相关文章
             猜你喜欢
        
        - 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
 - 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
 - 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
 - ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
 - ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
 
			TA的动态
			
		
				- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
 - 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
 - 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
 - 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
 - 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
 
快网idc优惠网
QQ交流群
				您的支持,是我们最大的动力!				
			
		
        热门文章
        
    
    - 
            2025-06-04 94
 - 
            2025-05-27 65
 - 
            2025-05-27 22
 - 
            2025-05-29 74
 - 
            2025-05-27 72
 
		热门评论
	
	
        
    		
            	
        
        