空白 或 空白 或 空白 或 以上用于提示首行缩进,与内容无关。
参考: 以上为参考链接,与内容无关。
1、需要当前时间,要“年月日时分秒”形式; 2、还需要对时间进行减法得到时间差; 3、可以定义精确到几位小数。
比较简单,详见代码,内有丰富注释
package test2Package; import java.sql.Timestamp; import java.text.DecimalFormat; import java.text.SimpleDateFormat; public class test2 { public static void main(String args[]) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//大小写皆有意义,不可改动 Timestamp time = new Timestamp(System.currentTimeMillis());//获取当前系统的时间 String str = df.format(time);//timestamp转string System.out.println(str); //按设置的格式输出年月日时分秒 System.out.println(time);//按函数库里原本的格式输出年月日时分秒 //自行任意设置时间戳 Timestamp a = Timestamp.valueOf("2018-05-23 10:54:44"); Timestamp b = Timestamp.valueOf("2018-05-22 13:50:30"); //时间戳之间相减并打出来 long c = a.getTime()-b.getTime();//得到差值,且差值默认为"微秒"形式 float d = c;//若不转float,将致差值不带小数 System.out.println(d);//输出差值 float longs = d / (1000*60*60);//"微秒"->"小时" //设置显示数字的小数有效位 DecimalFormat decimalFormat = new DecimalFormat("#.00");//保留2位小数 String longss = decimalFormat.format(longs); System.out.println("时间"+longss);//输出保留2位小数的差值(小时为单位) //timestamp->string->timestamp->打印至consloe String time2 = df.format(time); Timestamp ts = Timestamp.valueOf(time2); System.out.println(ts); } }1、可以获取时间; 2、可以按格式定义时间; 3、可以进行减法; 4、可以设置精确到几位小数。
