【问题标题】:How to find the interval between two dates with out using time如何在不使用时间的情况下找到两个日期之间的间隔
【发布时间】:2015-09-23 18:27:02
【问题描述】:

这是我使用时间计算事件日期和当前日期之间间隔的代码。这里我根据时间计算间隔。我不想要那个。我只想要天差地别。我的意思是考虑如果事件日期是 25-09-2015 当前日期:23-09-2015 那么间隔将是 2 天。我需要这种类型的代码。请帮我找出代码

String eventDate = btn_Date.getText().toString();
DateFormat date = new SimpleDateFormat("dd-M-yyyy");
Date date1 = date.parse(eventDate);

Date currentDate = new Date();
daysBefore = (int) ((date1.getTime() - currentDate.getTime()) / (1000 * 60 * 60 * 24));

【问题讨论】:

标签: android datetime


【解决方案1】:

试试这个。在这里,我也格式化了 currentDate。这是跳过时间,现在您可以获得确切的剩余天数。

 String eventDate = btn_Date.getText().toString();
 DateFormat date = new SimpleDateFormat("dd-M-yyyy");
    try {
        Date date1 = date.parse(eventDate);
        Date currentDate = new Date();
        currentDate = date.parse(date.format(currentDate));
        long difference = date1.getTime() - currentDate.getTime();
        long diff = TimeUnit.DAYS.convert(difference , TimeUnit.MILLISECONDS);
        Log.i("remaining days", diff+ "");

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【讨论】:

  • 只需将当前日期重新格式化为 (dd-M-yyyy) 这将跳过剩余时间。所以当前日期和事件日期是相同的格式。
  • 你能检查一下并告诉我解决方案吗
【解决方案2】:

一旦你有了两个Date 对象,你就可以创建一个小方法来为你计算它。像这样的:

public static long findDayDiff(Date date1, Date date2) {
    long difference = date2.getTime() - date1.getTime();
    return TimeUnit.DAYS.convert(difference , TimeUnit.MILLISECONDS);
}

【讨论】:

  • 您是否使用findDayDiff(currentDate, date1) 调用它?
  • 是的...我调用了那个方法
  • else应该基于什么?在某些时候,您必须获得Dates 中的Time
  • 我需要计算天数......事件日期是 25-09-2015 当前日期是 23-09-2015....我需要将间隔设为 2 天......如果您按时间计算(我的意思是使用获取时间),它将返回间隔为 1 天。因为时间已经过去了半天,所以它只会返回 1 天...我不想要那个...
【解决方案3】:

试试这个,

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

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class MyTimeActivity extends Activity
{
    private static final String TAG = "MyTimeActivity";

    //Activity
    private Context mContext;

    /**
     * Called when the activity is first created
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.mContext = this;

        Calendar cal1 = stringToCalendar("2012-03-20");
        Calendar cal2 = stringToCalendar("2012-03-26");
        String elapsedDaysText = getElapsedDaysText(cal1, cal2);
    }

    public static Calendar stringToCalendar(String dateString)
    {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try
        {
            date = sdf.parse(dateString);
            cal.setTime(date);
        }
        catch(ParseException e)
        {
            cal = null;
        }
        return cal;
    }   

    public String getElapsedDaysText(Calendar c1, Calendar c2)
    {
        long ms1 = c1.getTimeInMillis();
        long ms2 = c2.getTimeInMillis();
        long ps = (ms2 - ms1) / 1000;
        long elapsedDays = ps / 60 / 60 / 24;
        String elapsedDaysText = String.format("%d days ago", elapsedDays);
        return elapsedDaysText;
    }   
}

【讨论】:

    【解决方案4】:

    请找到这个

      long days = 0;
      long seconds = 0;
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                Date date1 = sdf.parse(firstDate);
                Date date2 = sdf.parse(nextDate);
                long diff = date2.getTime() - date1.getTime();
                days = diff / (24 * 60 * 60 * 1000);
                seconds = days *24*60*60
            }catch(Exception e){
                e.printStackTrace();
            }
    

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      相关资源
      最近更新 更多