1:将带宽流量转换单位
package com.chinacache.snp.TransferChannelAlarm.util;
import java.text.DecimalFormat;
import java.text.Format;
public class FormatToSuitableFluxUnit {
private static final long K_BIT = 1000;
private static final long M_BIT = 1000 * 1000;
private static final long G_BIT = 1000 * 1000 * 1000;
private static final long T_BIT = 1000 * 1000 * 1000 * 1000L;
private static final long P_BIT = 1000 * 1000 * 1000 * 1000L * 1000L;
private static final long E_BIT = 1000 * 1000 * 1000 * 1000L * 1000L * 1000L;
public static final String BIT_STR = "Byte";
public static final String K_BIT_STR = "KB";
public static final String M_BIT_STR = "MB";
public static final String G_BIT_STR = "GB";
public static final String T_BIT_STR = "TB";
public static final String P_BIT_STR = "PB";
public static final String E_BIT_STR = "EB";
/**
* 将以bit表示的带宽,转换为适合显示的带宽单位,精确到小数点后两位
*/
public static String getSuitableFluxRepresentation(long fluxInBit) {
// 保留小数点后面两位
Format format = new DecimalFormat("#.00");
if (fluxInBit >= E_BIT) {
return format.format((double) fluxInBit / E_BIT) + " " + E_BIT_STR;
} else if (fluxInBit >= P_BIT) {
return format.format((double) fluxInBit / P_BIT) + " " + P_BIT_STR;
} else if (fluxInBit >= T_BIT) {
return format.format((double) fluxInBit / T_BIT) + " " + T_BIT_STR;
} else if (fluxInBit >= G_BIT) {
return format.format((double) fluxInBit / G_BIT) + " " + G_BIT_STR;
} else if (fluxInBit >= M_BIT) {
return format.format((double) fluxInBit / M_BIT) + " " + M_BIT_STR;
} else if (fluxInBit >= K_BIT) {
return format.format((double) fluxInBit / K_BIT) + " " + K_BIT_STR;
} else {
return fluxInBit +" "+ BIT_STR;
}
}
/**
* 获取合适的带宽单位
*/
public static String getSuitableFluxUnit(long fluxInBit) {
if (fluxInBit >= E_BIT) {
return E_BIT_STR;
} else if (fluxInBit >= P_BIT) {
return P_BIT_STR;
} else if (fluxInBit >= T_BIT) {
return T_BIT_STR;
} else if (fluxInBit >= G_BIT) {
return G_BIT_STR;
} else if (fluxInBit >= M_BIT) {
return M_BIT_STR;
} else if (fluxInBit >= K_BIT) {
return K_BIT_STR;
} else {
return BIT_STR;
}
}
}
2:对list分组(前n个list大小为groupSize)
package com.chinacache.snp.bill.datacontrast.util;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
public class GroupUtil {
public static List<List<String>> divideIntoGroups(List<String> list, int groupSize) {
if (CollectionUtils.isEmpty(list)) {
return null;
}
List<List<String>> groups = new ArrayList<List<String>>();
List<String> group = null;
int totalSize = list.size();
for (int i = 0; i < totalSize; i++) {
if (i % groupSize == 0) {
group = new ArrayList<String>();
groups.add(group);
}
group.add(list.get(i));
}
return groups;
}
}
3:java时间区
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //格式日期
TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai"); //时区
sdf.setTimeZone(timezone );
System.out.println(sdf.format(new Date()));
4:读取properties配置文件的常量类
父类:
package com.chinacache.snp.TransferChannelAlarm.util;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;
/**
* 常量配置类,从指定文件读取常量
*
*/
public class ConfigurableContants {
private static final Logger logger = Logger.getLogger(ConfigurableContants.class);
protected static Properties p = new Properties();
protected static void init(String propertyFileName) {
InputStream in = null;
try {
in = ConfigurableContants.class.getResourceAsStream(propertyFileName);
if (in != null)
p.load(in);
} catch (IOException e) {
logger.error("load " + propertyFileName + " into Contants error");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
protected static String getProperty(String key, String defaultValue) {
return p.getProperty(key, defaultValue);
}
protected static String getProperty(String key) {
return getProperty(key, "");
}
/**
* 取以","分割的集合属性
*
* @param key
* @param defaultValue
* @return
*/
protected static Set<String> getSetProperty(String key, String defaultValue) {
String[] strings = p.getProperty(key, defaultValue).split(",");
HashSet<String> hashSet = new HashSet<String>(strings.length);
for (String string : strings) {
hashSet.add(string.trim());
}
return hashSet;
}
protected static Set<String> getSetProperty(String key) {
return getSetProperty(key, "");
}
protected static Date getPropertyWithDateFormate(String key, String dateFormate) {
String str = null;
Date date = null;
try {
str = getProperty(key, "");
date = new SimpleDateFormat(dateFormate).parse(str);
} catch (ParseException e) {
logger.error("Date [" + str + "] Error in Contants");
}
return date;
}
}
子类(每个配置文件一个子类)
package com.chinacache.snp.TransferChannelAlarm.util;
/**
* 常量类,包括配置文件的常量和程序定义的常量
*
*/
public final class SystemConstants extends ConfigurableContants {
static {
init("/system.properties");
}
public static final String thresholdValue = getProperty("report.threshold", "1");
public static final String channelState = getProperty("channel.state", "TEST,TRANSFER");
public static final String topSize = getProperty("channel.top.n", "1000");
public static final String mailServer = getProperty("email.server", "");
public static final String addresserUser = getProperty("addresser.user", "");
public static final String addresserPass = getProperty("addresser.pass", "");
public static final String cc = getProperty("cc", "baozong.gao@chinacache.com");
public static String getStringByKey(String key) {
return getProperty(key);
}
}
properties配置文件
report.threshold=0
channel.top.n=100000000
#channel.state=COMMERCIAL,TRANSFER
channel.state=TEST,COMMERCIAL,TRANSFER
TEST=测试
TRANSFER=转走
COMMERCIAL=商用
email.server=corp.chinacache.com
addresser.user=
addresser.pass=
cc=baozong.gao@chinacache.com
channel.top.n=100000000
#channel.state=COMMERCIAL,TRANSFER
channel.state=TEST,COMMERCIAL,TRANSFER
TEST=测试
TRANSFER=转走
COMMERCIAL=商用
email.server=corp.chinacache.com
addresser.user=
addresser.pass=
cc=baozong.gao@chinacache.com