package com.base;
|
|
import java.io.IOException;
|
import java.text.DateFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
|
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;
|
|
final public static String DTF_YMDhms = "yyyy-MM-dd HH:mm:ss";
|
final public static String DTF_YMDhm = "yyyy-MM-dd HH:mm";
|
final public static String DTF_YMDh = "yyyy-MM-dd HH";
|
final public static String DTF_YMD = "yyyy-MM-dd";
|
final public static String DTFYMD = "yyyyMMdd";
|
final public static String DTF_Y_M_D = "yyyy_MM_dd";
|
final public static String DTF_hms = "HH:mm:ss";
|
final public static String DTF_YMD_h_m_s = "yyyy-MM-dd+HH_mm_ss";
|
//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);
|
}
|
|
/**
|
* ÅжÏ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 str
|
* @return
|
*/
|
public static void setDateTime(Date ts){
|
String osName = System.getProperty("os.name");
|
String cmd = "";
|
|
try {
|
if (osName.matches("^(?i)Windows.*$")) {// Window ϵͳ
|
// ¸ñʽ HH:mm:ss
|
cmd = "cmd /c time " + getDateTimeFormat(ts, Com.DTF_hms);
|
Runtime.getRuntime().exec(cmd);
|
// ¸ñʽ£ºyyyy-MM-dd
|
cmd = "cmd /c date " + getDateTimeFormat(ts, Com.DTF_YMD);
|
Runtime.getRuntime().exec(cmd);
|
} else {// Linux ϵͳ
|
// ¸ñʽ£ºyyyyMMdd
|
cmd = "date -s " + getDateTimeFormat(ts, Com.DTFYMD);
|
Runtime.getRuntime().exec(cmd);
|
// ¸ñʽ HH:mm:ss
|
cmd = "date -s " + getDateTimeFormat(ts, Com.DTF_hms);
|
Runtime.getRuntime().exec(cmd);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
*
|
* @param
|
* @return
|
*/
|
public static void getIPFromStr(String ipstr, byte ip[])
|
{
|
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);
|
|
//System.out.println((ip[0]&0xFF) + ": "+(ip[1]&0xFF) + ": "+(ip[2]&0xFF)+ ": "+(ip[3]&0xFF));
|
}
|
catch(Exception e)
|
{
|
System.out.println(e.getMessage());
|
}
|
}
|
}
|