1.输入中文转换成拼音首字母。
需要的maven 工具包
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
/**
* 输入中文字符串转换获得中文首字母
* @param str 输入中文字符串
* @return 首字母
*/
public static String getPinYinHeadChar(String str) {
StringBuilder convert = new StringBuilder();
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert.append(pinyinArray[0].charAt(0));
} else {
convert.append(word);
}
}
return convert.toString();
}
2.随机获得指定长度的数字
public static String create_nonce_code(int len) {
String chars = "0123456789";
StringBuilder res = new StringBuilder();
for (int i = 0; i < len; i++) {
Random rd = new Random();
res.append(chars.charAt(rd.nextInt(chars.length() - 1)));
}
return res.toString();
}
3.字符串转输入流InputStream
public static InputStream write(String data){
InputStream input = null;
try {
input = new ByteArrayInputStream(data.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
log.error("文件写入异常:",e);
}
return input;
}
4.获得当前时间的前几个小时或者后几个小时,和当前时间的前后几分钟
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
/**
* 获取当前时间之前或之后几小时 hour 正数为后几小时,负数为前几小时
* @param hour
* @return
*/
public static String getTimeByHour(int hour) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
return sdf.format(calendar.getTime());
}
/**
* 获取当前时间之前或之后几分钟 minute 正数为后几分钟,负数为前几分钟
* @param minute
* @return
*/
public static String getTimeByMinute(int minute) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, minute);
return sdf.format(calendar.getTime());
}
5.获得当前时间的月初日期和月末日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String firstday, lastday;
Calendar cale = Calendar.getInstance();
// 获取当前月的第一天
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
firstday = format.format(cale.getTime())+" 00:00:00";
// 获取当前月的最后一天
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = format.format(cale.getTime())+" 23:59:59";
6.list集合倒序排序
List<String> list = new ArrayList<String>();
list.add("111");
list.add("222");
list.add("333");
Collections.reverse(list);//对list集合中的数据进行倒叙排序