public class DateUtils {
1 D) I- s5 s" a3 ^ ~: z /** , G4 r9 H2 }) P% x* D* p
* 获取当前时间日期的字符串 ' \9 _6 g& ^& ~0 U
*/
3 B3 ?! x$ t) r& _9 e" e public static String getCurrentDateStr(DateFormatType dateFormatType) { 2 z5 q1 v5 ^$ ~- {; {
Date date = getCurrentDate(); # o9 Q" B, l! L, N |
return (String) OpearationDate(date, dateFormatType.getValue()); : l2 t# [7 ]4 J, d/ j. P
}
/ H' [8 f- i( E, G& C /**
D8 j6 R: k0 { Q3 w * 时间、日期格式化成字符串
2 \/ P4 }( K, M* w) q. `1 P3 @ */
( ~+ a# u# O; g( R7 k( p3 j public static String formatDate(Date date, DateFormatType dateFormatType) {
6 p s* D; H' n: Y. | return (String) OpearationDate(date, dateFormatType.getValue());
6 W5 ^. J$ Q1 C+ n } % b, h% e A+ E2 b8 f# i4 ]) o6 a
/**
! U* x: E, `) u7 G7 K3 | V * 从字符串解析成时间、日期 7 e7 z* _. l& v- Y8 y
*/ " s( D! e3 M5 |, G1 B
public static Date parseDate(String dateStr, DateFormatType dateFormatType) { ; Z2 Q4 r- Q0 c
return (Date) OpearationDate(dateStr, dateFormatType.getValue()); ) r3 s6 s& g+ u7 m2 g
}
7 K' k: t ^7 x /** ! W% R3 s ?. Y/ N7 e
* 获取当前系统时间(原始格式)
* i8 @' ?. P, u */ 6 n" ^' K) @7 h$ b" [ K( D$ M5 n& e
public static Date getCurrentDate() { - `8 D2 ?* C5 p! e
Date date = new Date(System.currentTimeMillis());
2 ]' O. G2 q. j' z return date;
, N: x( `# q+ R7 M# ^; l1 l, ? } 4 e# k8 ?; D9 I
/**
1 Z1 y# b+ b- j/ ~* f4 p7 [- H * 获取当前日期的年、月、日、时、分、秒 : T+ S8 s0 Q/ G8 j
*/
' g8 W. s! F& \0 m public static int getCurrentTime(TimeFormatType timeFormatType) {
0 K& z; h" x T return getTime(getCurrentDate(), timeFormatType); # o$ G/ k* _5 `) p) A
} % ^5 U; S: T6 ` Z o
/** 6 h/ c% Q/ W3 K: F
* 获取指定日期的年、月、日、时、分、秒 i" n7 e* `! l" z) {9 K
*/ 0 b# ^/ t- `. q. i# r# R8 |" Y1 w
public static int getTime(Date date, TimeFormatType timeFormatType) { 2 R( |0 N+ U! b1 j2 j
try {
& ~" g9 T ?4 z% B" A Calendar c = Calendar.getInstance(); 2 o7 _' ^3 i" U$ D
c.setTime(date);
. Y" q- ^% @( c int type = timeFormatType.getValue(); - J$ ]# a/ `; f, |/ N O9 k& \6 b
int I = c.get(type);
9 @4 R* G8 p8 F M; E5 ?1 Q2 h6 q return type == 2 ? I + 1 : I; % O$ m X% M. ^! D, |7 I" H
} catch (Exception e) { 4 M6 D6 _1 T: W0 Q7 s, M# ~
throw new RuntimeException("获取失败", e);
! @. x5 i7 t2 g! l } } |