java时间方面的知识点

来源:互联网 发布:经典java编程面试题 编辑:程序博客网 时间:2024/05/22 22:18

延时

Thread.sleep(10000); //单位ms

获取系统时间戳用来计时

long start = System.currentTimeMillis();//返回当前系统时间(自1970年),单位:毫秒 测试代码 long end = System.currentTimeMillis();   long costTime = end - start;        System.out.println("cost: " + costTime + "ms");


(粗略计时,不考虑系统的繁忙)


时间戳转换成日期时间

long time = System.currentTimeMillis();  //(粗略计时,不考虑系统的繁忙)SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //yyyy-MM-dd hh:mm:ssString date = sdf.format(new Date(time)); // Date(毫秒数)System.out.println(date);

将毫秒转换成天、小时、分钟、秒

/** * 将毫秒转换成小时、分钟、秒(是否可以输出毫秒?) * @param time 毫秒 */public static String ms2HHMMSS(long time) {long milliseconds = time % 1000; //毫秒long seconds = Math.round(time / 1000.0);long minutes = seconds / 60;seconds %= 60;long hours = minutes / 60;minutes %= 60;long days = hours / 24;hours %= 24;StringBuilder timeReport = new StringBuilder();timeReport.append("\nTotal time: ");if (days != 0) {timeReport.append(days);timeReport.append(" days ");}if (hours != 0) {timeReport.append(hours);timeReport.append(" hours ");}if (minutes != 0) {timeReport.append(minutes);timeReport.append(" minutes ");}if(seconds !=0){timeReport.append(seconds);timeReport.append(" seconds ");}timeReport.append(milliseconds);timeReport.append(" ms");//System.out.println(timeReport);return timeReport.toString();}

JAVA如何插入MySql的datetime类型

使用java Timestamp类型
Date date = new Date();Timestamp timeStamp = new Timestamp(date.getTime());// tb_blessing(id,sender, receiver, content,time);dd.insert("insert into tb_blessing(sender, receiver, content,time)" + " values(? , ? , ?, ?)",getSender(), getReceiver(), getContent(), timeStamp);

0 0
原创粉丝点击