whyclj
2021-01-05 64aba412c2b739d67795b14a3cae069d311697f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com;
 
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;
 
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 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");
    
    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);
                    }
                }
            }
        }
    }
}