Time and String Conversion, date/string

Source: Internet
Author: User

Package com. neweb. botcashier. util;

Import java. Math. bigdecimal;
Import java. Text. dateformat;
Import java. Text. simpledateformat;
Import java. util. calendar;
Import java. util. date;

Import org. Apache. log4j. Logger;

Import com. neweb. botcashier. Base. Exception. appexception;

Public class dateutils {

Private Static logger = logger. getlogger (dateutils. Class );

Public static final string date_pattern_default = "yyyymmdd ";
Public static final string time_pattern_default = "hhmmss ";

/**
* String duration
* @ Param STR time string
* @ Param FMT string format
* @ Return date
*/
Public static date stringtodate (string STR, string FMT ){
If (null = STR | "". Equals (STR ))
Return NULL;

Java. Text. simpledateformat SDF = new java. Text. simpledateformat (FMT );
Try {
Return SDF. parse (STR );
} Catch (Java. Text. parseexception PE ){
Logger. Error (PE );
}
Return NULL;
}

/**
* String expiration date, which is in the format of yyyymmdd.
* @ Param STR date string
* @ Return date
*/
Public static date stringtodate (string Str ){
If (null = STR | "". Equals (STR ))
Return NULL;
Returnstringtodate (STR, date_pattern_default );
}

/**
* Date string
* @ Param date
* @ Param FMT string format
* @ Return string
*/
Public static string datetostring (date, string FMT ){
If (null = Date)
Return "";

Java. Text. simpledateformat SDF = new java. Text. simpledateformat (FMT );
Try {
Return new string (SDF. Format (date, new stringbuffer (), new java. Text. fieldposition (0 )));
} Catch (nullpointerexception ne ){
Logger. Error (NE );
}
Return NULL;
}

/**
* Date delimiter string, which is in the format of yyyymmdd.
* @ Param date
* @ Return string
*/
Public static string datetostring (date ){
If (null = Date)
Return "";
Returndatetostring (date, date_pattern_default );
}

/**
* Number of date records
* @ Param date
* @ Return
*/
Public static int datetoint (date ){
Try {
Return integer. parseint (dateutils. datetostring (date ));
} Catch (exception e ){
Logger. Error (E. getmessage (), e );
Return 0;
}
}

/**
* Data Date time period Date time
* @ Param date
* @ Param time
* @ Return
*/
Public static date inttodate (INT date, int time ){
String fulldatetime = intdatetostr (date) + inttimetostr (time );
Return stringtodate (fulldatetime, "yyyymmddhhmmss ");
}

/**
* Time period string, which is in the format of hhmmss.
* @ Param date time
* @ Return string
*/
Public static string timetostring (date ){
If (null = Date)
Return "";
Return datetostring (date, time_pattern_default );
}
/**
* Time period string, which is in the format of hhmmss.
* @ Param date time
* @ Return string
*/
Public static string timetostring (date, string FMT ){
If (null = Date)
Return "";
Return datetostring (date, FMT );
}
/**
* Time limit data
* @ Param date
* @ Return
*/
Public static int timetoint (date ){
Try {
Return integer. parseint (dateutils. timetostring (date ));
} Catch (exception e ){
Logger. Error (E. getmessage (), e );
Return 0;
}
}

/**
* Data Date string
* @ Param date
* @ Param FMT
* @ Return
*/
Public static string intdatetostr (INT date, string FMT ){
Return datetostring (stringtodate (string. Format ("% 08d", date), FMT );
}

/**
* Data Date string, in the specified format: yyyymmdd
* @ Param date
* @ Return
*/
Public static string intdatetostr (INT date ){
Return intdatetostr (date, date_pattern_default );
}

/**
* Data time period string
* @ Param time
* @ Param FMT
* @ Return
*/
Public static string inttimetostr (INT time, string FMT ){
Return datetostring (stringtodate (string. Format ("% 06d", time), time_pattern_default), FMT );
}

/**
* Data time duration string, which is in the format of hhmmss
* @ Param time
* @ Return
*/
Public static string inttimetostr (INT time ){
Return inttimetostr (time, time_pattern_default );
}


/**
* Get the system time
* @ Return date
*/
Public static date getsysdate (){
Return calendar. getinstance (). gettime ();
}

/**
* Get yesterday
* @ Return
*/
Public static date getyesterday (){
Calendar Cal = calendar. getinstance ();
Cal. Add (calendar. Date,-1 );
Return Cal. gettime ();
}

/**
* Get tomorrow (the next day)
* @ Return
*/
Public static date gettomorrow (){
Calendar Cal = calendar. getinstance ();
Cal. Add (calendar. Date, + 1 );
Return Cal. gettime ();
}

/**
* Add date month
* @ Param DT time
* @ Param amount add month
* @ Return date
*/
Public static date addmonth (date DT, int amount ){
Calendar Cal = calendar. getinstance ();
Cal. settime (DT );
Cal. Add (calendar. Month, amount );
Return Cal. gettime ();
}

/**
* Increase the number of days
* @ Param DT time
* @ Param amount: Increase the number of days
* @ Return date
*/
Public static date adddate (date DT, int amount ){
Calendar Cal = calendar. getinstance ();
Cal. settime (DT );
Cal. Add (calendar. day_of_month, amount );
Return Cal. gettime ();
}

/**
* Increase the score.
* @ Param DT time
* @ Param amount: Increase the score.
* @ Return
*/
Public static date addminute (date DT, int amount ){
Calendar Cal = calendar. getinstance ();
Cal. settime (DT );
Cal. Add (calendar. Minute, amount );
Return Cal. gettime ();
}

/**
* Computing time gap
* @ Param sdate Start Time
* @ Param edate End Time
* @ Return
*/
Public static long totalsec (date sdate, date edate ){
Long stime = sdate. gettime ();
Long etime = edate. gettime ();
Long Total = etime-stime;
Return total;
}

/**
* Check the validity period of your credit card.
* [V1> V2, with a return value greater than 0],
* [V1 = V2, return to limit 0],
* [V1 <V2, with a return value smaller than 0]
* @ Param V1
* @ Param v2
* @ Return
*/
Public static int comparevaildyearmonth (string V1, string V2 ){
String v1ym = v1.substring (2) + v1.substring (0, 2 );
String v2ym = v2.substring (2) + v2.substring (0, 2 );
Return v1ym. compareto (v2ym );
}

/**
* When your credit card has expired, mmyy has been converted to yyyymm.
* @ Param mmyy
* @ Return yyymm
*/
Public static string convertcardtvalidyearmonth (string mmyy ){
Try {
// Data
Integer. parseint (mmyy );
// Updated by year
String sysyear = dateutils. datetostring (getsysdate (), "YYYY ");
String result = sysyear. substring (0, 2)
+ Mmyy. substring (2)
+ Mmyy. substring (0, 2 );
Return result;
} Catch (exception e ){
Logger.info (E. getmessage (), e );
Return NULL;
}

}

/**
* Year of the western yuan string: Year of the Democratic Republic of China
* @ Param datestr
* @ Return
*/
Public static string datestrtoroc (string datestr ){
Try {
Date = dateutils. stringtodate (datestr );
Int dateint = dateutils. datetoint (date );
Return string. valueof (dateint-19110000 );
} Catch (exception e ){
Logger.info (E. getmessage (), e );
Return NULL;
}

}

/**
* The year string of the nation: the year string of the West dollar
* @ Param datestr
* @ Return
*/
Public static string roctodatestr (string rocstr ){
Try {
Int dateint = integer. parseint (rocstr) + 19110000;
Return string. valueof (dateint );
} Catch (exception e ){
Logger.info (E. getmessage (), e );
Return NULL;
}

}

/**
* Estimated remaining time
* @ Param limitdate End Time
* @ Param currentdate: The next time
* @ Return long [] Return callback [index: 0] Minutes, [index: 1] seconds
*/
Public static long [] getverifyremaintime (date limitdate, date currentdate ){
Long [] result = new long [] {0l, 0l };
Long totalmsec = limitdate. gettime ()-currentdate. gettime ();
Long totalsec = (New bigdecimal (totalmsec). Divide (New bigdecimal (1000), 0, bigdecimal. round_half_up). longvalue ();
System. Out. println (totalsec );
Result [0] = totalsec/60l;
Result [1] = totalsec % 60l;
Return result;
}

/**
* Date of Birth: The × day of the year
* @ Param currentdate
* @ Return
*/
Public static int getcalandaday (date currentdate ){
Calendar Cal = calendar. getinstance ();
Cal. settime (currentdate );
Return Cal. Get (calendar. day_of_year );
}

/**
* Obtain the string date of the west dollar of the system, EX: 20120101
*
* @ Param sign
* @ Return
*/
Public static string getsystemdate (){
Date = new date ();
Dateformat format = new simpledateformat ("YYYY" + "mm" + "DD"); // + "hh" + "mm" + "SS"
Return format. Format (date );
}
/**
* Time used to obtain the string of the West dollar of the system, EX: 171020
*
* @ Param sign
* @ Return
*/
Public static string getsystemtime (){
Calendar Cal = calendar. getinstance ();
String hours = "0" + String. valueof (Cal. Get (calendar. hour_of_day ));
String minute = "0" + String. valueof (Cal. Get (calendar. Minute ));
String second = "0" + String. valueof (Cal. Get (calendar. Second ));
String required IME = hours. substring (hours. length ()-2) + minute. substring (Minute. length ()-2) + second. substring (second. length ()-2 );
Return required IME;
}
Public static void main (string [] ARGs ){
Try {
Date senddate = dateutils. inttodate (20121101,172 440 );
Date afert5mdate = dateutils. addminute (senddate, 5 );
System. Out. println (afert5mdate );
Date sysdate = dateutils. getsysdate ();
System. Out. println (sysdate );
If (sysdate. Before (afert5mdate )){
Long [] times = dateutils. getverifyremaintime (afert5mdate, sysdate );
Throw appexception. getvalidateerr ("idx1023", new string [] {string. valueof (Times [0]), String. valueof (Times [1])});
}
} Catch (exception e ){
System. Out. println (E. getmessage ());
}

}
}

Time and String Conversion, date/string

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.