Datemedils time plug-in and datemedils plug-in

Source: Internet
Author: User
Tags gmp

Datemedils time plug-in and datemedils plug-in

Import java. text. DateFormat;
Import java. text. ParsePosition;
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import java. util. Date;

/**
*
* @ Author ljb
* @ Version 1.0
*/
Public class datemedils {
/**
* International Standards
* A week starts from Sunday.
*/
Public static int WEEK_OF_ISO = 0;
/**
* China Standard
* One week starts from Monday.
*/
Public static int WEEK_OF_GBK = 1;
Private int local_GMP = DateTools. WEEK_OF_GBK;
/**
* The default constructor is from Monday to Sunday.
*/
Public datatels (){

}
/**
* Constructor
* @ Param GMP standard type WEEK_OF_GBK China Standard WEEK_OF_ISO International Standard
*/
Public datatels (int GMP ){
This. local_GMP = GMP;
}
/**
* Get the current time and return it as a String type.
* @ Return
*
* @ Return returns the time format yyyyMMddHHmmss.
*/
Public String getDate (){
Return getDate ("yyyyMMddHHmmss ");
}


/**
* Obtain the current time. Time Format formatted Based on fmt
*
* @ Param fmt
* Date Format
* @ Return: The returned time format is generated based on the fmt Format.
*/
Public String getDate (String fmt ){
Date myDate = new Date (System. currentTimeMillis ());
SimpleDateFormat sDateformat = new SimpleDateFormat (fmt );
Return sDateformat. format (myDate). toString ();
}






Private Calendar getCal (String strdate, String fmt ){
Calendar cal = null;
Try {
If (strdate! = Null) & (fmt! = Null )){

SimpleDateFormat nowDate = new SimpleDateFormat (fmt );
Date d = nowDate. parse (strdate, new ParsePosition (0 ));
If (d! = Null ){
Cal = Calendar. getInstance ();
Cal. clear ();
Cal. setTime (d );
}
}
} Catch (Exception e ){
E. printStackTrace ();
Return null;
}
Return cal;
}

/**
* Calculate the week number of the current year.
*
* @ Param strdate
* Calculation date
* @ Param fmt
* Date Format
* @ Return strdate indicates the number of weeks in the year. If the strdate format is yyyy, the default value is January 1, January 1.
*/
Public int getWeekOfYear (String strdate, String fmt ){
Int ret =-1;
Try {
If (strdate! = Null) & (fmt! = Null )){

Calendar cal = getCal (strdate, fmt );
If (cal! = Null ){

Ret = cal. get (Calendar. WEEK_OF_YEAR );
If (this. local_GMP = datatels. WEEK_OF_GBK & cal. get (Calendar. DAY_OF_WEEK) = Calendar. SUNDAY ){
Ret-= 1;
}
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return ret;
}

/**
* Calculate all the dates of the week of the given date.
*
* @ Param strdate
* Specified Date
* @ Param oldfmt
* Custom Date Format
* @ Param newfmt
* Output format
* @ Return returns the date of the week in the form of an array. If the format of the specified date is yyyy or yyyyMM, the date of the first week of the current year or the first week of the current month is calculated.
*/
Public String [] getWeekDay (String strdate, String oldfmt, String newfmt ){
String [] weekday = new String [7];
Try {
If (strdate! = Null) & (oldfmt! = Null) & (newfmt! = Null )){
Calendar cal = getCal (strdate, oldfmt );
If (cal! = Null ){
Int dayOfWeek = cal. get (Calendar. DAY_OF_WEEK );
DayOfWeek =-dayOfWeek + 1 + this. local_GMP;
If (dayOfWeek> 0 ){
DayOfWeek =-6;
}
Cal. add (Calendar. DATE, dayOfWeek );
SimpleDateFormat sdf = new SimpleDateFormat (newfmt );
Weekday [0] = sdf. format (cal. getTime ());
For (int I = 1; I <7; I ++ ){
Cal. add (5, 1 );
Weekday [I] = sdf. format (cal. getTime ());
}
}
}
} Catch (IndexOutOfBoundsException iworkflow ){
Iworkflow. printStackTrace ();
}
Return weekday;
}

/**
* Calculate all dates in a given week
*
* @ Param year
* Year
* @ Param week
* Specify the week * @ param newfmt * return value format * @ return result array to output the date of the week. */Public String [] getWeekDate (String year, int week, String newfmt) {String [] jweekday = new String [7]; try {if (year! = Null) & (year. length () = 4) & (week> 0) & (newfmt! = Null) {Calendar cal = getCal (year + "0101", "yyyyMMdd"); if (cal! = Null) {week --; cal. add (5, week * 7-cal. get (7) + 2); SimpleDateFormat sdf = new SimpleDateFormat (newfmt); jweekday [0] = sdf. format (cal. getTime (); for (int I = 1; I <7; I ++) {cal. add (5, 1); jweekday [I] = sdf. format (cal. getTime () ;}}} catch (IndexOutOfBoundsException iworkflow) {iworkflow. printStackTrace ();} return jweekday;}/*** calculate the specified date of the week ** @ param strdate * specified date * @ param oldfmt * Specified Date Format * @ Param fmt * output format * @ return returns the fmt + result. */Public String getDayOfWeek (String strdate, String oldfmt, String fmt) {String sWeek = null; try {if (strdate! = Null) & (oldfmt! = Null) & (fmt! = Null) {Calendar cal = getCal (strdate, oldfmt); if (cal! = Null) {int iWeek = cal. get (7); sWeek = fmt + (iWeek-1 = 0? 7: iWeek-1) ;}} catch (Exception e) {e. printStackTrace ();} return sWeek ;} /*** calculate the total number of weeks in a specified year ** @ param year * specify the year format yyyy * @ return returns the number of weeks */public int getWeekNum (String year) {int weeknum =-1; try {if (year! = Null) {Calendar cal = getCal (year + "1231", "yyyyMMdd"); if (cal! = Null) {if (cal. get (3) = 1) cal. add (5,-7); weeknum = cal. get (3) ;}} catch (Exception e) {e. printStackTrace ();} return weeknum ;} /*** calculate the difference between two given time ** @ param startdate * start date * @ param enddate * end date * @ param fmt * Date Format * @ param refmt * return value format ms s m minutes h d days * @ return value */public String cntTimeDifference (String startdate, string enddate, String fmt, String refmt) {String ret = null; tr Y {if (startdate! = Null) & (enddate! = Null) & (fmt! = Null) & (refmt! = Null) {Date scal = getCal (startdate, fmt ). getTime (); Date ecal = getCal (enddate, fmt ). getTime (); if (scal = null) | (ecal = null) {return null;} else {long diffMillis = ecal. getTime ()-scal. getTime (); long diffSecs = diffMillis/1000L; long diffMins = diffMillis/60000L; long diffHours = diffMillis/3600000L; long diffDays = diffMillis/86400000L; if (refmt. equals ("ms") ret = Long. ToString (diffMillis); else if (refmt. equals ("s") ret = Long. toString (diffSecs); else if (refmt. equals ("m") ret = Long. toString (diffMins); else if (refmt. equals ("h") ret = Long. toString (diffHours); else if (refmt. equals ("d") ret = Long. toString (diffDays); else ret = Long. toString (diffHours) ;}} catch (Exception e) {e. printStackTrace ();} return ret;}/*** calculate the date after the specified date in minutes ** @ param deft Ime * Custom date * @ param oldfmt * Date Format * @ param timediff * minute * @ param newfmt * return value format * @ return date, timediff> 0 forward calculation, timediff <0 calculate backward */public String getBeforeTime (String deftime, String oldfmt, int timediff, String newfmt) {String rq = null; try {if (deftime! = Null )&&(! Deftime. equals ("") {Calendar cal = getCal (deftime, oldfmt); if (cal! = Null) {cal. add (Calendar. MINUTE,-timediff); SimpleDateFormat sdf = new SimpleDateFormat (newfmt); rq = sdf. format (cal. getTime () ;}} catch (Exception e) {e. printStackTrace ();} return rq ;} /*** calculate the date after which the specified date expires ** @ param deftime * Custom date * @ param oldfmt * Date Format * @ param timediff * hour * @ param newfmt * return value format * @ return date, timediff> 0 forward calculation, timediff <0 Backward calculation */public String getBeforeTimeByH (St Ring deftime, String oldfmt, int timediff, String newfmt) {String rq = null; try {if (deftime! = Null )&&(! Deftime. equals ("") {Calendar cal = getCal (deftime, oldfmt); if (cal! = Null) {cal. add (12,-timediff * 60); SimpleDateFormat sdf = new SimpleDateFormat (newfmt); rq = sdf. format (cal. getTime () ;}} catch (Exception e) {e. printStackTrace ();} return rq ;} /*** calculate the date of the specified date after the specified month ** @ param deftime * Custom date * @ param oldfmt * Date Format * @ param timediff * month * @ param newfmt * return value format * @ return date, timediff> 0 forward calculation, timediff <0 Backward calculation */public String getBeforeTimeByM (String defti Me, String oldfmt, int timediff, String newfmt) {String rq = null; try {if (deftime! = Null )&&(! Deftime. equals ("") {Calendar cal = getCal (deftime, oldfmt); if (cal! = Null) {cal. add (2,-timediff); SimpleDateFormat sdf = new SimpleDateFormat (newfmt); rq = sdf. format (cal. getTime () ;}} catch (Exception e) {e. printStackTrace ();} return rq ;} /*** format the date ** @ param mydate * date * @ param oldfmt * old format * @ param newfmt * New format * @ return format returns the result based on newfmt's root format */public string fmtDate (String mydate, string oldfmt, String newfmt) {String restr = null; try {if (mydate! = Null) & (oldfmt! = Null) & (newfmt! = Null) {SimpleDateFormat newDate = new SimpleDateFormat (newfmt); Calendar cal = getCal (mydate, oldfmt); if (cal! = Null) {restr = newDate. format (cal. getTime () ;}} catch (Exception e) {e. printStackTrace ();} return restr ;} /*** convert the date format of the text line into the Date type * @ param mydate * @ param fmt * @ return */public Date fmtDate (String myDate, String fmt) {Date newDate = null; if (myDate! = Null) & (fmt! = Null) {Calendar cal = getCal (myDate, fmt); newDate = cal. getTime () ;}return newDate;} public String fmtDate (Date myDate, String fmt) {String newDate = ""; SimpleDateFormat sDateformat = new SimpleDateFormat (fmt); newDate = sDateformat. format (myDate ). toString (); return newDate;} public String fmtDate (Long myDate, String fmt) {Calendar calendar = Calendar ar. getInstance (); calendar. setTimeInMillis (myDate ); DateFormat formatter = new SimpleDateFormat (fmt); return formatter. format (calendar. getTime ();}/* public static void main (String [] args) {datemedils dt = new datemedils (); System. out. print (dt. fmtDate (1462878420000l, "yyyy-MM-dd HH: mm: ss "));} * // *** calculate the number of days in a month of a year * @ param year * @ return */public int getMonthDays (int year, int month) {if (month> 12 | month <1) {throw new RuntimeException ("month is error "+ Month);} String bigmonth =", 12 "; String smallmonth =", "; if (bigmonth. indexOf (month + "")>-1) {return 31;} if (smallmonth. indexOf (month + "")>-1) {return 30;} if (year % 400 = 0 | (year % 4 = 0 & year % 100! = 0) {return 29;} else {return 28 ;}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.