Java 得到当前系统时间,时间转字符串,字符串转时间,以及求时间差

`//时间转字符串
//截取当前系统时间
Date currentTime = new Date();
//改变输出格式(自己想要的格式)
SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
//得到字符串时间
String s8 = formatter.format(currentTime);
//字符串转时间
DateFormat fmt =new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
try {
n = fmt.parse(D.getDrutime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DateFormat fm =new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
try {
m = fm.parse(s8);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//字符串转double类型;
double danjia=Double.valueOf(s4).doubleValue();
//计算两个时间差
long between=(m.getTime()-n.getTime())/1000;
//除以1000是为了转换成秒
long day=between/(24*3600);
if(day>=0) {
if(day>0) {

            String x=""+day*danjia;
            jTextField3.setText(x);
        }else {
            String x=""+danjia;
            jTextField3.setText(x);
        }
    }else {
        JOptionPane.showMessageDialog(new JPanel(), "时间错误", "错误",JOptionPane.WARNING_MESSAGE);
    } `

猜你喜欢

转载自blog.csdn.net/qq_41441312/article/details/78835135