A useful java utility class

Source: Internet
Author: User

Import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. unsupportedEncodingException; import java.net. inet4Address; import java.net. inetAddress; import java. text. parseException; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; import org. apache. commons. lang. stringUtils; public class CommonFun {public static String GetC UrrentDatetimeString () {SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date curDate = new Date (System. currentTimeMillis (); // get the current time return formatter. format (curDate);} public static String convertStringEncoding (String str) {try {return new String (str. getBytes ("GBK"), "iso-8859-1");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke. printStackTrac E (); return str ;}} public static String DateToStr (Date date) {if (date = null) {return "";} simpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd"); return format. format (date);} public static String DateTimeToStr (Date date) {if (date = null) {return "";} simpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); return format. format (date);} public static Date StrToDate (String date ){ If (date = null) {return new Date (System. currentTimeMillis ();} SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); try {return format. parse (date);} catch (ParseException e) {} return new Date (System. currentTimeMillis ();} public static Calendar strToCalendar (String date) {Calendar happenTime = null; if (StringUtils. isNotBlank (date) {date. replace ("T", ""); happenTime = Calendar. g EtInstance (); happenTime. setTime (StrToDate (date);} return happenTime;} public static Calendar strToCalendar (String date, boolean isEnd) {Calendar happenTime = null; if (StringUtil. isNotNullOrNotEmpty (date) {date = date. replaceAll ("T", ""); happenTime = Calendar. getInstance (); happenTime. setTime (StrToDate (date, isEnd);} return happenTime;} public static Date StrToDate (String date, boolean isEnd) {if (Date = null) {return new Date (System. currentTimeMillis ();} // determines whether the date format complies with yyyy-MM-dd HH: mm: ssif (date. indexOf ("") =-1) {if (isEnd) {date + = "23:59:59" ;}else {date + = "00:00:00 ";}} else {// judge if date is yyyy-MM-dd 00: 00: 00if (isEnd) {int nIndex = date. indexOf (""); date = date. substring (0, nIndex); date + = "23:59:59" ;}} SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); try {return f Ormat. parse (date);} catch (ParseException e) {e. printStackTrace ();} return new Date (System. currentTimeMillis ();} public static boolean between (final int base, final int begin, final int end) {return base >=begin & base <= end ;} public static String fetchLocIp () {String localAddr = "127.0.0.1"; InetAddress [] localIpArray = null; try {localIpArray = InetAddress. getAllByName (InetAddress. getLocalHost (). GetHostName ();} catch (Exception e) {} if (localIpArray! = Null & localIpArray. length> 0) {for (InetAddress localIp: localIpArray) {if (localIp instanceof Inet4Address) {localAddr = localIpArray [0]. getHostAddress (); break ;}}return localAddr;} public static String inputStream2String (InputStream is) throws IOException {ByteArrayOutputStream baos = new ByteArrayOutputStream (); int I =-1; while (I = is. read ())! =-1) {baos. write (I);} return baos. toString ();

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.