package com;
|
|
import java.lang.management.ManagementFactory;
|
import java.lang.management.RuntimeMXBean;
|
import java.net.Inet4Address;
|
import java.net.InetAddress;
|
import java.net.NetworkInterface;
|
import java.text.DateFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.Enumeration;
|
import java.util.Vector;
|
|
import javax.swing.JComboBox;
|
import javax.swing.JTextField;
|
|
|
public class Com {
|
final public static int UploadData_ClientType_BS_CLI = 0;
|
final public static int UploadData_ClientType_CS_CLI = 1;
|
final public static int UploadData_ClientType_CS_SVR = 2;
|
|
public static final int EDIT_VIEW = 0;
|
public static final int EDIT_ADD = 1;
|
public static final int EDIT_MODIFY = 2;
|
|
public static final int CNT_MAX = 99999999;
|
|
public static final String DTF_YMDhms_S = "yyyy-MM-dd HH:mm:ss.SSS";
|
public static final String DTF_YMDhms = "yyyy-MM-dd HH:mm:ss";
|
public static final String DTF_YMDhm = "yyyy-MM-dd HH:mm";
|
public static final String DTF_YMDh = "yyyy-MM-dd HH";
|
public static final String DTF_YMD = "yyyy-MM-dd";
|
public static final String DTF_hms = "HH:mm:ss";
|
public static final long DT_OFFSET = 3600*8*1000;
|
|
public static final String[] YesNoText = new String[] {
|
"·ñ",
|
"ÊÇ",
|
};
|
//final public static DateFormat DateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
/**
|
* ½«´«ÈëµÄʱ¼äת»»³ÉÖ¸¶¨µÄµÄʱ¼ä¸ñʽ²¢·µ»ØÊ±¼ä¸ñʽ»¯Ö®ºóµÄ×Ö·û´®
|
* @param dt ÐèҪת»»µÄʱ¼ä
|
* @param format ¸ñʽ»¯×Ö·û´®
|
* @return
|
*/
|
public static String getDateTimeFormat(Date dt, String format){
|
DateFormat dtf = new SimpleDateFormat(format);
|
return dtf.format(dt);
|
}
|
|
/**
|
* ½«´«ÈëµÄ×Ö·û´®°´Ö¸¶¨µÄ¸ñʽ½âÎö³Éʱ¼äÀàÐͲ¢·µ»Ø
|
* @param dt ÐèÒª½âÎöµÄ×Ö·û´®
|
* @param format Ö¸¶¨µÄʱ¼ä¸ñʽ
|
* @return
|
*/
|
public static Date getDateTimeFromStr(String dt, String format){
|
DateFormat dtf = new SimpleDateFormat(format);
|
Date date = new Date();
|
try {
|
date = dtf.parse(dt);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
|
return date;
|
}
|
|
public static String getNowTimeWithAt() {
|
return " @ " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms);
|
}
|
|
|
public static String get_DTF(Date dt, String format){
|
DateFormat dtf = new SimpleDateFormat(format);
|
return dtf.format(dt);
|
}
|
|
public static Date get_DT_FromStr(String dt, String format){
|
DateFormat dtf = new SimpleDateFormat(format);
|
Date date = new Date();
|
try {
|
date = dtf.parse(dt);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
|
return date;
|
}
|
|
/**
|
* ÅжÏnumber²ÎÊýÊÇ·ñÊÇÕûÐÍÊý±íʾ·½Ê½
|
* @param number
|
* @return
|
*/
|
public static boolean isIntegerNumber(String number){
|
number = number.trim();
|
String intNumRegex = "\\-{0,1}\\d+"; //ÕûÊýµÄÕýÔò±í´ïʽ
|
if(number.matches(intNumRegex))
|
return true;
|
else
|
return false;
|
}
|
|
/**
|
* ÅжÏnumber²ÎÊýÊÇ·ñÊǸ¡µãÊý±íʾ·½Ê½
|
* @param numbe
|
* @return
|
*/
|
public static boolean isFloatPointNumber(String number){
|
number = number.trim();
|
String intNumRegex = "\\-{0,1}\\d+"; //ÕûÊýµÄÕýÔò±í´ïʽ
|
String pointPrefix = "(\\-|\\+){0,1}\\d*\\.\\d+"; //¸¡µãÊýµÄÕýÔò±í´ïʽ-СÊýµãÔÚÖмäÓëÇ°Ãæ
|
String pointSuffix = "(\\-|\\+){0,1}\\d+\\."; //¸¡µãÊýµÄÕýÔò±í´ïʽ-СÊýµãÔÚºóÃæ
|
if(number.matches(intNumRegex) || number.matches(pointPrefix) || number.matches(pointSuffix))
|
return true;
|
else
|
return false;
|
}
|
|
/**
|
* ÅжÏnumber²ÎÊýÊÇ·ñÊÇÈÕÆÚ±íʾ·½Ê½
|
* @param str
|
* @return
|
*/
|
public static boolean isValidDate(String str, String format) {
|
boolean convertSuccess = true;
|
try {
|
//DateTimeFormat.setLenient(false);
|
DateFormat dtf = new SimpleDateFormat(format);
|
dtf.parse(str);
|
} catch (ParseException e) {
|
convertSuccess = false;
|
}
|
return convertSuccess;
|
}
|
|
/**
|
*
|
* @param
|
* @return
|
*/
|
public static float[] sortData(float[] data, int len) {
|
float[] sort_data = new float[len];
|
for(int n=0; n<sort_data.length; n++)
|
sort_data[n] = data[n];
|
|
float tmp;
|
for(int n=0; n<sort_data.length; n++)
|
{
|
tmp = sort_data[n];
|
for(int m=n; m<sort_data.length; m++)
|
{
|
if(sort_data[n] > sort_data[m])
|
{
|
sort_data[n] = sort_data[m];
|
sort_data[m] = tmp;
|
tmp = sort_data[n];
|
}
|
}
|
}
|
|
return sort_data;
|
}
|
|
/**
|
*
|
* @param
|
* @return
|
*/
|
public static float getAvrageData(float[] data, int len) {
|
float sum_data = 0;
|
for(int n=0; n<len; n++)
|
sum_data += data[n];
|
|
return (sum_data/len);
|
}
|
|
/**
|
*
|
* @param
|
* @return
|
*/
|
/*
|
public static short CalCRC16(byte[] Pushdata, int length)
|
{
|
int Reg_CRC = 0xFFFF;
|
for(int i = 0; i<length; i ++)
|
{
|
Reg_CRC ^= Pushdata[i];
|
for (int j = 0; j<8; j++)
|
{
|
if((Reg_CRC & 0x0001) > 0)
|
Reg_CRC = (Reg_CRC>>1) ^ 0xA001;
|
else
|
Reg_CRC >>= 1;
|
}
|
}
|
return (short)(Reg_CRC&0xFFFF);
|
}
|
*/
|
/**
|
*
|
* @param
|
* @return
|
*/
|
static public String getLoacalHostIp()
|
{
|
Enumeration<?> allNetInterfaces = null;
|
try {
|
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
|
} catch (java.net.SocketException e) {
|
e.printStackTrace();
|
}
|
|
while (allNetInterfaces.hasMoreElements())
|
{
|
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
|
System.out.println(netInterface.getName());
|
|
Enumeration<?> addresses = netInterface.getInetAddresses();
|
while (addresses.hasMoreElements())
|
{
|
InetAddress ip = (InetAddress) addresses.nextElement();
|
if (ip != null && ip instanceof Inet4Address)
|
{
|
System.out.println(netInterface.getName() + ": " + ip.getHostAddress());
|
if(netInterface.getName().contains("wlan0"))
|
{
|
return ip.getHostAddress();
|
}
|
}
|
}
|
}
|
|
return "127.0.0.1";
|
}
|
/**
|
*
|
* @param
|
* @return
|
*/
|
public static boolean getIPFromStr(String ipstr, byte ip[])
|
{
|
boolean res = false;
|
try {
|
for(int n=0; n<3; n++) {
|
int index = ipstr.indexOf('.');
|
if(index > 0)
|
ip[n] = (byte)Integer.parseInt(ipstr.substring(0, index));
|
|
ipstr = ipstr.substring(index+1);
|
}
|
ip[3] = (byte)Integer.parseInt(ipstr);
|
|
res = true;
|
} catch(Exception e) {
|
System.out.println(e.getMessage());
|
}
|
|
return res;
|
}
|
/**
|
*
|
* @param
|
* @return
|
*/
|
public static Object getComboBoxItemText(JComboBox<Object> cb, int it_index)
|
{
|
Object obj = "";
|
if(it_index < cb.getItemCount())
|
obj = cb.getItemAt(it_index);
|
|
return obj;
|
}
|
/**
|
*
|
* @param
|
* @return
|
*/
|
public static int getComboBoxItemIndex(JComboBox<String> cb_sex, String it_obj)
|
{
|
int it_index = -1;
|
for(int n=0; n<cb_sex.getItemCount(); n++)
|
{
|
if(cb_sex.getItemAt(n).equals(it_obj))
|
{
|
it_index = n;
|
break;
|
}
|
}
|
|
return it_index;
|
}
|
|
public static int getComboBoxItemIndex(String[] cb, String it_obj)
|
{
|
int it_index = -1;
|
for(int n=0; n<cb.length; n++)
|
{
|
if(cb[n].equals(it_obj))
|
{
|
it_index = n;
|
break;
|
}
|
}
|
|
return it_index;
|
}
|
|
public static String getAppPath() {
|
String app_path = System.getProperty("user.dir");
|
return app_path;
|
}
|
|
public static void copyTableRowData(Vector<Vector<Object>> src_vvo, Vector<Vector<Object>> target_vvo)
|
{
|
if((null != src_vvo) && (null != target_vvo)) {
|
if(target_vvo.size() < src_vvo.size()) {
|
for(int n=0; n<src_vvo.size(); n++) {
|
if(n < target_vvo.size()) {
|
target_vvo.setElementAt(src_vvo.get(n), n);
|
} else {
|
target_vvo.add(src_vvo.get(n));
|
}
|
}
|
} else {
|
for(int n=(target_vvo.size()-1); n>=0; n--) {
|
if(n >= src_vvo.size()) {
|
target_vvo.removeElementAt(n);
|
} else {
|
target_vvo.setElementAt(src_vvo.get(n), n);
|
}
|
}
|
}
|
}
|
}
|
|
public static int getPid() {
|
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
|
String name = runtime.getName(); // format: "pid@hostname"
|
try {
|
return Integer.parseInt(name.substring(0, name.indexOf('@')));
|
} catch (Exception e) {
|
return -1;
|
}
|
}
|
|
public static void setTFDocLimit(JTextField tf, int charlen, String str) {
|
tf.setDocument(new LimitedDocument(charlen, str));
|
}
|
|
/**
|
* ½«Ê±¼äת»¯³ÉH:M:S
|
* @param min
|
* @return
|
*/
|
public static String minConvertHourMinSecond(Integer min) {
|
String result = "";
|
if (min != null) {
|
Integer m = min;
|
String format;
|
Object[] array;
|
Integer days = m / (60 * 24);
|
Integer hours = m / (60) - days * 24;
|
Integer minutes;
|
Integer second;
|
if (days > 0) {
|
hours += days * 24;
|
minutes = m - hours * 60;
|
second = ( m - minutes - hours * 60) / 60;
|
}else{
|
minutes = m - hours * 60 - days * 24 * 60;
|
second = (m - minutes - hours * 60) / 60;
|
}
|
String hoursstr = null;
|
String minutesstr = null;
|
String secondstr = null;
|
if(hours < 10){
|
hoursstr = "0"+hours;
|
}else {
|
hoursstr = hours + "";
|
}
|
if(minutes < 10){
|
minutesstr="0"+minutes;
|
}else {
|
minutesstr = minutes + "";
|
}
|
if(second < 10){
|
secondstr="0"+second;
|
}else {
|
secondstr = second + "";
|
}
|
format = "%1$s:%2$s:%3$s";
|
array = new Object[]{hoursstr, minutesstr,secondstr};
|
result = String.format(format, array);
|
}
|
return result;
|
}
|
|
public static void main(String[] args) {
|
System.out.println(minConvertHourMinSecond(61));
|
}
|
}
|