跟日期 时间有关的计算与比较
来源:互联网 发布:新疆移动4g网络什么时候开通 编辑:程序博客网 时间:2024/05/17 20:51
/**
* 计算N年之后的日期
* */
public Date calDateByNum(int num){
Date date = new Date();
Calendar cld = Calendar.getInstance();
cld.setTime(date);
cld.add(Calendar.YEAR, num);
return cld.getTime();
}
/**
* 时间差转为小时数
* @throws ParseException
*/
public double calDateToHours(String beg,String end) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse(beg);
Date d2 = sdf.parse(end);
d1.setYear(2015);
d1.setMonth(11);
d1.setDate(11);
d2.setYear(2015);
d2.setMonth(11);
d2.setDate(11);
long m1 = d1.getTime();
long m2 = d2.getTime();
long n = m2 - m1;
double t = (double)n/3600000;
String str = t+"";
if(str.length()>=4){
str = str.substring(0, 4);
}
return Double.parseDouble(str);
}
/***
* 计算两个日期之间的年数
*/
public double calDateToYears(String end,String beg) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
double day = 0;
double year = 0;
try {
Date date = myFormatter.parse(end);
Date mydate = myFormatter.parse(beg);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
year = day/365;
String str = year+"";
if(str.length()>=4){
year = Double.parseDouble(str.substring(0, 4));
}
} catch (Exception e) {
}
return year;
}
/***
* 返回当前系统日期+时间
*/
public String systemTime() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(now);
}
/***
* 获取当前的星期数
*/
public int getNowWeek() {
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.DAY_OF_WEEK)-1;
}
/**
*
* 获取当前的时间
*/
public String getNowTime() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
return dateFormat.format(now);
}
/**
* 把日期转为时间
*/
public String dateToTime(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
return dateFormat.format(date);
}
/**
* 开始时间跟结束时间作比较,如果结束时间大返回1;否则返回0
* @param beg
* @param end
*/
public int compareTime(Date beg,Date end){
SimpleDateFormat f = new SimpleDateFormat("HHmmss"); //格式化为 hhmmss
int d1Number = Integer.parseInt(f.format(beg).toString()); //将第一个时间格式化后转为int
int d2Number = Integer.parseInt(f.format(end).toString()); //将第二个时间格式化后转为int
if(d1Number>d2Number){
return 0;
}
else{
return 1;
}
}
* 计算N年之后的日期
* */
public Date calDateByNum(int num){
Date date = new Date();
Calendar cld = Calendar.getInstance();
cld.setTime(date);
cld.add(Calendar.YEAR, num);
return cld.getTime();
}
/**
* 时间差转为小时数
* @throws ParseException
*/
public double calDateToHours(String beg,String end) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse(beg);
Date d2 = sdf.parse(end);
d1.setYear(2015);
d1.setMonth(11);
d1.setDate(11);
d2.setYear(2015);
d2.setMonth(11);
d2.setDate(11);
long m1 = d1.getTime();
long m2 = d2.getTime();
long n = m2 - m1;
double t = (double)n/3600000;
String str = t+"";
if(str.length()>=4){
str = str.substring(0, 4);
}
return Double.parseDouble(str);
}
/***
* 计算两个日期之间的年数
*/
public double calDateToYears(String end,String beg) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
double day = 0;
double year = 0;
try {
Date date = myFormatter.parse(end);
Date mydate = myFormatter.parse(beg);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
year = day/365;
String str = year+"";
if(str.length()>=4){
year = Double.parseDouble(str.substring(0, 4));
}
} catch (Exception e) {
}
return year;
}
/***
* 返回当前系统日期+时间
*/
public String systemTime() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(now);
}
/***
* 获取当前的星期数
*/
public int getNowWeek() {
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.DAY_OF_WEEK)-1;
}
/**
*
* 获取当前的时间
*/
public String getNowTime() {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
return dateFormat.format(now);
}
/**
* 把日期转为时间
*/
public String dateToTime(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
return dateFormat.format(date);
}
/**
* 开始时间跟结束时间作比较,如果结束时间大返回1;否则返回0
* @param beg
* @param end
*/
public int compareTime(Date beg,Date end){
SimpleDateFormat f = new SimpleDateFormat("HHmmss"); //格式化为 hhmmss
int d1Number = Integer.parseInt(f.format(beg).toString()); //将第一个时间格式化后转为int
int d2Number = Integer.parseInt(f.format(end).toString()); //将第二个时间格式化后转为int
if(d1Number>d2Number){
return 0;
}
else{
return 1;
}
}
0 0
- 跟日期 时间有关的计算与比较
- js计算时间加减及有关时间大小的比较
- 与日期和时间有关的类
- Oracle 中与时间有关的计算
- C# 时间日期的获取与比较
- java日期时间的处理与比较
- DELPHI 日期与时间的计算
- 关于时间与日期的计算
- js正则表达式具体时间的验证,当前日期跟所填日期比较大小,时间的比较
- java 时间的计算与比较
- 在Linux中与日期、计算有关的几个命令
- 时间日期的计算
- 日期时间的计算
- Java与日期和时间有关的类
- C/C++中与时间、日期有关的函数总结
- PHP日期与时间的应用 倒计时 比较时间大小
- Java的日期与时间(三)计算运行时间
- PHP日期与时间的应用 计算脚本运行时间
- lua中遍历table的几种方式比较
- VS一些设置及编译时候的一些问题以及解决方案
- iOS App检测版本更新
- 电商之路
- 用CStdioFile写文件
- 跟日期 时间有关的计算与比较
- chrome jsonView插件安装
- QPPc menu过程
- YAML Class ID Reference
- typedef用法详解
- SAT阅读考试考生如何培养阅读兴趣【zhasite】
- SDWebImage 加载图片异常问题
- 从”JAVA“而终 13:java IO流和File类的讲解
- android底层开发进阶(2)--makefile笔记与android的平台编译