package com.qg.lb.comm;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
public class MyUtil
{
public static void Log(String strMsg)
{
if (Config.Debug)
System.out.println(strMsg);
}
/**
* 生成[x, y]之間的隨機(jī)數(shù)
*
* @return [x, y]之間的隨機(jī)數(shù)
*/
public static Integer getRd(Integer start, Integer end)
{
int num = (int) (Math.random() * (end - start + 1) + start);
return num;
}
// 返回百分比概率
public static boolean GetPercentage(int per)
{
if (per <= 0)
return false;
if (per >= 100)
return true;
int num = MyUtil.getRd(1, 100);
return num >= 1 && num <= per;
}
public static String MakeToken()
{
return UUID.randomUUID().toString();
}
// 獲取當(dāng)前時(shí)間
public static String GetNow()
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
return format.format(date);
}
// 計(jì)算幾天后的時(shí)間
public static String AfterTimes(String cur, int days)
{
// 對當(dāng)前時(shí)間進(jìn)行轉(zhuǎn)換 括號(hào)里面的是轉(zhuǎn)換規(guī)則("yyyy-MM-dd HH:mm:ss")
//? ? ? ? SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//? ? ? ? Date date = new Date();
//? ? ? ? date.setTime(System.currentTimeMillis());
//? ? ? ? Calendar c = Calendar.getInstance();
//? ? ? ? c.setTime(date);
//? ? ? ? //5是傳的參數(shù) 就是說要獲取幾天后的當(dāng)前具體時(shí)間
//? ? ? ? c.add(Calendar.DATE, days);
//? ? ? ? Date d3 = c.getTime();
//? ? ? ? String date3 = format.format(d3);
//? ? ? ? return date3;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
Date date = format.parse(cur);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, days);
Date d3 = c.getTime();
String date3 = format.format(d3);
return date3;
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
// 計(jì)算幾天后的日期
public static String AfterDays(int days)
{
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String currdate = format.format(d);
Calendar ca = Calendar.getInstance();
try
{
ca.setTime(format.parse(currdate));
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
ca.add(Calendar.DATE, days);// num為增加的天數(shù)德频,可以改變的
d = ca.getTime();
String enddate = format.format(d);
return enddate;
}
// 是否在天數(shù)內(nèi)
public static boolean WithinTheLimitOfDay(String begin, String end)
{
if (begin.equals("") || end.equals(""))
return false;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String str1 = begin;
String str2 = end;
Date date1;
Date date2;
try
{
date1 = df.parse(str1);
date2 = df.parse(str2);
String now = df.format(new Date());
Date dateNow = df.parse(now);
if (date1.getTime() <= dateNow.getTime() && date2.getTime() >= dateNow.getTime())
{
return true;
}
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
// 是否在時(shí)間內(nèi)
public static boolean WithinTheLimitOfTime(String begin, String end)
{
if (begin.equals("") || end.equals(""))
return false;
DateFormat df = new SimpleDateFormat("HH:mm");
String str1 = begin;
String str2 = end;
Date date1;
Date date2;
try
{
date1 = df.parse(str1);
date2 = df.parse(str2);
String now = df.format(new Date());
Date dateNow = df.parse(now);
if (date1.getTime() <= dateNow.getTime() && date2.getTime() >= dateNow.getTime())
{
return true;
}
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
// 是否在日期內(nèi)
public static boolean WithinTheLimitOfDate(String begin, String end)
{
if (begin.equals("") || end.equals(""))
return false;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String str1 = begin;
String str2 = end;
Date date1;
Date date2;
try
{
date1 = df.parse(str1);
date2 = df.parse(str2);
String now = df.format(new Date());
Date dateNow = df.parse(now);
if (date1.getTime() <= dateNow.getTime() && date2.getTime() >= dateNow.getTime())
{
return true;
}
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static String GetToday()
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String today = df.format(new Date());
return today;
}
public static String ToDate(String tm)
{
if (!tm.equals(""))
{
String[] sp = tm.split(" ");
if (sp.length > 1)
{
return sp[0];
}
}
return "";
}
// list 深拷貝
public static <T> List<T> DeepCpy(List<T> src) throws IOException, ClassNotFoundException
{
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List<T> copy_list = (List<T>) in.readObject();
return copy_list;
}
public static String GetInviteCode()
{
int maxNum = 36;
int i;
int count = 0;
char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while (count < 8)
{
i = Math.abs(r.nextInt(maxNum));
if (i >= 0 && i < str.length)
{
pwd.append(str[i]);
count++;
}
}
return pwd.toString();
}
}