1、 时间转换类
//a: 将日期格式转换为13位
private long convertDate2Long(String str) {
java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-hh HH:mm:ss");
long date = 0L;
try {
Date d = format.parsr(str); //时间格式必须一致,否则会提示错误
date = d.getTime(); //如果为10位,则d.getTime() / 1000L
}catch(Exception e) {
throw new RuntimeException("***");
}
return date;
}
//b: 将13位转换为日期格式
private String convertTimeToString(long longTime, String format) { //format: "yyyy-MM-dd HH:mm:ss"
java.sql.Timestamp ts;
try {
ts = new Timestamp(longTime); //如果是10位,此处longTime * 1000L
SimpleDateFormat s = new SimpleDateFormat(format);
return s.format(t);
} catch(Exception e) {
throw new :::;
}
}
2、 创建文件唯一名称
String uuid = UUID.randomUUID().toString();
3、二进制、八进制、十进制、十六进制转换
public static void main(String[] args) {
// 不管是哪种进制的数据赋值, 在计算机中都是二进制, 所以i和j在计算机中存储方式是一样的
int i = 0xff;
System.out.println("Hex = " + Integer.toHexString(i));
System.out.println("Octal = " + Integer.toOctalString(i));
System.out.println("Binary = " + Integer.toBinaryString(i));
System.out.println("Decimal = " + i);
int j = 0377;
System.out.println("Hex = " + Integer.toHexString(j));
System.out.println("Octal = " + Integer.toOctalString(j));
System.out.println("Binary = " + Integer.toBinaryString(j));
System.out.println("Decimal = " + j);
}
//以下是程序运行结果
Hex = ff
Octal = 377
Binary = 11111111;
Decimal = 255
Hex = ff
Octal = 377
Binary = 11111111;
Decimal = 255
4、 java SimpleDateFormat校验日期格式是否符合自定义格式
/**
* 功能描述: <br>
* 校验是否是正规的时间字符串
*
* @param dateStr 2019-09-09
* @param format yyyy-MM-dd格式最好全面点
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static final boolean justifyLegalDateStr(String dateStr, String format) {
DateFormat dateFormat = new SimpleDateFormat(format);
try {
Date date = dateFormat.parse(dateStr);
dateFormat.setLenient(false);
return dateStr.equalsIgnoreCase(dateFormat.format(date));
} catch (ParseException e) {
logger.error(e.getMessage(), e);
return false;
}
}