import java.sql.Clob;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import bsh.StringUtil;
import com.gsoft.cos.core.util.Assert;
import com.gsoft.cos.core.util.BeanUtils;
public class Util {
public static String removeZero(double b){
String s = b + "";
if(s.indexOf(".") > 0){
//正则表达
s = s.replaceAll("0+?$", "");//去掉后面无用的零
s = s.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
}
return s;
}
public static int getYear(){
Calendar c = Calendar.getInstance();
return c.get(Calendar.YEAR);
}
public static int getMonth(){
Calendar c = Calendar.getInstance();
return c.get(Calendar.MONTH) + 1;
}
public static int getDay(){
Calendar c = Calendar.getInstance();
return c.get(Calendar.DATE);
}
public static String getNowYmd(){
Calendar c = Calendar.getInstance();
return c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH) + 1)+"-"+c.get(Calendar.DATE);
}
public static boolean isNumeric(String str){
String reg = "^[0-9]+(.[0-9]+)?$";
return str.matches(reg);
}
/**
* @param date1 需要比较的时间 不能为空(null),需要正确的日期格式
* @param date2 被比较的时间 为空(null)则为当前时间
* @param stype 返回值类型 0为多少天,1为多少个月,2为多少年
* @return
*/
public static int compareDate(String date1,String date2,int stype){
int n = 0;
@SuppressWarnings("unused")
String[] u = {"天","月","年"};
String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd";
date2 = date2==null?GgczUtil.getCurrentDate():date2;
DateFormat df = new SimpleDateFormat(formatStyle);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
try {
c1.setTime(df.parse(date1));
c2.setTime(df.parse(date2));
} catch (Exception e3) {
System.out.println("wrong occured");
}
//List list = new ArrayList();
while (!c1.after(c2)) { // 循环对比,直到相等,n 就是所要的结果
//list.add(df.format(c1.getTime())); // 这里可以把间隔的日期存到数组中 打印出来
n++;
if(stype==1){
c1.add(Calendar.MONTH, 1); // 比较月份,月份+1
}
else{
c1.add(Calendar.DATE, 1); // 比较天数,日期+1
}
}
n = n-1;
if(stype==2){
n = (int)n/365;
}
return n;
}
/**
* 得到当前日期
* @return
*/
public static String getCurrentDate() {
Calendar c = Calendar.getInstance();
Date date = c.getTime();
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
return simple.format(date);
}
public static String getDateString(Date date,String str){
if(Assert.isNotEmpty(date)){
String dateStr=new SimpleDateFormat(str).format(date);
return dateStr;
}
return null;
}
public static Date getDate(Date date,String str){
SimpleDateFormat sf=new SimpleDateFormat(str);
String s=sf.format(date);
Date newdate=null;
try {
newdate = sf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return newdate;
}
public static int dayDydyxy(String str,String str1){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date dt2= null;
Date dt1 = null;
try {
dt1 = df.parse(str);
dt2 = df.parse(str1);
if (dt1.getTime() > dt2.getTime()) {
return 2;
}else if (dt1.getTime() < dt2.getTime()) {
return 1;
}else{
return 0;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
public static Date getDate(String date,String str){
SimpleDateFormat sdf=new SimpleDateFormat(str);
Date newdate=null;
try {
newdate = (Date) sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return newdate;
}
public static String getDateToString(String date,String str) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat(str);
Date dateStr=null;
try {
dateStr = dateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormat.format(dateStr);
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static List< Map<String, Object>> listToMap(List<?> list){
List<Map<String, Object>> lm=new ArrayList<Map<String, Object>>();
for (int i = 0; i < list.size(); i++) {
Map parameter = new HashMap<String, Object>();
try {
parameter = BeanUtils.describe(list.get(i));
} catch (Exception e) {
e.printStackTrace();
}
lm.add(parameter);
}
return lm;
}
public static int daysBetween(Date early, Date late) {
java.util.Calendar calst = java.util.Calendar.getInstance();
java.util.Calendar caled = java.util.Calendar.getInstance();
calst.setTime(early);
caled.setTime(late);
//设置时间为0时
calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
calst.set(java.util.Calendar.MINUTE, 0);
calst.set(java.util.Calendar.SECOND, 0);
caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
caled.set(java.util.Calendar.MINUTE, 0);
caled.set(java.util.Calendar.SECOND, 0);
//得到两个日期相差的天数
int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst
.getTime().getTime() / 1000)) / 3600 / 24;
if(calst.getTime().getTime()<caled.getTime().getTime()){
days=-days;
}
return days;
}
public static boolean isInteger(String str) {// 判断整型
return str.matches("^\\d+$$");
}
public static boolean isNumber(String str) {// 判断整数或double
if(isInteger(str)||isDouble(str)){
return true;
}
return false;
}
public static boolean isDouble(String str) {// 判断小数,与判断整型的区别在与d后面的小数点(红色)
return str.matches("\\d+\\.\\d+$");
}
public static boolean isStartPointNubmer(String str) {// 判断小数点开头
return str.matches("\\.\\d+$");
}
public static String getDoubleRound(String str){
if(Assert.isNotEmpty(str)){
return String.format("%.2f", Double.parseDouble(str));
}
return String.format("%.2f", Double.parseDouble("0.00"));
}
/**
* 根据数组符号获得符号间隔字符串
* @param args
* @param symbol
* @return
*/
public static String getStringToSymbol(String[] args,String symbol){
String newSymbol=",";
if(Assert.isNotEmpty(symbol)){
newSymbol=symbol;
}
if(Assert.isNotEmpty(args)){
String str="";
for (String string : args) {
str+=string+newSymbol;
}
return str.substring(0,str.length()-1);
}
return "";
}
/**
* 根据数组符号获得符号间隔字符串
* @param args
* @param symbol
* @return
*/
public static String getStringToSymbol(Integer[] args,String symbol){
String newSymbol=",";
if(Assert.isNotEmpty(symbol)){
newSymbol=symbol;
}
if(Assert.isNotEmpty(args)){
String str="";
for (Integer string : args) {
str+=string+newSymbol;
}
return str.substring(0,str.length()-1);
}
return "";
}
/**
* 判断存在
* @param a
* @param b
* @return
*/
public static boolean isContains(String a,String b){
boolean isTrue=false;
if((","+a+",").contains(","+b+",")){
isTrue=true;
}
return isTrue;
}
public static final int YEAR_RETURN = 0;
public static final int MONTH_RETURN = 1;
public static final int DAY_RETURN = 2;
public static final int HOUR_RETURN= 3;
public static final int MINUTE_RETURN = 4;
public static final int SECOND_RETURN = 5;
public static final String YYYY = "yyyy";
public static final String YYYYMM = "yyyy-MM";
public static final String YYYYMMDD = "yyyy-MM-dd";
public static final String YYYYMMDDHH= "yyyy-MM-dd HH";
public static final String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
public static long getBetween(String beginTime, String endTime, String formatPattern, int returnPattern){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);
Date beginDate=new Date();
Date endDate=new Date();
try {
beginDate = simpleDateFormat.parse(beginTime);
endDate = simpleDateFormat.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar beginCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
beginCalendar.setTime(beginDate);
endCalendar.setTime(endDate);
switch (returnPattern) {
case YEAR_RETURN:
return GgczUtil.getByField(beginCalendar, endCalendar, Calendar.YEAR);
case MONTH_RETURN:
return GgczUtil.getByField(beginCalendar, endCalendar, Calendar.YEAR)*12 + GgczUtil.getByField(beginCalendar, endCalendar, Calendar.MONTH);
case DAY_RETURN:
return GgczUtil.getTime(beginDate, endDate)/(24*60*60*1000);
case HOUR_RETURN:
return GgczUtil.getTime(beginDate, endDate)/(60*60*1000);
case MINUTE_RETURN:
return GgczUtil.getTime(beginDate, endDate)/(60*1000);
case SECOND_RETURN:
return GgczUtil.getTime(beginDate, endDate)/1000;
default:
return 0;
}
}
private static long getByField(Calendar beginCalendar, Calendar endCalendar, int calendarField){
return endCalendar.get(calendarField) - beginCalendar.get(calendarField);
}
private static long getTime(Date beginDate, Date endDate){
return endDate.getTime() - beginDate.getTime();
}
/**
* 根据年 月 获取对应的月份 天数
* */
public static int getDaysByYearMonth(int year, int month) {
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* 去空格
* @param str
* @return
*/
public static String removeSpace(String str){
if(Assert.isNotEmpty(str)){
return str.replaceAll("\\s*", "");
}
return "";
}
public static String getOrder(String qz,String order){
String [] str=StringUtil.split(order, ",");
String newOrderBy="";
for (int i = 0; i < str.length; i++) {
if(i==(str.length-1)){
newOrderBy+=qz+str[i];
}else{
newOrderBy+=qz+str[i]+",";
}
}
return newOrderBy;
}
public static List<Long> getLongList(String str,String s){
if(Assert.isNotEmpty(str)){
String [] strs= str.split(s);
List<Long> list= new ArrayList<Long>();
for (String ss : strs) {
list.add(Long.valueOf(ss));
}
return list;
}
return null;
}
public static List<String> getStringList(String str,String s){
if(Assert.isNotEmpty(str)){
String [] strs= str.split(s);
List<String> list= new ArrayList<String>();
for (String ss : strs) {
list.add(ss);
}
return list;
}
return null;
}
public static String doubleToString(Double d){
if(d != null){
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setGroupingUsed(false);
return nf.format(d);
}else{
return "0";
}
}
public static String doubleToString1(Double d){
if(d != null){
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");//格式化设置
return decimalFormat.format(d);
}else{
return "0";
}
}
public static String oracleClob2Str(Clob clob) throws Exception {
return (clob != null ? clob.getSubString(1, (int) clob.length()) : null);
}
/**
* 将String转成Clob ,静态方法
*
* @param str
* 字段
* @return clob对象,如果出现错误,返回 null
*/
public static Clob stringToClob(String str) {
if (null == str)
return null;
else {
try {
java.sql.Clob c = new javax.sql.rowset.serial.SerialClob(str
.toCharArray());
return c;
} catch (Exception e) {
return null;
}
}
}
public static String returnWHStr(String str){
String[] exportIds = str.split(",");
String strs = "";
for (int i = 0; i < exportIds.length; i++) {
if(i==(exportIds.length-1)){
strs += "?";
}else{
strs += "?,";
}
}
return strs;
}
}