依赖
import com.github.pagehelper.util.StringUtil;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.security.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
下面有很多的工具类,可以按自己的需求来查找
package com.ruoyi.common.utils;
import com.github.pagehelper.util.StringUtil;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.security.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EonUtils {
private static Map forClockin(String latitudeS, String longitudeS, String latitudeT, String longitudeT) {
GlobalCoordinates source = new GlobalCoordinates(EonUtils.CastUtil.castDouble(latitudeS), EonUtils.CastUtil.castDouble(longitudeS));
GlobalCoordinates target = new GlobalCoordinates(EonUtils.CastUtil.castDouble(latitudeT), EonUtils.CastUtil.castDouble(longitudeT));
GeodeticCalculator geodeticCalculator = new GeodeticCalculator();
double meter2 = geodeticCalculator.calculateGeodeticCurve(Ellipsoid.WGS84, source, target).getEllipsoidalDistance();
Double l1 = EonUtils.CastUtil.castDouble(1500.0000);
Boolean result = false;
if (l1.compareTo(meter2) >= 0) {
result = true;
}
EonUtils.CastUtil.castLong(meter2);
HashMap<String, Object> resultMap = new HashMap<>();
resultMap.put("DiffForST", meter2);
resultMap.put("Type", result);
System.err.println(resultMap.toString());
return resultMap;
}
public static class ConfigurationManager {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationManager.class);
private static Properties prop = new Properties();
private static final String fileName = "usertrack.properties";
static {
InputStream in = null;
try {
in = ConfigurationManager.class
.getClassLoader().getResourceAsStream(fileName);
prop.load(in);
if (in == null) {
throw new FileNotFoundException(fileName + "file is not Found");
}
} catch (Exception e) {
LOGGER.error("load properties file filure", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
LOGGER.error("close input stream failure", e);
}
}
}
}
public static String getProperty(String key) {
return prop.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
String value = defaultValue;
if (prop.containsKey(key)) {
value = getProperty(key);
}
return value;
}
public static String getString(String key) {
return getProperty(key);
}
public static String getString(String key, String defaultValue) {
return getProperty(key, defaultValue);
}
public static Integer getInteger(String key) {
String value = getProperty(key);
try {
return Integer.valueOf(value);
} catch (Exception e) {
LOGGER.error("value is: '" + value + "' Not a numeric string and can not be converted to a number", e);
}
return 0;
}
public static Integer getInteger(String key, int defaultValue) {
int value = defaultValue;
if (prop.containsKey(key)) {
value = getInteger(key);
}
return value;
}
public static Boolean getBoolean(String key) {
String value = getProperty(key);
try {
return Boolean.valueOf(value);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static Boolean getBoolean(String key, Boolean defaultValue) {
Boolean value = defaultValue;
if (prop.containsKey(key)) {
String str = getProperty(key);
if (str.equalsIgnoreCase("true") && str.equalsIgnoreCase("false")) {
value = getBoolean(key);
}
}
return value;
}
public static Long getLong(String key) {
String value = getProperty(key);
try {
return Long.valueOf(value);
} catch (Exception e) {
LOGGER.error("value is: '" + value + "' Not a Long type and can not be converted to a Long", e);
}
return 0L;
}
public static Long getLong(String key, Long defaultValue) {
Long value = defaultValue;
if (prop.containsKey(key)) {
value = getLong(key);
}
return value;
}
}
public static class CastUtil {
public static String castString(Object obj) {
return CastUtil.castString(obj, "");
}
public static String castString(Object obj, String defaultValue) {
return obj != null ? String.valueOf(obj) : defaultValue;
}
public static double castDouble(Object obj) {
return CastUtil.castDouble(obj, 0);
}
public static double castDouble(Object obj, double defaultValue) {
double value = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtil.isNotEmpty(strValue)) {
try {
value = Double.parseDouble(strValue);
} catch (NumberFormatException e) {
value = defaultValue;
}
}
}
return value;
}
public static long castLong(Object obj) {
return CastUtil.castLong(obj, 0);
}
public static long castLong(Object obj, long defaultValue) {
long value = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtil.isNotEmpty(strValue)) {
try {
value = Long.parseLong(strValue);
} catch (NumberFormatException e) {
value = defaultValue;
}
}
}
return value;
}
public static int castInt(Object obj) {
return CastUtil.castInt(obj, 0);
}
public static int castInt(Object obj, int defaultValue) {
int value = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtil.isNotEmpty(strValue)) {
try {
value = Integer.parseInt(strValue);
} catch (NumberFormatException e) {
value = defaultValue;
}
}
}
return value;
}
public static boolean castBoolean(Object obj) {
return CastUtil.castBoolean(obj, false);
}
public static boolean castBoolean(Object obj, boolean defaultValue) {
boolean value = defaultValue;
if (obj != null) {
value = Boolean.parseBoolean(castString(obj));
}
return value;
}
}
public static class FileUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);
private static final String WIN_NEW_LINE = "\r\n";
public static BufferedReader getReader(String filePath, String encode) {
BufferedReader reader = null;
try {
File filename = new File(filePath);
if (!filename.exists()) {
throw new FileNotFoundException(filePath + "file is not Found");
}
FileInputStream fileInputStream = new FileInputStream(filename);
reader = new BufferedReader(new InputStreamReader(fileInputStream, encode));
} catch (IOException e) {
LOGGER.error("read file failure", e);
}
return reader;
}
public static BufferedReader getReader(String filePath) {
return getReader(filePath, "utf-8");
}
public static BufferedWriter getWriter(String writerFilePath, Boolean isAppend) {
BufferedWriter writer = null;
try {
File writeFileName = new File(writerFilePath);
if (!writeFileName.exists()) {
writeFileName.createNewFile();
}
FileWriter fileWriter = new FileWriter(writeFileName, isAppend);
writer = new BufferedWriter(fileWriter);
} catch (IOException e) {
e.printStackTrace();
}
return writer;
}
public static BufferedWriter getWriter(String writerFilePath) {
return getWriter(writerFilePath, false);
}
public static void writeFile(String writerFilePath, String context, Boolean isAppend) {
BufferedWriter writer = getWriter(writerFilePath, isAppend);
StringBuffer buffer = new StringBuffer();
buffer.append(context + WIN_NEW_LINE);
try {
writer.write(buffer.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeFile(String writerFilePath, String context) {
writeFile(writerFilePath, context, false);
}
public static String readfileToString(String filePath, String encode) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader reader = getReader(filePath, encode);
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static String readfileToString(String filePath) {
return readfileToString(filePath, "utf-8");
}
public static List<String> readFileToList(String filePath, String encode) {
List<String> lines = new ArrayList<String>();
String line = null;
BufferedReader reader = getReader(filePath, encode);
try {
while ((line = reader.readLine()) != null) {
lines.add(line.toString());
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
public static List<String> readFileToList(String filePath) {
return readFileToList(filePath, "utf-8");
}
public static boolean mkDir(String dirPath) {
File file = new File(dirPath);
return file.mkdirs();
}
public static boolean touch(String filePath) {
File file = new File(filePath);
boolean success = false;
try {
if (!file.exists()) {
success = file.createNewFile();
}
} catch (Exception e) {
LOGGER.error("新建文件操作出错", e);
}
return success;
}
public static void deleteEveryThing(String filePath) {
try {
File file = new File(filePath);
if (!file.exists()) {
return;
}
if (file.isFile()) {
file.delete();
} else {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
String root = files[i].getAbsolutePath();
deleteEveryThing(root);
}
file.delete();
}
} catch (Exception e) {
LOGGER.error("删除文件失败", e);
}
}
public static boolean fileIsExists(String filePath) {
File file = new File(filePath);
return file.exists();
}
public static List<String> getAllFileNameInFold(String fold_path) {
List<String> file_paths = new ArrayList<String>();
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(fold_path);
while (folderList.size() > 0) {
File file = new File(folderList.peekLast());
folderList.removeLast();
File[] files = file.listFiles();
ArrayList<File> fileList = new ArrayList<File>();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
folderList.add(files[i].getPath());
} else {
fileList.add(files[i]);
}
}
for (File f : fileList) {
file_paths.add(f.getAbsoluteFile().getPath());
}
}
return file_paths;
}
}
public static class DataUtil {
public static final String DATE_FORMAT_YYYY = "yyyy";
public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";
public static final String DATE_FORMAT_YYMMDD = "yyMMdd";
public static final String DATE_FORMAT_YY_MM_DD = "yy-MM-dd";
public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
public static final String DATE_FORMAT_POINTYYYYMMDD = "yyyy.MM.dd";
public static final String DATE_TIME_FORMAT_YYYY年MM月DD日 = "yyyy年MM月dd日";
public static final String DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm";
public static final String DATE_TIME_FORMAT_YYYYMMDD_HH_MI = "yyyyMMdd HH:mm";
public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";
public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";
public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";
public static final String DATE_FORMAT_MMDDHHMI = "MM-dd HH:mm";
public static Integer getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
public static Integer getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
}
public static Integer getDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DATE);
return day;
}
public static String parseDateToStr(Date time, String timeFromat) {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
}
public static String parseTimestampToStr(Timestamp timestamp, String timeFromat) {
SimpleDateFormat df = new SimpleDateFormat(timeFromat);
return df.format(timestamp);
}
public static String parseDateToStr(Date time, String timeFromat, final Date defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
} catch (Exception e) {
if (defaultValue != null)
return parseDateToStr(defaultValue, timeFromat);
else
return parseDateToStr(new Date(), timeFromat);
}
}
public static String parseDateToStr(Date time, String timeFromat, final String defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
} catch (Exception e) {
return defaultValue;
}
}
public static Date parseStrToDate(String time, String timeFromat) {
if (time == null || time.equals("")) {
return null;
}
Date date = null;
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
date = dateFormat.parse(time);
} catch (Exception e) {
}
return date;
}
public static Date parseStrToDate(String strTime, String timeFromat,
Date defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.parse(strTime);
} catch (Exception e) {
return defaultValue;
}
}
public static Date strToDate(String strTime) {
if (strTime == null || strTime.trim().length() <= 0)
return null;
Date date = null;
List<String> list = new ArrayList<String>(0);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
list.add(DATE_FORMAT_YYYY_MM_DD);
list.add(DATE_FORMAT_YYYYMMDD);
list.add(DATE_FORMAT_YYYY_MM);
list.add(DATE_FORMAT_YYYYMM);
list.add(DATE_FORMAT_YYYY);
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
String format = (String) iter.next();
if (strTime.indexOf("-") > 0 && format.indexOf("-") < 0)
continue;
if (strTime.indexOf("-") < 0 && format.indexOf("-") > 0)
continue;
if (strTime.length() > format.length())
continue;
date = parseStrToDate(strTime, format);
if (date != null)
break;
}
return date;
}
public static List<String> getMonthListOfDate(String beginDateStr, String endDateStr) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");
String sRet = "";
Date beginDate = null;
Date endDate = null;
GregorianCalendar beginGC = null;
GregorianCalendar endGC = null;
List<String> list = new ArrayList<String>();
try {
beginDate = f.parse(beginDateStr);
endDate = f.parse(endDateStr);
beginGC = new GregorianCalendar();
beginGC.setTime(beginDate);
endGC = new GregorianCalendar();
endGC.setTime(endDate);
while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {
sRet = beginGC.get(Calendar.YEAR) + "-"
+ (beginGC.get(Calendar.MONTH) + 1);
list.add(sRet);
beginGC.add(Calendar.MONTH, 1);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static List<String> getDayListOfDate(String beginDateStr, String endDateStr) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate = null;
Date endDate = null;
Calendar beginGC = null;
Calendar endGC = null;
List<String> list = new ArrayList<String>();
try {
beginDate = f.parse(beginDateStr);
endDate = f.parse(endDateStr);
beginGC = Calendar.getInstance();
beginGC.setTime(beginDate);
endGC = Calendar.getInstance();
endGC.setTime(endDate);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {
list.add(sdf.format(beginGC.getTime()));
beginGC.add(Calendar.DAY_OF_MONTH, 1);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static List<Integer> getYearListOfYears(int before, int behind) {
if (before < 0 || behind < 0) {
return null;
}
List<Integer> list = new ArrayList<Integer>();
Calendar c = null;
c = Calendar.getInstance();
c.setTime(new Date());
int currYear = Calendar.getInstance().get(Calendar.YEAR);
int startYear = currYear - before;
int endYear = currYear + behind;
for (int i = startYear; i < endYear; i++) {
list.add(Integer.valueOf(i));
}
return list;
}
public static Integer getWeekthOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime(date);
return c.get(Calendar.WEEK_OF_YEAR);
}
public static HashMap<Integer, String> getWeekTimeOfYear(int year) {
HashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
int count = getWeekthOfYear(c.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dayOfWeekStart = "";
String dayOfWeekEnd = "";
for (int i = 1; i <= count; i++) {
dayOfWeekStart = sdf.format(getFirstDayOfWeek(year, i));
dayOfWeekEnd = sdf.format(getLastDayOfWeek(year, i));
map.put(Integer.valueOf(i), "第" + i + "周(从" + dayOfWeekStart + "至" + dayOfWeekEnd + ")");
}
return map;
}
public static Integer getWeekCountOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
int count = getWeekthOfYear(c.getTime());
return count;
}
public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
return c.getTime();
}
public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
return c.getTime();
}
public static Date getFirstDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime());
}
public static Date getLastDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getLastDayOfWeek(cal.getTime());
}
public static Date getFirstDayOfMonth(int year, int month) {
month = month - 1;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int day = c.getActualMinimum(c.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public static Date getLastDayOfMonth(int year, int month) {
month = month - 1;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int day = c.getActualMaximum(c.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
public static String getDayWeekOfDate1(Date date) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static Integer getDayWeekOfDate2(Date date) {
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(date);
int weekDay = aCalendar.get(Calendar.DAY_OF_WEEK);
return weekDay;
}
public static boolean validateIsDate(String strTime) {
if (strTime == null || strTime.trim().length() <= 0)
return false;
Date date = null;
List<String> list = new ArrayList<String>(0);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
list.add(DATE_FORMAT_YYYY_MM_DD);
list.add(DATE_FORMAT_YYYYMMDD);
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
String format = (String) iter.next();
if (strTime.indexOf("-") > 0 && format.indexOf("-") < 0)
continue;
if (strTime.indexOf("-") < 0 && format.indexOf("-") > 0)
continue;
if (strTime.length() > format.length())
continue;
date = parseStrToDate(strTime.trim(), format);
if (date != null)
break;
}
if (date != null) {
System.out.println("生成的日期:" + EonUtils.DataUtil.parseDateToStr(date, EonUtils.DataUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS, "--null--"));
return true;
}
return false;
}
public static Date formatHhMmSsOfDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date addDate(Date date, int year, int month, int day, int hour, int minute, int second, int millisecond) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, year);
c.add(Calendar.MONTH, month);
c.add(Calendar.DATE, day);
c.add(Calendar.HOUR, hour);
c.add(Calendar.MINUTE, minute);
c.add(Calendar.SECOND, second);
c.add(Calendar.MILLISECOND, millisecond);
return c.getTime();
}
public static Long getDistanceTimestamp(Date startDate, Date endDate) {
long daysBetween = (endDate.getTime() - startDate.getTime() + 1000000) / (3600 * 24 * 1000);
return daysBetween;
}
public static Boolean compareIsSameMonth(Date date1, Date date2) {
boolean flag = false;
int year1 = getYear(date1);
int year2 = getYear(date2);
if (year1 == year2) {
int month1 = getMonth(date1);
int month2 = getMonth(date2);
if (month1 == month2) flag = true;
}
return flag;
}
public static long[] getDistanceTime(Date one, Date two) {
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
try {
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
} catch (Exception e) {
e.printStackTrace();
}
long[] times = {day, hour, min, sec};
return times;
}
public static long[] getDistanceTime(String str1, String str2) {
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date one;
Date two;
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
} catch (ParseException e) {
e.printStackTrace();
}
long[] times = {day, hour, min, sec};
return times;
}
public static Long getDistanceDays(String str1, String str2) throws Exception {
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date one;
Date two;
long days = 0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
days = diff / (1000 * 60 * 60 * 24);
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
public static Date getDayBeginTime(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public static Date getDayEndTime(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
}
public static int getAge(Date birth) {
Calendar cal = Calendar.getInstance();
int thisYear = cal.get(Calendar.YEAR);
int thisMonth = cal.get(Calendar.MONTH);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birth);
int birthYear = cal.get(Calendar.YEAR);
int birthMonth = cal.get(Calendar.MONTH);
int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int age = thisYear - birthYear;
if (thisMonth <= birthMonth) {
if (thisMonth == birthMonth) {
if (dayOfMonth < birthdayOfMonth) {
age--;
}
} else {
age--;
}
}
return age;
}
public static boolean isPhoneNumber(String phoneNumber) {
String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(phoneNumber);
return m.matches();
}
public static boolean isLatValid(String checkLatitude) {
boolean flag = false;
try {
if (checkLatitude != null) {
Pattern pattern = Pattern.compile("([0-9]|[1-5][0-9])\\.([0-9]{6})");
if (pattern.matcher(checkLatitude).matches()) {
flag = true;
}
}
} catch (Exception e) {
e.getMessage();
}
return flag;
}
public static boolean isLonValid(String checkLonitude) {
boolean flag = false;
try {
if (checkLonitude != null) {
Pattern pattern = Pattern.compile("([7-9][0-9]|1[0-5][0-9])\\.([0-9]{6})");
if (pattern.matcher(checkLonitude).matches()) {
flag = true;
}
}
} catch (Exception e) {
e.getMessage();
}
return flag;
}
public static String IdentityCardVerification(String idStr) {
String[] wf = {"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"};
String[] checkCode = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};
String iDCardNo = "";
try {
if (idStr.length() != 15 && idStr.length() != 18) {
return "身份证号码长度应该为15位或18位";
}
if (idStr.length() == 18) {
iDCardNo = idStr.substring(0, 17);
} else if (idStr.length() == 15) {
iDCardNo = idStr.substring(0, 6) + "19" + idStr.substring(6, 15);
}
if (isStrNum(iDCardNo) == false) {
return "身份证15位号码都应为数字;18位号码除最后一位外,都应为数字";
}
String strYear = iDCardNo.substring(6, 10);
String strMonth = iDCardNo.substring(10, 12);
String strDay = iDCardNo.substring(12, 14);
if (isStrDate(strYear + "-" + strMonth + "-" + strDay) == false) {
return "身份证生日无效";
}
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150 || (gc.getTime().getTime() - s.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
return "身份证生日不在有效范围";
}
if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
return "身份证月份无效";
}
if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
return "身份证日期无效";
}
Hashtable h = GetAreaCode();
if (h.get(iDCardNo.substring(0, 2)) == null) {
return "身份证地区编码错误";
}
int theLastOne = 0;
for (int i = 0; i < 17; i++) {
theLastOne = theLastOne + Integer.parseInt(String.valueOf(iDCardNo.charAt(i))) * Integer.parseInt(checkCode[i]);
}
int modValue = theLastOne % 11;
String strVerifyCode = wf[modValue];
iDCardNo = iDCardNo + strVerifyCode;
if (idStr.length() == 18 && !iDCardNo.equals(idStr)) {
return "身份证无效,不是合法的身份证号码";
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
private static Hashtable GetAreaCode() {
Hashtable hashtable = new Hashtable();
hashtable.put("11", "北京");
hashtable.put("12", "天津");
hashtable.put("13", "河北");
hashtable.put("14", "山西");
hashtable.put("15", "内蒙古");
hashtable.put("21", "辽宁");
hashtable.put("22", "吉林");
hashtable.put("23", "黑龙江");
hashtable.put("31", "上海");
hashtable.put("32", "江苏");
hashtable.put("33", "浙江");
hashtable.put("34", "安徽");
hashtable.put("35", "福建");
hashtable.put("36", "江西");
hashtable.put("37", "山东");
hashtable.put("41", "河南");
hashtable.put("42", "湖北");
hashtable.put("43", "湖南");
hashtable.put("44", "广东");
hashtable.put("45", "广西");
hashtable.put("46", "海南");
hashtable.put("50", "重庆");
hashtable.put("51", "四川");
hashtable.put("52", "贵州");
hashtable.put("53", "云南");
hashtable.put("54", "西藏");
hashtable.put("61", "陕西");
hashtable.put("62", "甘肃");
hashtable.put("63", "青海");
hashtable.put("64", "宁夏");
hashtable.put("65", "新疆");
hashtable.put("71", "台湾");
hashtable.put("81", "香港");
hashtable.put("82", "澳门");
hashtable.put("91", "国外");
return hashtable;
}
private static boolean isStrNum(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (isNum.matches()) {
return true;
} else {
return false;
}
}
public static boolean isStrDate(String strDate) {
Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
Matcher m = pattern.matcher(strDate);
if (m.matches()) {
return true;
} else {
return false;
}
}
public static String[] getPhotos(String photostr) {
if (photostr == null) return null;
String[] split = photostr.split(",");
return split;
}
public static String getPhotoStr(String[] photos) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < photos.length; i++) {
buffer.append(photos[i] + ",");
}
return buffer.toString();
}
public static boolean isZuoJiPhone(String phone){
Pattern p1 = null;
Matcher m = null;
boolean isPhone = false;
p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,8}$");
if (phone.length() > 9) {
m = p1.matcher(phone);
isPhone = m.matches();
} else {
return isPhone;
}
return isPhone;
}
}