java邮箱校验,手机号校验,格式化日期等等

java邮箱校验,手机号校验,格式化日期等等

具体内容

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
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import 这个是传入数据的封装jar包,可以不用看,具体看自己需求context.Recordset;

public class ApplyUtil {
public final static String Number_Chinese = "1"; //2012年2月16日
public final static String Number_Point = "2";//2010.2.16
/**
* 判断数据是否为空
* @param str 字符串
* @return
*/
public static boolean isEmpty(String str){
if(null == str || str.trim().equals("")){
return true;
}
return false;
}

/**
* 判断记录集是否为空
* @param rs
* @return
*/
public static boolean isRecordsetNull(Recordset rs){
if(null == rs || rs.size() == 0){
return true;
}
return false;
}

/**
* 为当前日期增加month月
* @time 2012-6-6 上午10:57:32
* @param month
* @return
*/
public static String addMonth(int month){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sFmt = new SimpleDateFormat("yyyy年MM月dd日");
if (month != 0) {
cal.add(Calendar.MONTH, month);
}
return sFmt.format(cal.getTime());
}


/**
* 根据要求将时间转化成字符串
* @param date 8位字符
* @param style 样式
* @return
*/
public static String parseDate2String(String date,String style){
String formatDate = "";
int len = 0;
if(!ApplyUtil.isEmpty(date)){
len = date.trim().length();
if(len == 8){
if(Number_Chinese.equals(style)){
formatDate = getNumberChineseDate(date);
}
if(Number_Point.equals(style)){
formatDate = getNumberPointDate(date);
}
}else{
formatDate = date;
}
}
return formatDate;
}

/**
* 格式化日期为:2012年2月16日
* @param date
* @return
*/
private static String getNumberChineseDate(String date){
String year = date.substring(0, 4);
String month = leaveZero(date.substring(4, 6));
String day = leaveZero(date.substring(6,8));
return year + "年" + month + "月" + day + "日";
}

/**
* 格式化日期为:2012年2月16日
* @param date
* @return
*/
private static String getNumberPointDate(String date){
String year = date.substring(0, 4);
String month = leaveZero(date.substring(4, 6));
String day = leaveZero(date.substring(6,8));
return year + "." + month + "." + day;
}

/**
* 去掉前面含有的0
* @param str
* @return
*/
private static String leaveZero(String str){
if(str.length() == 2 && str.charAt(0) == '0'){
return str.substring(1,2);
}
return str;
}

/**
* 获得文件的类型
* @author ly
* @date 2012-4-9
* @param file 文件的全路径
* @return
*/
public static String getSuffix(String file) {
if(isEmpty(file) || file.indexOf(".") == -1 || file.indexOf(".") == file.length() - 1)
return "";
System.out.println(file.length());
System.out.println(file.lastIndexOf("."));
String suffix=file.substring(file.lastIndexOf("."),file.length());
return suffix.toLowerCase();
}


/**
* 邮箱校验
* @param email
* @return true:邮箱格式正确; false:邮箱格式错误
*/
public static boolean isEmail(String email){
//String str = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";//带横杠的出错
//String str = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";//带下划线出错
// Pattern p = Pattern.compile(str);
// Matcher m = p.matcher(email.trim());
// return m.matches();
//Pattern pattern = Pattern.compile("[0-9a-zA-Z]*.[0-9a-zA-Z]*@[a-zA-Z]*.[a-zA-Z]*", Pattern.LITERAL);
if(email == null){
return false;
}

//验证开始

//不能有连续的.
if(email.indexOf("..") != -1){
return false;
}

//必须带有@
int atCharacter = email.indexOf("@");
if (atCharacter == -1) {
return false;
}

//最后一个.必须在@之后,且不能连续出现
if(atCharacter > email.lastIndexOf('.') || atCharacter+1 == email.lastIndexOf('.')){
return false;
}

//不能以.,@结束和开始
if (email.endsWith(".") || email.endsWith("@") || email.startsWith(".") || email.startsWith("@")) {
return false;
}
return true;

}

/**
* 手机号校验
* @param mobiles
* @return true:手机号格式正确; false:手机号格式不正确
*/
public static boolean isMobileNO(String mobiles){
String str = "^1[3|4|5|8][0-9][0-9]{8}$";
//String str = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
Pattern p = Pattern.compile(str);
Matcher m = p.matcher(mobiles.trim());
return m.matches();
}

}

本作品采用知识共享署名 4.0 中国大陆许可协议进行许可,欢迎转载,但转载请注明来自御前提笔小书童,并保持转载后文章内容的完整。本人保留所有版权相关权利。

本文链接:https://royalscholar.cn/2017/03/15/java邮箱校验,手机号校验,格式化日期等等/

# JAVA

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×