博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
时间日期转换+两个日期相减
阅读量:7003 次
发布时间:2019-06-27

本文共 2575 字,大约阅读时间需要 8 分钟。

package m;

import java.text.ParseException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateT {

// date类型转换为String类型
// formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
// data Date类型的时间
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
// long类型转换为String类型
// currentTime要转换的long类型的时间
// formatType要转换的string类型的时间格式
public static String longToString(long currentTime, String formatType)
throws ParseException {
Date date = longToDate(currentTime, formatType); // long类型转成Date类型
String strTime = dateToString(date, formatType); // date类型转成String
return strTime;
}
// string类型转换为date类型
// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH时mm分ss秒,
// strTime的时间格式必须要与formatType的时间格式相同
public static Date stringToDate(String strTime, String formatType)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
// long转换为Date类型
// currentTime要转换的long类型的时间
// formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
public static Date longToDate(long currentTime, String formatType)
throws ParseException {
Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
return date;
}
// string类型转换为long类型
// strTime要转换的String类型的时间
// formatType时间格式
// strTime的时间格式和formatType的时间格式必须相同
public static long stringToLong(String strTime, String formatType)
throws ParseException {
Date date = stringToDate(strTime, formatType); // String类型转成date类型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date类型转成long类型
return currentTime;
}
}
// date类型转换为long类型
// date要转换的date类型的时间
public static long dateToLong(Date date) {
return date.getTime();
}

}

 

public static void main(String[] args)throws Exception {

long startT=DateT.stringToLong("2015-03-03 14:51:23","yyyy-MM-dd HH:mm:ss"); //定义上机时间
long endT=DateT.stringToLong("2014-12-03 13:50:23","yyyy-MM-dd HH:mm:ss"); //定义下机时间
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+" 毫秒");
}

 

来自:http://www.blogjava.net/weishuangshuang/archive/2012/09/27/388712.html

来自:http://blog.csdn.net/yuqinying112/article/details/7314278

 

转载于:https://www.cnblogs.com/lanliying/p/6210041.html

你可能感兴趣的文章
python中的常用模块(2)
查看>>
登陆的键盘敲击事件
查看>>
执行计划基础 统计信息
查看>>
python MD5加密方法
查看>>
mysql连接jdbc查询代码
查看>>
SpringMVC10数据验证
查看>>
处理异常Error resolving template [/login], template might not exist or might not be accessible by......
查看>>
洛谷 P1147 连续自然数和 Label:等差数列
查看>>
线程间的同步和通信机制
查看>>
Python脚本实现值更新事件赋值过程记录日志监控
查看>>
[bzoj 1503][NOI 2004]郁闷的出纳员
查看>>
Java课程上机实验1_ConnectionManager
查看>>
node.js中通过dgram数据报模块创建UDP服务器和客户端
查看>>
FZU Tic-Tac-Toe -.- FZU邀请赛 FZU 2283
查看>>
外痔田螺用法
查看>>
异常:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值...
查看>>
nginx访问静态文件配置
查看>>
mysql索引
查看>>
ubuntu server 14.04 上安装jdk1.8
查看>>
python file.tell() 在windows下需要注意的地方
查看>>