【问题标题】:Current time - last tim (java) [duplicate]当前时间 - 上次时间(java)[重复]
【发布时间】:2015-10-23 00:28:28
【问题描述】:

对不起我的英语。我有当前时间2015-07-31 12:19:55 和上次2015-07-31 13:38:20,我不知道如何获得日期差异。示例:

2015-07-31 12:19:55 - 2015-07-31 13:38:20 = date what i need

我的简单代码:

private Date date = new Date();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private String currectDate = dateFormat.format(date);
    private String lastLoginDate = "2015-07-31 12:19:55";

【问题讨论】:

  • 需要日期差异吗?
  • @Satya 感谢您的回答,是的,需要日期差异

标签: java android date


【解决方案1】:
    java.text.DateFormat outputFormat1 =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date d1 = outputFormat1.parse("2015-07-31 12:19:55");//start date
    Date d2 = outputFormat1.parse("2015-07-31 13:38:20");//end date
    long diff = d2.getTime() - d1.getTime();
    long diffHours = diff / (60 * 60 * 1000) % 24;//in terms of hours
    long diffmin = diff / (60 * 1000) % 60;//in mimutes
    long diffsec = diff / (1000) % 60;//in seconds
    String rtime=diffHours+"h:"+diffmin+"m:"+diffsec+"s";
    System.out.println(rtime);//1h:18m:25s

以天计?

  long diffDays = diff / (24 * 60 * 60 * 1000);//in terms of days

使用Period

Period period = new Period(d1, d2);
System.out.print(period.getYears() + " years, ");
System.out.print(period.getMonths() + " months, ");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 2015-04-10
    相关资源
    最近更新 更多