Java常用的时间工具类实例

2025-05-29 0 89

本文实例讲述了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程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java常用的时间工具类实例 https://www.kuaiidc.com/116241.html

相关文章

发表评论
暂无评论