1.时间相减

import java.util.Date;

import com.ibm.icu.text.SimpleDateFormat;

public class mss {
 
  public mss() {   
  } 

  public long fromDateStringToLong(String inVal) { //此方法计算时间毫秒
  Date date = null;   //定义时间类型       
  SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:ss"); 
  try { 
  date = inputFormat.parse(inVal); //将字符型转换成日期型
  } catch (Exception e) { 
  e.printStackTrace(); 
  } 
  return date.getTime();   //返回毫秒数
  } 

  private static  String dqsj() {  //此方法用于获得当前系统时间(格式类型2007-11-6 15:10:58)
   Date date = new Date();  //实例化日期类型
   String today = date.toLocaleString(); //获取当前时间
   System.out.println("获得当前系统时间 "+today);  //显示
   return today;  //返回当前时间
  }

  public static void main(String[] args) { 
  String dqsj = dqsj();   //获得String dqsj = dqsj();   //获得当前系统时间
  mss df = new mss();  //实例化方法

  long startT=df.fromDateStringToLong("2005-03-03 14:51:23"); //定义上机时间
  long endT=df.fromDateStringToLong("2004-03-03 13:50:23");  //定义下机时间

  long ss=(startT-endT)/(1000); //共计秒数
  int MM = (int)ss/60;   //共计分钟数
  int hh=(int)ss/3600;  //共计小时数
  int dd=(int)hh/24;   //共计天数

  System.out.println("共"+dd+"天 准确时间是:"+hh+" 小时 "+MM+" 分钟"+ss+" 秒 共计:"+ss*1000+" 毫秒"); 
  
  } 

2.时间格式

SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyy-MM-dd hh:mm:ss");     
String   date   =   sDateFormat.format(new   java.util.Date()); 

相关文章:

  • 2022-01-05
  • 2021-08-10
  • 2021-11-18
  • 2022-03-02
  • 2022-12-23
  • 2021-08-02
  • 2021-09-20
猜你喜欢
  • 2022-12-23
  • 2021-10-03
  • 2022-01-01
  • 2021-11-21
  • 2021-12-15
  • 2021-12-27
相关资源
相似解决方案