whyclj
2019-10-16 3825e10fc9eba23411e8836b34ba70ed10abee7b
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
package com.util;
 
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_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());
        }
    }
}