Skip to content

Commit f5477f5

Browse files
committed
Tools
//生成六位随机数验证码 //检测字符串是否不为null 或 "" //检测字符串是否为null 或 "" //字符串转化为字符串数组 //用默认的分隔符(,)将字符串转换为字符串数组 //按照指定格式,日期转为字符串 //按照yyyy-MM-dd HH:mm:ss的格式,日期转字符串 //按照yyyy-MM-dd HH:mm:ss的格式,字符串转日期 //把时间根据时、分、秒转换为时间段 //写txt里的单行内容 //验证邮箱 //验证手机号码 //检测KEY是否正确 //读取txt里的单行内容
1 parent 933fbc3 commit f5477f5

1 file changed

Lines changed: 207 additions & 0 deletions

File tree

‎Tools.java‎

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package com.fh.util;
2+
3+
import java.io.*;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
import java.util.Random;
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
10+
11+
/**
12+
* DateTime: 2016/9/19 21:43
13+
* 功能:工具类
14+
* 思路:
15+
*/
16+
public class Tools {
17+
//测试
18+
public static void main(String[] args) {
19+
System.out.println(getRandomNum());
20+
System.out.println(notEmpty(""));
21+
System.out.println(str2StrArray("abc,def,hig")[0]);
22+
System.out.println(date2Str(new Date()));
23+
Date old=new Date();
24+
try {
25+
Thread.sleep(10000);
26+
} catch ( InterruptedException e ) {
27+
e.printStackTrace();
28+
}
29+
System.out.println(getTimes(date2Str(old)));
30+
}
31+
32+
//生成六位随机数验证码
33+
public static int getRandomNum(){
34+
Random r=new Random();
35+
return r.nextInt(900000)+100000;
36+
}
37+
38+
//检测字符串是否不为null 或 ""
39+
public static boolean notEmpty(String s){
40+
return s!=null && !"".equals(s) && !"null".equals(s);
41+
}
42+
43+
//检测字符串是否为null 或 ""
44+
public static boolean isEmpty(String s){
45+
return s==null || "".equals(s) || "null".equals(s);
46+
}
47+
48+
//字符串转化为字符串数组
49+
public static String[] str2StrArray(String str,String splitRegex){
50+
if(isEmpty(str)){
51+
return null;
52+
}
53+
return str.split(splitRegex);
54+
}
55+
56+
//用默认的分隔符(,)将字符串转换为字符串数组
57+
public static String[] str2StrArray(String str){
58+
return str2StrArray(str,",\\s*");
59+
}
60+
61+
//按照指定格式,日期转为字符串
62+
public static String date2Str(Date date,String format){
63+
if(null!=date){
64+
SimpleDateFormat sdf=new SimpleDateFormat(format);
65+
return sdf.format(date);
66+
}else return "";
67+
}
68+
69+
//按照yyyy-MM-dd HH:mm:ss的格式,日期转字符串
70+
public static String date2Str(Date date){
71+
return date2Str(date,"yyyy-MM-dd HH:mm:ss");
72+
}
73+
74+
//按照yyyy-MM-dd HH:mm:ss的格式,字符串转日期
75+
public static Date str2Date(String str){
76+
if(notEmpty(str)){
77+
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
78+
try {
79+
return sdf.parse(str);
80+
} catch ( ParseException e ) {
81+
e.printStackTrace();
82+
}
83+
return new Date();
84+
}else return null;
85+
}
86+
87+
//把时间根据时、分、秒转换为时间段
88+
public static String getTimes(String strDate){
89+
String resultTimes="";
90+
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
91+
Date now;
92+
93+
try {
94+
now = new Date(); //现在时间
95+
Date date = sdf.parse(strDate); //传入时间
96+
long times = now.getTime()-date.getTime();
97+
long day = times/(24*60*60*1000);
98+
long hour = (times/(60*60*1000)-day*24);
99+
long min = ((times/(60*1000))-day*24*60-hour*60);
100+
long sec = (times/1000-day*24*60*60-hour*60*60-min*60);
101+
102+
StringBuffer sb=new StringBuffer();
103+
if(hour>0){
104+
sb.append(hour+"小时前");
105+
}else if(min>0){
106+
sb.append(min+"分钟前");
107+
}else {
108+
sb.append(sec+"秒前");
109+
}
110+
resultTimes=sb.toString();
111+
112+
} catch ( ParseException e ) {
113+
e.printStackTrace();
114+
}
115+
return resultTimes;
116+
}
117+
118+
//写txt里的单行内容
119+
public static void writeFile(String fileP,String content){
120+
String filePath=String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../"; //项目路径
121+
filePath=(filePath.trim()+fileP.trim()).substring(6).trim();
122+
if(filePath.indexOf(":")!=1){
123+
filePath= File.separator+filePath;
124+
}
125+
126+
try {
127+
OutputStreamWriter write=new OutputStreamWriter(new FileOutputStream(filePath),"utf-8");
128+
BufferedWriter writer=new BufferedWriter(write);
129+
writer.write(content);
130+
writer.close();
131+
} catch ( FileNotFoundException e ) {
132+
e.printStackTrace();
133+
} catch ( UnsupportedEncodingException e ) {
134+
e.printStackTrace();
135+
} catch ( IOException e ) {
136+
e.printStackTrace();
137+
}
138+
139+
}
140+
141+
//验证邮箱
142+
public static boolean checkEmail(String email){
143+
boolean flag=false;
144+
try {
145+
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
146+
Pattern regex=Pattern.compile(check);
147+
Matcher matcher=regex.matcher(email);
148+
flag=matcher.matches();
149+
}catch ( Exception e ){
150+
flag=false;
151+
}
152+
return flag;
153+
}
154+
155+
//验证手机号码
156+
public static boolean checkMobileNumber(String mobileNumber){
157+
boolean flag=false;
158+
try {
159+
Pattern regex=Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
160+
Matcher matcher=regex.matcher(mobileNumber);
161+
flag=matcher.matches();
162+
}catch ( Exception e ){
163+
flag=false;
164+
}
165+
return flag;
166+
}
167+
168+
//检测KEY是否正确
169+
public static boolean checkKey(String paraname,String FKEY){
170+
paraname=(null==paraname)?"":paraname;
171+
return MD5.md5(paraname+DateUtil.getDays()+",fh,").equals(FKEY);
172+
}
173+
174+
//读取txt里的单行内容
175+
public static String readTxtFile(String fileP){
176+
try {
177+
String filePath=String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../";//项目路径
178+
filePath=filePath.replaceAll("file:/", "");
179+
filePath=filePath.replaceAll("%20", " ");
180+
filePath=filePath.trim()+fileP.trim();
181+
if(filePath.indexOf(":")!=1){
182+
filePath=File.separator+filePath;
183+
}
184+
String encoding="utf-8";
185+
File file=new File(filePath);
186+
if(file.isFile() && file.exists()){ //判断文件是否存在
187+
InputStreamReader read=new InputStreamReader(new FileInputStream(file),encoding); // 考虑到编码格式
188+
BufferedReader bufferedReader=new BufferedReader(read);
189+
String lineTxt=null;
190+
while ( (lineTxt=bufferedReader.readLine())!=null ){
191+
return lineTxt;
192+
}
193+
read.close();
194+
}else {
195+
System.out.println("找不到指定的文件,查看此路径是否正确:"+filePath);
196+
}
197+
} catch ( FileNotFoundException e ) {
198+
e.printStackTrace();
199+
} catch ( UnsupportedEncodingException e ) {
200+
e.printStackTrace();
201+
} catch ( IOException e ) {
202+
e.printStackTrace();
203+
}
204+
return "";
205+
}
206+
207+
}

0 commit comments

Comments
 (0)