Java String Date processing tool class

Source: Internet
Author: User
Tags dateformat difference between two times string format
Package com.newer;


Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Import java.util.List;




/**
* * 1. String Common operations <br>
* 2. Date Time Operation <br>
* Provides static methods that cannot be instantiated.
* @author RSun
* @date 2012-2-26 03:58:06
*/
@SuppressWarnings ("Rawtypes")
public class Strutil {



/** prohibit instantiation of **/
Private Strutil () {
}


/**
* Check whether the string is null or empty <br>
* is null string or returns true for NULL, otherwise returns false
*/
public static Boolean isnullorempty (String str) {
Boolean result = true;
if (str!= null &&! "". Equals (Str.trim ())) {
result = false;
}
return result;
}


/**
* To determine whether the array is null or size 0 <br>
* Returns TRUE if NULL or size is 0 o'clock, otherwise returns false
* @param obj
* @return
*/
public static Boolean isarraynull (object[] obj) {
if (obj = null | | obj.length <= 0) {
return true;
}
return false;
}


/**
* Determine whether the list is null or size 0 <br>
* Returns TRUE if NULL or size is 0 o'clock, otherwise returns false
* @return
*/
public static Boolean islistnull (List list) {
if (list = = NULL | | list.size () <= 0) {
return true;
}
return false;
}


/**
* Check whether the string is null or "null" <br>
* is null or "null", returns "", otherwise returns a string <br>
* For processing database query data
*/
public static String getcleanstring (Object obj) {
if (obj = = null) {
Return "";
}else if (string.valueof (obj). Equals ("null")) {
Return "";
}else{
return string.valueof (obj);
}
}


/**
* Check whether the shaping data is null<br>
* is NULL, returns 0, otherwise returns the original value <br>
* For processing database query data
*/
public static int Getcleaninteger (Object obj) {
if (obj = = null) {
return 0;
}else{
Return Integer.parseint ((String) obj);
}
}


/**
* Check whether float data is null<br>
* is NULL, returns 0, otherwise returns the original value <br>
* For processing database query data
*/
public static float getcleanfloat (Object obj) {
if (obj = = null) {
return 0f;
}else{
Return Float.parsefloat ((String) obj);
}
}



/**
* Array conversion to string <br>
* Conversion failed or array is null, return ""
* @param strarray the array to convert
* @param split split break
* @return
*/
public static string Strarraybystr (object[] strarray, String split) {
Return Stringutils.join (Strarray, split);
StringBuilder ss = new StringBuilder ();
for (int i=0, Len = (Strarray = null? 0:strarray.length); i<len; i++) {
if (i > 0) ss.append (split);
Ss.append (Strarray[i]);
}
return ss.tostring ();
}


/**
* Two string values that are NOT NULL are null and return ""
* @param str1
* @param str2
* @return Priority return to STR1
*/
public static string Emailoralias (String str1, String str2) {
String str = getcleanstring (STR1);
if (null = = STR1 | | "". Equals (str1)) {
str = getcleanstring (STR2);
}
return str;
}



================================ Date Common methods =============================


/** gets the current timestamp **/
public static String Gettimestamp () {
String TimeStamp = new SimpleDateFormat ("Yyyymmddhhmmsssss"). Format (new Date ());
return timeStamp;
}


/** gets the first day of the Week (YYYY-MM-DD) **/
public static String Getmonofweek () {
Calendar C = new GregorianCalendar ();
C.setfirstdayofweek (Calendar.monday);
C.settime (New Date ());
C.set (Calendar.day_of_week, C.getfirstdayofweek ());

Calendar C = calendar.getinstance ();
C.set (Calendar.day_of_week, C.getactualminimum (Calendar.day_of_week));

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
String str = Sdf.format (C.gettime ());
return str;
}


/** gets the last day of the Week (YYYY-MM-DD) **/
public static String Getsunofweek () {
Calendar C = new GregorianCalendar ();
C.setfirstdayofweek (Calendar.monday);
C.settime (New Date ());
C.set (Calendar.day_of_week, C.getfirstdayofweek () + 6);

Calendar C = calendar.getinstance ();
C.set (Calendar.day_of_week, C.getactualmaximum (Calendar.day_of_week));

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
String str = Sdf.format (C.gettime ());
return str;
}


/** gets the first day of the month (YYYY-MM-DD) **/
public static String Getfirstdayofmonth () {
Calendar cal = Calendar.getinstance ();
Cal.set (Calendar.day_of_month, Cal.getactualminimum (Calendar.day_of_month));
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
String Str=sdf.format (Cal.gettime ());
return str;
}


/** gets the last day of the month (YYYY-MM-DD) **/
public static String Getlastdayofmonth () {
Calendar cal = Calendar.getinstance ();
Cal.set (Calendar.day_of_month, Cal.getactualmaximum (Calendar.day_of_month));
SimpleDateFormat sdfs=new SimpleDateFormat ("Yyyy-mm-dd");
String Str=sdfs.format (Cal.gettime ());
return str;
}


/**
* According to time to get the difference between days, hours of new time
* @param Date Reference time
* @param type Day or hour [d/h] (case sensitive)
* @param num Difference, for example: 2 after 2 days or hours,-2 means 2 days or hours before
* @return
*/
public static date Getnextday (date date, char type, int num) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Switch (type) {
Case ' d ':
Calendar.add (Calendar.day_of_month, num);
Break
Case ' H ':
Calendar.add (Calendar.hour_of_day, num);
Break
Default
Break
}
Date = Calendar.gettime ();
return date;
}


/**
* Compare the difference between two times (D1 and D2)
* @param d1 time A
* @param D2
* @param type "h/m/s" (case sensitive)
* @return D1-d2, indicating that D1 is earlier or later than D2 xxx
*/
public static long comparedate (date D1, date D2, char type) {
Long num = D1.gettime ()-d2.gettime ();
num/=1000;
if (' m ' = = Type) {
num/=60;
}else if (' h ' = = type) {
num/=3600;
}
return num;
}


/**
* Format time, time-conversion string
* @param date NULL is the current system time
* @param format, NULL defaults to: ' Yyyy-mm-dd HH:mm:ss '
* @return Date in string format
*/
public static string Getdatetimebystr (date date, String format) {
if (date = null)
Date = new Date ();
if (format = = null)
Format = "Yyyy-mm-dd HH:mm:ss";
return new SimpleDateFormat (format). format (date);
}


/**
* Format time, string turn time
* @param datastr the strings to be converted
* @param format, NULL defaults to: ' Yyyy-mm-dd HH:mm:ss '
* Date of @return conversion
*/
public static Date Getstrbydatatime (string datastr, string format) {
if (datastr = null)
return new Date ();
if (format = = null)
Format = "Yyyy-mm-dd HH:mm:ss";
DateFormat SDF = new SimpleDateFormat (format);
Date date = null;
try {
Date = Sdf.parse (DATASTR);
catch (Exception e) {
E.printstacktrace ();
}
return date;
}




================================ Other common methods =============================

/**
* Randomly extract count numbers from number values
* @param count the number of values to extract
* @param number is in the range of 0
* @return
*/
public static int[] Randomnumber (int count, int number) {
int K=count, N=number;
int[] numbers = new Int[n];
for (int i = 0; i < numbers.length; i++)
Numbers[i] = i + 1;
Int[] result = new Int[k];
for (int i = 0; i < result.length; i++) {
int r = (int) (Math.random () * n);
Result[i] = Numbers[r];
NUMBERS[R] = numbers[n-1];
n--;
}

Arrays.sort (result);
for (int r:result)
System.out.println (R);
return result;

}




}

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.