Package cn. hnedu. util;
Import java. io .*;
Import java. text .*;
Import java. util .*;
Import java. SQL. Timestamp;
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import java. util. Date;
Public class DateUtils {
Public DateUtils (){
}
Public final static Calendar ACC = Calendar. getInstance ();
Public static final int seconds = 1000;
Public static final int MINUTE = SECOND * 60;
Public static final int HOUR = MINUTE * 60;
Public static final int DAY = HOUR * 24;
Public static final int WEEK = DAY * 7;
Public static final int YEAR = DAY * 365; // or 366 ???
Public static final long GMT_VIETNAM_TIME_OFFSET = HOUR * 8;
Private static long SERVER_TIME_OFFSET = HOUR * 0;
Private static DateFormat ddMMyyyyFormat = new SimpleDateFormat ("dd/MM/yyyy ");
Private static DateFormat yyyymmddhhmmssFormat = new SimpleDateFormat (
"Yyyy-MM-dd kk: mm: ss ");
Private static DateFormat mmddhhmmFormat = new SimpleDateFormat (
"MM dd-mm kk: MM ");
Private static DateFormat yyyymmddhhmmFormat = new SimpleDateFormat (
"Yyyy-MM-dd kk: mm ");
Private static DateFormat dateFormat = SimpleDateFormat. getDateInstance (
SimpleDateFormat. DEFAULT );
Private static DateFormat datetimeFormat = SimpleDateFormat.
GetDateTimeInstance (SimpleDateFormat. DEFAULT, SimpleDateFormat. DEFAULT );
/**
* @ Return returns the current time. format:
*/
Public static String getTime (){
Return getTime ("yyyy-MM-dd HH: mm: ss ");
}
Public static Date getDate (String parrten ){
If (! StringUtils. hasText (parrten )){
Parrten = "yyyyMMddHH ";
}
String starttime = DateUtils. getTime (parrten );
Return DateUtils. getStrToDate (starttime, parrten );
}
Public static Date getDate (){
Return getDate ("yyyy-MM-dd HH: mm: ss ");
}
/**
* Obtain the current time
* @ Param parrten the Output Time Format
* @ Return time
*/
Public static String getTime (String parrten ){
String timestr;
If (parrten = null | parrten. equals ("")){
Parrten = "yyyyMMddHHmmss ";
}
Java. text. SimpleDateFormat sdf = new SimpleDateFormat (parrten );
Java. util. Date cday = new Date ();
Timestr = sdf. format (cday );
Return timestr;
}
/**
* Time Format Conversion
* @ Param cday
* @ Param parrten
* @ Return
*/
Public static String getTime (Date cday, String parrten ){
String timestr;
If (parrten = null | parrten. equals ("")){
Parrten = "yyyyMMddHHmmss ";
}
Java. text. SimpleDateFormat sdf = new SimpleDateFormat (parrten );
Timestr = sdf. format (cday );
Return timestr;
}
/**
* Get the time of yesterday
* @ Return
*/
Public static String getYestday (){
String timestr;
Java. util. Calendar cc = ACC;
Cc. roll (cc. DAY_OF_YEAR,-1 );
Java. text. SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
Timestr = sdf. format (cc. getTime ());
Return timestr;
}
// Convert a time string to the date type
Public static Date getStrToDate (String time, String parrten ){
SimpleDateFormat formatter = new SimpleDateFormat (parrten );
ParsePosition pos = new ParsePosition (0 );
Date dt1 = formatter. parse (time, pos );
Return dt1;
}
/**
* Convert the time to the format of xxxx Year xx month xx day
* @ Param t1 original time
* @ Param parrten original time format
* @ Return
*/
Public static String getTime (String t1, String parrten ){
SimpleDateFormat formatter = new SimpleDateFormat (parrten );
SimpleDateFormat formatter2 = new SimpleDateFormat ("MM dd, yyyy ");
ParsePosition pos = new ParsePosition (0 );
Date dt1 = formatter. parse (t1, pos );
Return formatter2.format (dt1 );
}
/**
* Convert time to parrten2 format
* @ Param t1
* @ Param parrten
* @ Param parrten2
* @ Return
*/
Public static String getTime (String t1, String parrten, String parrten2 ){
SimpleDateFormat formatter = new SimpleDateFormat (parrten );
SimpleDateFormat formatter2 = new SimpleDateFormat (parrten2 );
ParsePosition pos = new ParsePosition (0 );
Date dt1 = formatter. parse (t1, pos );
Return formatter2.format (dt1 );
}
/**
* Compare the time of two strings
* @ Param t1 time 1
* @ Param t2 time 2
* @ Param parrten: yyyy-MM-dd
* @ Return returns long = 0 equal,> 0 t1> t2, <0 t1 <t2
*/
Public static long compareStringTime (String t1, String t2, String parrten ){
SimpleDateFormat formatter = new SimpleDateFormat (parrten );
ParsePosition pos = new ParsePosition (0 );
ParsePosition pos1 = new ParsePosition (0 );
Date dt1 = formatter. parse (t1, pos );
Date dt2 = formatter. parse (t2, pos1 );
Long l = dt1.getTime ()-dt2.getTime ();
Return l;
}
// Days of difference
Public static int compareTime (String time1, String time2 ){
Long l = compareStringTime (time1, time2, "yyyy-MM-dd ");
Long days = l/1000/60/60/24;
Return (int) days;
}
Public static synchronized String getDateDDMMYYYY (Date date ){
Return ddMMyyyyFormat. format (date );
}
Public static synchronized String getDateindex (Date date ){
Return yyyymmddhhmmssFormat. format (date );
}
Public static synchronized String getDateSearch (Date date ){
Return mmddhhmmFormat. format (date );
}
Public static synchronized String getDateNoSencond (Date date ){
Return yyyymmddhhmmFormat. format (date );
}
Public static synchronized String formatDate (Date date ){
Return dateFormat. format (date );
}
Public static synchronized String formatDateTime (Date date ){
Return datetimeFormat. format (date );
}
Public static Timestamp getCurrentGMTTimestamp (){
Return new Timestamp (System. currentTimeMillis ()-SERVER_TIME_OFFSET );
}
Public static void updateCurrentGMTTimestamp (Timestamp timeToUpdate ){