CommonUtils(一)

来源:互联网 发布:c语言void的应用 编辑:程序博客网 时间:2024/06/06 14:01

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 = ffOctal = 377Binary = 11111111;Decimal = 255Hex = ffOctal = 377Binary = 11111111;Decimal = 255
原创粉丝点击