java date 计算时间差
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java date 计算时间差相关的知识,希望对你有一定的参考价值。
用最简单的方法计算给出的时间(string)于当前时间的时间差。输出格式:刚刚 或 XX分钟前 或 XX小时前 或 XX天前 或 XX月前 或 XX年前。
现在是2004-03-26 13:31:40
过去是:2004-01-02 11:30:24
要获得两个日期差,差的形式为:XX天XX小时XX分XX秒
方法一:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
Date d1 = df.parse("2004-03-26 13:31:40");
Date d2 = df.parse("2004-01-02 11:30:24");
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60 * 60 * 24);
catch (Exception e)
方法二:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date now = df.parse("2004-03-26 13:31:40");
java.util.Date date=df.parse("2004-01-02 11:30:24");
long l=now.getTime()-date.getTime();
long day=l/(24*60*60*1000);
long hour=(l/(60*60*1000)-day*24);
long min=((l/(60*1000))-day*24*60-hour*60);
long s=(l/1000-day*24*60*60-hour*60*60-min*60);
System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
方法三:
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date begin=dfs.parse("2004-01-02 11:30:24");
java.util.Date end = dfs.parse("2004-03-26 13:31:40");
long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
long day1=between/(24*3600);
long hour1=between%(24*3600)/3600;
long minute1=between%3600/60;
long second1=between%60/60;
System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");
参考技术A public static String diff(String strDate, String pattern)
String ret = "";
try
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date tmp = sdf.parse(strDate);
if(tmp.after(now))
return "时间超过当前时间";
long s = (now.getTime() - tmp.getTime()) / 1000;
long count = 0;
if((count = s / (3600 * 24 * 365)) > 0)
ret = count + "年前";
else if((count = s / (3600 * 24 * 30)) > 0)
ret = count + "月前";
else if((count = s / 3600 * 24) > 0)
ret = count + "天前";
else if((count = s / 3600) > 0)
ret = count + "小时前";
else if((count = s / 60) > 0)
ret = count + "分钟前";
else
ret = "刚刚";
catch (Exception e)
return ret;
以上算法月份是按30天来算,年以365天来算的。
追问算法有待继续提高,每次计算3600*24……实属冗余,每个月都是30天就不合理,年也一样。
思路还是正确的。
* 获取前几天或者后几天的时间戳
*
*/
public static String getDayTime(int days)
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, days);
String timeStamp = String.valueOf(cal.getTimeInMillis()/1000);
return timeStamp;
Java Date 类型时间运算,对年做减法运算计算工龄得分
/**
* 工龄折算分数
* 1.1.1小学:记60分结合折算0.54分
* 1.1.2初中:记70分结合折算0.63分
* 1.1.3中专(高中):记80分结合折算0.72分
* 1.1.4大专:记90分结合折算0.81分
* 1.1.5本科(及以上):记100分结合折算0.9分
* @param workYears
* @return
*/
public static double getWorkYearsScore(Date workYears)
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
ParsePosition pos = new ParsePosition(0);
Date startWorkYear = formatter.parse(formatter.format(workYears), pos);
ParsePosition pos2 = new ParsePosition(0);
Date nowDate = formatter.parse(formatter.format(new Date()), pos2);
//将Date类型转化为LocalDate
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = startWorkYear.toInstant();
LocalDate localstartWorkYear = instant.atZone(zoneId).toLocalDate();
Instant instant2 = nowDate.toInstant();
LocalDate localnowDate = instant2.atZone(zoneId).toLocalDate();
Period period = Period.between(localstartWorkYear, localnowDate);
int years = period.getYears();
double score;
if (years >= 0 && years <= 3)
score = 0.3;
else if (years >= 4 && years <= 6)
score = 0.35;
else if (years >= 7 && years <= 10)
score = 0.4;
else if (years >= 11 && years <= 20)
score = 0.45;
else
score = 0.5;
return score;
以上是关于java date 计算时间差的主要内容,如果未能解决你的问题,请参考以下文章