分包处理

main
tamguo 7 years ago
parent cb672719a0
commit 697291236f

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

@ -0,0 +1 @@
/target/

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>tamguo-common</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tamguo</groupId>
<artifactId>tamguo-common</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<finalName>tamguo-modules-core</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,114 @@
package com.tamguo.common.utils;
import java.io.InterruptedIOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public abstract class AbstractRunningLogHandler implements LogHandler {
private static Method getStackTraceMethod;
private static Method getClassNameMethod;
private static Method getMethodNameMethod;
private static Method getFileNameMethod;
private static Method getLineNumberMethod;
static {
try {
Class<?>[] noArgs = null;
getStackTraceMethod = Throwable.class.getMethod("getStackTrace", noArgs);
Class<?> stackTraceElementClass = Class.forName("java.lang.StackTraceElement");
getClassNameMethod = stackTraceElementClass.getMethod("getClassName", noArgs);
getMethodNameMethod = stackTraceElementClass.getMethod("getMethodName", noArgs);
getFileNameMethod = stackTraceElementClass.getMethod("getFileName", noArgs);
getLineNumberMethod = stackTraceElementClass.getMethod("getLineNumber", noArgs);
} catch (ClassNotFoundException ex) {
LogDebug.debug("will use pre-JDK 1.4 methods to determine location.");
} catch (NoSuchMethodException ex) {
LogDebug.debug("will use pre-JDK 1.4 methods to determine location.");
}
}
/**
* classStackTraceElement
*
* @param t
* @param fqnOfCallingClass
*
* @return
*/
protected StackTraceElement getRunningStackTrace(Throwable t, String fqnOfCallingClass) {
if (getLineNumberMethod != null) {
try {
Object[] noArgs = null;
Object[] elements = (Object[]) getStackTraceMethod.invoke(t, noArgs);
for (int i = elements.length - 1; i >= 0; i--) {
String thisClass = (String) getClassNameMethod.invoke(elements[i], noArgs);
if (fqnOfCallingClass.equals(thisClass)) {
// 执行class名称
String className = fqnOfCallingClass;
// 执行方法名称
String methodName = (String) getMethodNameMethod.invoke(elements[i], noArgs);
// 执行class文件名称
String fileName = (String) getFileNameMethod.invoke(elements[i], noArgs);
// 执行到行号
int lineNumber = ((Integer) getLineNumberMethod.invoke(elements[i], noArgs)).intValue();
return new StackTraceElement(className, methodName, fileName, lineNumber);
}
}
} catch (IllegalAccessException ex) {
LogDebug.debug("failed using JDK 1.4 methods", ex);
} catch (InvocationTargetException ex) {
if (ex.getTargetException() instanceof InterruptedException
|| ex.getTargetException() instanceof InterruptedIOException) {
Thread.currentThread().interrupt();
}
LogDebug.debug("failed using JDK 1.4 methods", ex);
} catch (RuntimeException ex) {
LogDebug.debug("failed using JDK 1.4 methods", ex);
}
}
return this.createDefaultStackTrace();
}
/**
* StackTraceElement
*
* @return
*/
private StackTraceElement createDefaultStackTrace() {
return new StackTraceElement(this.getClass().getName(), "log", this.getClass().getName(), 0);
}
@Override
public void info(String msg, String fqnOfCallingClass) {
}
@Override
public void info(String msg, Throwable t, String fqnOfCallingClass) {
}
@Override
public void error(String msg, String fqnOfCallingClass) {
}
@Override
public void error(String msg, Throwable t, String fqnOfCallingClass) {
}
@Override
public void debug(String msg, String fqnOfCallingClass) {
}
@Override
public void debug(String msg, Throwable t, String fqnOfCallingClass) {
}
@Override
public void warning(String msg, String fqnOfCallingClass) {
}
@Override
public void warning(String msg, Throwable t, String fqnOfCallingClass) {
}
}

@ -0,0 +1,23 @@
package com.tamguo.common.utils;
public class CException extends RuntimeException {
private static final long serialVersionUID = 6401592364022805815L;
public CException() {
super();
}
public CException(String message, Throwable cause) {
super(message, cause);
}
public CException(String message) {
super(message);
}
public CException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,524 @@
package com.tamguo.common.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public final class DateUtil {
/**
* .
*/
private static final SimpleDateFormat NORMAL_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat NORMAL_DATE_FORMAT_YY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat NORMAL_DATE_FORMAT_YY_MM_DD_ = new SimpleDateFormat("yyyyMMdd");
private static final SimpleDateFormat NORMAL_DATE_FORMAT_YYMMDDHHMISS = new SimpleDateFormat("yyyyMMddHHmmss");
/**
* .
*
* @param date
* 197011
* @return (2011-11-24)
*/
public static String dateFormatYYMMDD(long date) {
return NORMAL_DATE_FORMAT_YY_MM_DD.format(new Date(date * 1000));
}
/**
* .
*
* @param date
* 197011
* @return (20111124)
*/
public static String getCurrentDateStr() {
return NORMAL_DATE_FORMAT_YY_MM_DD_.format(new Date());
}
public static String getCurrentDateYYYYMMDDStr() {
return NORMAL_DATE_FORMAT_YY_MM_DD.format(new Date());
}
public static String getNextDayYYYYMMDDStr() {
Calendar cal = Calendar.getInstance();
try {
cal.setTime(NORMAL_DATE_FORMAT_YY_MM_DD.parse(getCurrentTime()));
cal.add(Calendar.DATE, 1);
return NORMAL_DATE_FORMAT_YY_MM_DD.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static long getNextDayYYYYMMDDLong() {
Calendar cal = Calendar.getInstance();
try {
cal.setTime(NORMAL_DATE_FORMAT_YY_MM_DD.parse(getCurrentTime()));
cal.add(Calendar.DATE, 1);
return cal.getTimeInMillis();
} catch (ParseException e) {
e.printStackTrace();
}
return 0l;
}
/**
* .
*
* @param date
* 197011
* @return (2011-11-24 16:46:38)
*/
public static String dateFormat(long date) {
return NORMAL_DATE_FORMAT.format(new Date(date * 1000));
}
/**
* .
*
* @param date
* 197011
* @return (2011-11-24)
*/
public static String dateFormatYYYYMMDD(long date) {
return NORMAL_DATE_FORMAT_YY_MM_DD.format(new Date(date * 1000));
}
/**
* .
*
* @param date
* 197011
* @return (2011-11-24 16:46:38)
*/
public static String dateFormat(Date date) {
return NORMAL_DATE_FORMAT.format(date);
}
/**
* .
*
* @param date
* 197011
* @return (20111124164638)
*/
public static String dateFormathhmmss(Date date) {
return NORMAL_DATE_FORMAT_YYMMDDHHMISS.format(date);
}
/**
* .
*
* @param date
* 197011
* @return (20111124164638)
*/
public static String dateFormatYYYYMMDD(Date date) {
return NORMAL_DATE_FORMAT_YY_MM_DD_.format(date);
}
/**
* .
*
* @param date
* 197011
* @return (2011-11-24 16:46:38)
*/
public static String getCurrentTime() {
return NORMAL_DATE_FORMAT_YY_MM_DD.format(new Date());
}
/**
* 197011.
*
* @return 197011.
*/
public static Long getTime() {
return new Date().getTime() / 1000;
}
/**
* 197011.
*
* @return .
*/
public static long getLastWeekTime() {
return new Date().getTime() / 1000 - 7 * 24 * 60 * 60;
}
/**
* 197011.
*
* @return .
*/
public static long getLastMonthTime() {
return new Date().getTime() / 1000 - 30 * 24 * 60 * 60;
}
/**
* .
*
* @return 2011-12-31 00:00:00 .
*/
public static long getFirstDayOfMonth(String year, String month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.valueOf(year));
cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
return parseDate(NORMAL_DATE_FORMAT_YY_MM_DD.format(cal.getTime()) + " 00:00:00").getTime() / 1000;
}
/**
* .
*
* @return 2011-12-31 23:59:59 .
*/
public static long getLastDayOfMonth(String year, String month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.valueOf(year));
cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, value);
return parseDate(NORMAL_DATE_FORMAT_YY_MM_DD.format(cal.getTime()) + " 23:59:59").getTime() / 1000;
}
/**
* Date.
*
* @param dateString
* 2011-12-17 17:41:18.843 CST.
* @return .null
*/
public static Date parseDate(String dateStr) {
try {
return NORMAL_DATE_FORMAT.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* .
*
* @param date
* 197011
* @return ()
*/
public static long parseLong(String date) {
return parseDate(date).getTime() / 1000;
}
/**
*
*
* @param date
* @return
*/
public static String parseString(Date date) {
return NORMAL_DATE_FORMAT_YY_MM_DD.format(date);
}
public static long getLastDayStartTime(long daytime) {
Calendar todayStart = Calendar.getInstance();
todayStart.setTimeInMillis(daytime * 1000);
todayStart.add(Calendar.DAY_OF_YEAR, -1);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTimeInMillis() / 1000;
}
public static long getLastDayEndTime(long daytime) {
Calendar todayEnd = Calendar.getInstance();
todayEnd.setTimeInMillis(daytime * 1000);
todayEnd.add(Calendar.DAY_OF_YEAR, -1);
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTimeInMillis() / 1000;
}
public static long getTime(long time) {
Calendar timeCalendar = Calendar.getInstance();
Calendar nowCalendar = Calendar.getInstance();
timeCalendar.setTimeInMillis(time * 1000);
timeCalendar.set(Calendar.HOUR_OF_DAY, nowCalendar.get(Calendar.HOUR_OF_DAY));
timeCalendar.set(Calendar.MINUTE, nowCalendar.get(Calendar.MINUTE));
timeCalendar.set(Calendar.SECOND, nowCalendar.get(Calendar.SECOND));
timeCalendar.set(Calendar.MILLISECOND, nowCalendar.get(Calendar.MILLISECOND));
return timeCalendar.getTimeInMillis() / 1000;
}
public static long getStartTime(long daytime) {
Calendar todayStart = Calendar.getInstance();
todayStart.setTimeInMillis(daytime * 1000);
// todayStart.add(Calendar.DAY_OF_YEAR, -1);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTime().getTime() / 1000;
}
public static long getEndTime(long daytime) {
Calendar todayEnd = Calendar.getInstance();
todayEnd.setTimeInMillis(daytime * 1000);
// todayEnd.add(Calendar.DAY_OF_YEAR, -1);
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTimeInMillis() / 1000;
}
/**
*
*
* @param d1
* @param d2
* @return
*/
public static boolean compareSameDate(Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
boolean isSameYear = c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR);
boolean isSameMonth = isSameYear && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH);
boolean isSameDate = isSameMonth && c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH);
return isSameDate;
}
/**
* 197011.
*
* @param date
* @return long
* @author mengxm
*/
public static final long firstMonthDayTime() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime().getTime() / 1000;
}
/**
*
*
* @param dateString
* @param fmtString
* @return Date
* @author mengxm
*/
public static final Date parse(String dateString, String fmtString) {
Date date = null;
try {
DateFormat format = new SimpleDateFormat(fmtString);
date = format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/** 比较提现前后时间间隔秒数 */
public static boolean getDatePoor(Date endDate, Date nowDate) {
// 获得两个时间的毫秒时间差异
System.out.println(endDate.getTime());
System.out.println(nowDate.getTime());
long diff = (endDate.getTime() - nowDate.getTime()) / 1000;
if (diff < 30) {
return true;
}
return false;
}
/**
*
*
* @param day
*
* @return
*/
public static long currentDateAddDay(int day) {
Calendar cal = Calendar.getInstance();
try {
cal.setTime(NORMAL_DATE_FORMAT_YY_MM_DD.parse(getCurrentTime()));
cal.add(Calendar.DATE, day); // add N days
return cal.getTimeInMillis() / 1000;
} catch (ParseException e) {
e.printStackTrace();
}
return 0l;
}
public static long dateAddDay(long dateLong, int day) {
Calendar cal = Calendar.getInstance();
Date d = new Date(dateLong * 1000);
cal.setTime(d);
cal.add(Calendar.DATE, day);
return cal.getTimeInMillis() / 1000;
}
/**
*
*
* @param smdate
*
* @param bdate
*
* @return
* @throws ParseException
*/
public static int daysBetween(Date smdate, Date bdate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
long between_days = 0L;
try {
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
between_days = (time2 - time1) / (1000 * 3600 * 24);
} catch (ParseException e) {
e.printStackTrace();
}
return Integer.parseInt(String.valueOf(between_days));
}
/** 日期转换为自定义格式输出 */
public static String DateToString(Date date, String formatType) {
if (date == null) {
return null;
}
if (formatType == null || "".equals(formatType)) {
return null;
}
String dateStr = "";
try {
SimpleDateFormat sdf = new SimpleDateFormat(formatType);
dateStr = sdf.format(date);
return dateStr;
} catch (Exception e) {
return null;
}
}
public static long getStartTimeCurrentDay() {
Calendar todayStart = Calendar.getInstance();
// todayStart.add(Calendar.DAY_OF_YEAR, -1);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTime().getTime() / 1000;
}
public static long getEndTimeCurrentDay() {
Calendar todayStart = Calendar.getInstance();
// todayStart.add(Calendar.DAY_OF_YEAR, -1);
todayStart.set(Calendar.HOUR_OF_DAY, 23);
todayStart.set(Calendar.MINUTE, 59);
todayStart.set(Calendar.SECOND, 59);
todayStart.set(Calendar.MILLISECOND, 59);
return todayStart.getTime().getTime() / 1000;
}
/** 日期转换为自定义格式输出 */
public static String fomatDate(Date date, String format) {
if (date == null) {
return "";
}
if (format == null || "".equals(format)) {
return "";
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static String getLastDayFmtYYYYMMDD() {
Calendar todayStart = Calendar.getInstance();
todayStart.add(Calendar.DAY_OF_YEAR, -1);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return NORMAL_DATE_FORMAT_YY_MM_DD_.format(todayStart.getTime());
}
/**
* @Title: getDateFormat
* @Description: yyyy-MM-dd
* @param str
* @return String
*/
public static String getDateFormat(String str) {
return dateFormatYYMMDD(Long.parseLong(str));
}
/**
* @Title: getTimeFormat
* @Description: yyyy-MM-dd HH:mm:ss
* @param str
* @return String
*/
public static String getTimeFormat(String str) {
return dateFormat(Long.parseLong(str));
}
public static void main(String[] args) {
System.out.println(dateFormat(1441036802));
System.out.println(getFirstDayOfMonth("2015", "9"));
System.out.println(getLastDayOfMonth("2015", "8"));
System.out.println(dateFormat(getLastMonthTime()));
System.out.println(dateFormat(getLastWeekTime()));
System.out.println(parseLong("2017-01-01 00:00:00"));
System.out.println(getTime());
System.out.println(dateFormat(1451624155));
System.out.println(parse("20151222", "yyyyMMdd"));
Calendar c = Calendar.getInstance();
Date nowDate = c.getTime();
c.set(Calendar.MINUTE, -1);
Date endDate = c.getTime();
System.out.println("nowDate--" + nowDate + ";endDate--" + endDate);
System.out.println(getDatePoor(nowDate, endDate));
System.out.println(dateFormatYYYYMMDD(new Date()));
System.out.println("args = [" + DateUtil.currentDateAddDay(0) + "]");
System.out.println("args = [" + DateUtil.parse("2016-01-19", "yyyy-MM-dd").getTime() + "]");
System.out.println(getTime());
System.out.println(getEndTimeCurrentDay());
System.out.println(getEndTimeCurrentDay() - getTime());
}
}

@ -0,0 +1,274 @@
package com.tamguo.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public static final String DEFAULT_PATTERN = "yyyy-MM-dd";
public static String getOneDayFromNow(int day, String pattern) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_MONTH, day);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(cal.getTime());
}
public static String getOneDayFromNow(int day) {
return DateUtils.getOneDayFromNow(day, DEFAULT_PATTERN);
}
/**
*
*
* @param smdate
*
* @param bdate
*
* @return
* @throws ParseException
* @throws java.text.ParseException
*/
public static int daysBetween(Date smdate, Date bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
public static void main(String[] args) throws ParseException {
System.out.println(daysBetween(null, null));
}
/**
* 2010-12-01
*/
public static String FORMAT_SHORT = "yyyy-MM-dd";
/**
* 2010-12-01 23:15:06
*/
public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
/**
* yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/**
* 20101201
*/
public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
/**
* 20101201 231506
*/
public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
/**
*
*/
public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
/**
* date pattern
*/
public static String getDatePattern() {
return FORMAT_LONG;
}
/**
*
*
* @return
*/
public static String getNow() {
return format(new Date());
}
/**
*
*
* @param format
* @return
*/
public static String getNow(String format) {
return format(new Date(), format);
}
/**
* 使
*
* @param date
* @return
*/
public static String format(Date date) {
return format(date, getDatePattern());
}
/**
* 使
*
* @param date
*
* @param pattern
*
* @return
*/
public static String format(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* 使
*
* @param timestamp
*
* @param pattern
*
* @return
*/
public static String format(String timestamp, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date(Long.parseLong(timestamp)));
}
/**
* 使
*
* @param strDate
*
* @return
*/
public static Date parse(String strDate) {
return parse(strDate, getDatePattern());
}
/**
* 使
*
* @param strDate
*
* @param pattern
*
* @return
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
*
*
* @param date
*
* @param n
*
* @return
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
/**
*
*
* @param date
*
* @param n
*
* @return
*/
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}
/**
*
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
/**
*
*
* @param date
*
* @return
*/
public static String getYear(Date date) {
return format(date).substring(0, 4);
}
/**
*
*
* @param date
*
* @return
*/
public static int countDays(String date) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
/**
*
*
* @param date
*
* @param format
*
* @return
*/
public static int countDays(String date, String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
public static String timeFormat(Date date, String format, Boolean flag, int beforeDay, int nowDay) {
if(date == null) {
date = new Date();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if(flag) {
cal.add(Calendar.DATE,-30);
} else {
cal.add(Calendar.DATE,0);
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(cal.getTime());
}
}

@ -0,0 +1,32 @@
package com.tamguo.common.utils;
/**
*
*
*/
public class ExceptionSupport {
private final static Result failResult = Result.failResult("500");
private static LogHandler handler = new Log4jHandler();
private final static String LOG_INFO_PREFIX = "--info>> ";
private final static String LOG_ERROR_PREFIX = "--error>> ";
public static Result resolverResult(String methodInfo, Class<?> clazz, Exception e) {
if(e instanceof CException) {
handler.info(formatInfoLevelMsg(methodInfo, e.getMessage()), clazz.getName());
return Result.failResult(e.getMessage());
}
handler.error(formatErrorLevelMsg(methodInfo), e, clazz.getName());
return failResult;
}
private static String formatInfoLevelMsg(String methodInfo, String infoMsg) {
return LOG_INFO_PREFIX + methodInfo + ": " + infoMsg;
}
private static String formatErrorLevelMsg(String methodInfo) {
return LOG_ERROR_PREFIX + methodInfo;
}
}

@ -0,0 +1,99 @@
package com.tamguo.common.utils;
public class IdGen
{
private long workerId;
private long datacenterId;
private long sequence = 0L;
private long twepoch = 1288834974657L;
private long workerIdBits = 5L;
private long datacenterIdBits = 5L;
private long maxWorkerId = -1L ^ (-1L << workerIdBits);
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
private long sequenceBits = 12L;
private long workerIdShift = sequenceBits;
private long datacenterIdShift = sequenceBits + workerIdBits;
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private long sequenceMask = -1L ^ (-1L << sequenceBits);
private long lastTimestamp = -1L;
private String suffix;
private boolean flag;
private static class IdGenHolder {
private static final IdGen instance = new IdGen();
}
public static IdGen get(){
return IdGenHolder.instance;
}
/**
* id
* @param suffix
* @param flag true: false:
* @return
*/
public static IdGen get(String suffix,boolean flag){
if(suffix == null || suffix.trim().length() == 0)
return IdGenHolder.instance;
else{
return new IdGen(suffix,flag);
}
}
public IdGen() {
this(0L, 0L);
}
public IdGen(String suffix,boolean flag){
this.suffix = suffix;
this.flag = flag;
}
public IdGen(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public synchronized String nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format(
"Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
long serialNumber = ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift)
| (workerId << workerIdShift) | sequence;
return (suffix == null || suffix.trim().length() == 0) ? serialNumber+"" : (flag ? (new StringBuffer()).append(suffix).append(serialNumber).toString() : (new StringBuffer()).append(serialNumber).append(suffix).toString());
}
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
protected long timeGen() {
return System.currentTimeMillis();
}
}

@ -0,0 +1,27 @@
package com.tamguo.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
public class Log4jHandler extends AbstractRunningLogHandler {
private static final Logger logger = LoggerFactory.getLogger(Log4jHandler.class);
public void info(String msg, String fqnOfCallingClass) {
logger.info(fqnOfCallingClass, Level.INFO, msg, null);
}
public void info(String msg, Throwable t, String fqnOfCallingClass) {
logger.info(fqnOfCallingClass, Level.INFO, msg, t);
}
public void error(String msg, String fqnOfCallingClass) {
logger.error(fqnOfCallingClass, Level.ERROR, msg, null);
}
public void error(String msg, Throwable t, String fqnOfCallingClass) {
logger.error(fqnOfCallingClass, Level.ERROR, msg, t);
}
}

@ -0,0 +1,17 @@
package com.tamguo.common.utils;
public class LogDebug {
private final static String DEBUG_LOG_KEY = "-- LogHandler: ";
public static void debug(String msg) {
System.err.println(DEBUG_LOG_KEY + msg);
}
public static void debug(String msg, Throwable t) {
System.err.println(DEBUG_LOG_KEY + msg);
if (t != null)
t.printStackTrace(System.err);
}
}

@ -0,0 +1,21 @@
package com.tamguo.common.utils;
public interface LogHandler {
public void info(String msg, String fqnOfCallingClass);
public void info(String msg, Throwable t, String fqnOfCallingClass);
public void error(String msg, String fqnOfCallingClass);
public void error(String msg, Throwable t, String fqnOfCallingClass);
public void debug(String msg, String fqnOfCallingClass);
public void debug(String msg, Throwable t, String fqnOfCallingClass);
public void warning(String msg, String fqnOfCallingClass);
public void warning(String msg, Throwable t, String fqnOfCallingClass);
}

@ -0,0 +1,69 @@
package com.tamguo.common.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectUtil extends SerializeTranscoder {
@Override
public byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] result = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(value);
os.close();
bos.close();
result = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return result;
}
@Override
public Object deserialize(byte[] in) {
Object result = null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
result = is.readObject();
is.close();
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
close(is);
close(bis);
}
return result;
}
public static boolean equals(Object o1, Object o2) {
if (o1 == o2) {
return true;
} else if (o1 == null || o2 == null) {
return false;
} else {
return o1.equals(o2);
}
}
}

@ -0,0 +1,108 @@
package com.tamguo.common.utils;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Result implements Serializable {
private static final long serialVersionUID = -1651614836984397356L;
private int code;
private Object result;
private String message;
public static final int SUCCESS_CODE = 0;
public static final int FAIL_CODE = 1;
private Result() {
}
private Result(int code, Object result, String message) {
this.code = code;
this.result = result;
this.message = message;
}
/**
*
*
* @param result
* @return
*/
public static Result successResult(Object result) {
return result(SUCCESS_CODE, result, "");
}
public static Result successResult(Object records, Long recordSum, Long rowsOfPage) {
return successResult(records, recordSum, rowsOfPage, null);
}
public static Result successResult(Object records, Long recordSum, Long rowsOfPage, Object userData) {
Map<String, Object> result = resultOfList(records, recordSum, rowsOfPage, userData);
return successResult(result);
}
public static Map<String, Object> resultOfList(Object records, Long recordSum, Long rowsOfPage) {
return resultOfList(records, recordSum, rowsOfPage, null);
}
public static Map<String, Object> resultOfList(Object Obj, Long records, Long rowsOfPage, Object userData) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("rows", Obj);
result.put("records", records);
result.put("total", rowsOfPage);
if (null != userData) {
result.put("userdata", userData);
}
;
return result;
}
public static Map<String, Object> jqGridResult(List<?> list, long totalCount, int pageSize, int currPage,
int totalPage) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("list", list);
result.put("count", totalCount);
result.put("next", currPage == totalPage ? currPage : currPage + 1);
result.put("prev", currPage == 1 ? 1 : currPage - 1);
result.put("first", 1);
result.put("last", totalPage);
result.put("pageSize", pageSize);
result.put("pageNo", currPage);
return result;
}
/**
*
*
* @param errorMsg
* @return
*/
public static Result failResult(String errorMsg) {
return result(FAIL_CODE, "", errorMsg);
}
public static Result result(int code, Object result, String message) {
Result res = new Result(code, result, message);
return res;
}
public int getCode() {
return code;
}
public Object getResult() {
return result;
}
public String getMessage() {
return message;
}
}

@ -0,0 +1,20 @@
package com.tamguo.common.utils;
import java.io.Closeable;
public abstract class SerializeTranscoder {
public abstract byte[] serialize(Object value);
public abstract Object deserialize(byte[] in);
public void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

@ -0,0 +1,68 @@
package com.tamguo.common.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class ShaEncrypt {
/**
* SHA-256
*
* @param strText
* @return
*/
public static String SHA256(final String strText) {
return SHA(strText, "SHA-256");
}
/**
* SHA-512
*
* @param strText
* @return
*/
public static String SHA512(final String strText) {
return SHA(strText, "SHA-512");
}
/**
* SHA
*
* @param strSourceText
* @return
*/
private static String SHA(final String strText, final String strType) {
// 返回值
String strResult = null;
// 是否是有效字符串
if (strText != null && strText.length() > 0) {
try {
// SHA 加密开始
// 创建加密对象 并傳入加密類型
MessageDigest messageDigest = MessageDigest.getInstance(strType);
// 传入要加密的字符串
messageDigest.update(strText.getBytes());
// 得到 byte 類型结果
byte byteBuffer[] = messageDigest.digest();
// 將 byte 轉換爲 string
StringBuffer strHexString = new StringBuffer();
// 遍歷 byte buffer
for (int i = 0; i < byteBuffer.length; i++) {
String hex = Integer.toHexString(0xff & byteBuffer[i]);
if (hex.length() == 1) {
strHexString.append('0');
}
strHexString.append(hex);
}
// 得到返回結果
strResult = strHexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return strResult;
}
}

@ -0,0 +1,13 @@
package com.tamguo.common.utils;
public class SystemConstant {
/** 初始密码*/
public static final String INIT_PASSWORD = "123456";
/** 验证码常数*/
public static final String KAPTCHA_SESSION_KEY = "KAPTCHA_SESSION_KEY";
/** 验证码常数*/
public static final String SYSTEM_USER_CODE = "system";
}

@ -0,0 +1,53 @@
package com.tamguo.common.utils;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class XMLConfiguration {
private Document document = null;
public boolean readConfigFile(String configFilename) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(configFilename);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (document == null) {
return false;
}
return true;
}
public boolean readConfigFile(InputStream stream) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(stream);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (document == null) {
return false;
}
return true;
}
public Document getDocument() {
return document;
}
protected void setDocument(Document document) {
this.document = document;
}
}
Loading…
Cancel
Save