a useful java utility class

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 GetCurrentDatetimeString() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
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 block
e.printStackTrace();
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.getInstance();
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());
}
//判断date 格式是否符合 yyyy-MM-dd HH:mm:ss
if (date.indexOf(" ") == -1) {
if(isEnd){
date += " 23:59:59";
}else{
date += " 00:00:00";
}
}else{//判断 若date 为 yyyy-MM-dd 00:00:00
if(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 format.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();
}
}

你可能感兴趣的:(Core,JAVA)