时间日期转换+两个日期相减
2016-12-22 10:13
274 查看
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
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
相关文章推荐
- JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)
- jQuery 两个日期时间相减
- 日期与时间的转换,计算两个日期间的天数,月数
- 日期处理 yy-mm-dd HH:mm:ss时间格式转换毫秒,两个时间格式毫秒数相减,返回“xx天x
- java 关于时间的转换,两个时间日期的比较
- 两个日期相减的时间
- 日期字符串转换 and 两个日期相减
- C/C++中的日期和时间 time_t与struct tm转换[转]
- 两个javascript 日期、时间 输入的控件
- Python时间,日期,时间戳之间转换
- java 时间戳和日期的相互转换
- 实现传入两个字符串类型的时间的相减
- Mysql日期差函数,Mysql选择两个日期字段相差大于或小于一定时间
- Python时间,日期,时间戳之间转换
- sqlserver 日期时间格式加减 转换
- Python时间,日期,时间戳之间转换
- .Net与JS时间日期格式的转换问题对比分析
- poj 3751时间日期格式转换
- mysql 函数日期 两个日期相减 得到相差几天
- vue-cli项目中将时间戳的日期格式转换成年月日(时分秒)