1

友達、私は日数の違いを計算しようとしています。

31st Aug 23:59:00と次の日付1 Sept 00:02:00を入力すると、レコードを 1 日として表示する必要があるとします。

これを手伝ってください。

現在、同じものを使用して計算して .getTimeInMillis()いますが、上記の日付条件に対して期待される結果が得られません。

4

7 に答える 7

1

私はあなたが日と時間の違いを探し、私のコードを使用します

public class AndroidWebImage extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Date sdate=Calendar.getInstance().getTime();
  SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy HH:mm:ss");

  String setDate = "13/09/12 10:20:43";
  Date AlarmDate=new Date(setDate);
  String currentDate = format.format(sdate);

  Date d1 = null;
  Date d2 = null;
  try {
      d1 = format.parse(setDate);
      d2 = format.parse(currentDate);
  } catch (ParseException e) {
      e.printStackTrace();
  }    
 //Comparison
  long diff = d1.getTime() - d2.getTime();
  long diffSeconds = diff / 1000 % 60;  

  long days = (int) (diff / (1000 * 60 * 60 * 24));
  long diffHours = (int) ((diff- (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));
  long diffMinutes = (int) (diff- (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 *      diffHours))/ (1000 * 60);

  int curhour=sdate.getHours();
  int curmin=sdate.getMinutes();
  int alarmhour=AlarmDate.getHours();
  int alarmmin=AlarmDate.getMinutes();
  if(curhour==alarmhour && curmin==alarmmin)
  {
      Toast.makeText(getApplicationContext(), String.valueOf(days+"days\n"+diffHours+"hrs"+diffMinutes+"min\n"+diffSeconds+"sec"),Toast.LENGTH_LONG).show();
  }
  else if(curhour>=alarmhour && curmin>=alarmmin || curhour<=alarmhour && curmin<=alarmmin)
  {
      Toast.makeText(getApplicationContext(), String.valueOf(days+"days\n"+diffHours+"hrs"+diffMinutes+"min\n"+diffSeconds+"sec"),Toast.LENGTH_LONG).show();
  }




 }
}
于 2012-09-25T11:20:31.113 に答える
0

私は以前にこのコードを作成しました、それはあなたを助けるかもしれません

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author MErsan
 */
public class DateFormatter {

    public static String formatDate(long time) {
        StringBuilder result = new StringBuilder();

        // 1- Check the year
        // 2- Check the Month
        // 3- Check the Day
        // 4- Check the Hours

        Date myDate = new Date(time);
        Date todayDate = new Date(System.currentTimeMillis());

        if (todayDate.getYear() - myDate.getYear() != 0) {
            // Not same year, and should append the whole time
            return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(myDate);
        }
        // Same Year
        // now Check the month
        if (todayDate.getMonth() - myDate.getMonth() != 0) {
            return new SimpleDateFormat("MMM dd, hh:mm a").format(myDate);// Aug
            // 16,
            // 11:55
            // PM
        }

        // Now Same Month
        // Check the day
        int daysDiff = todayDate.getDate() - myDate.getDate();
        if (daysDiff == 1) {// Yesterday
            result.append("Yesterday").append(' ');
            result.append(new SimpleDateFormat("hh:mm a").format(myDate));
            return result.toString();
        } else if (daysDiff != 0) {
            return new SimpleDateFormat("MMM dd, hh:mm a").format(myDate);// Aug
            // 16,
            // 11:55
            // PM
        }

        // Same Day :')
        // Check the hour
        int hoursDiff = todayDate.getHours() - myDate.getHours();
        if (hoursDiff < 0) {// Invalid Time
            // :@
            result.append("Today").append(' ');
            result.append(new SimpleDateFormat("hh:mm a").format(myDate));
            return result.toString();
        } else if (hoursDiff > 3) {// Not Same Hour, Hour Diff more than 3 hours
            result.append("Today").append(' ');
            result.append(new SimpleDateFormat("hh:mm a").format(myDate));
            return result.toString();
        } else if (hoursDiff != 0) {// Hours Diff less than 3 hours, but not
            // current hour
            int mintuesDiff = todayDate.getMinutes() - myDate.getMinutes();

            result.append("Before").append(' ');
            result.append(hoursDiff).append(' ');
            result.append("Hours").append(' ');
            result.append("and").append(' ');
            result.append(Math.abs(mintuesDiff)).append(' ');
            result.append("Minutes");
            System.err.println("Case 6");
            return result.toString();
        } else if (hoursDiff == 0) {// Same Hours
            int mintuesDiff = todayDate.getMinutes() - myDate.getMinutes();
            if (mintuesDiff < 1) {// Seconds Only {Same Minute}
                int secondsDiff = todayDate.getSeconds() - myDate.getSeconds();
                result.append("Before").append(' ');
                result.append(Math.abs(secondsDiff)).append(' ');
                result.append("Seconds");
                return result.toString();
            } else {
                result.append("Before").append(' ');
                result.append(Math.abs(mintuesDiff)).append(' ');
                result.append("Minutes");
                return result.toString();
            }
        }

        // Default
        return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(myDate);
    }
}
于 2012-09-25T11:19:08.400 に答える
0

再投稿

少なくとも私にとっては、実行可能な唯一の解決策である単純な解決策があります。

問題は、Joda、Calendar、Dateなどを使用して、私が目にするすべての答えがミリ秒単位しか考慮されていないことです。最終的には、実際の日数ではなく、2つの日付間の24時間サイクルの数をカウントします。したがって、1月1日午後11時から1月2日午前1時までの何かは0日を返します。

startDateとの間の実際の日数を数えるには、次のendDateようにします。

// Find the sequential day from a date, essentially resetting time to start of the day
long startDay = startDate.getTime() / 1000 / 60 / 60 / 24;
long endDay = endDate.getTime() / 1000 / 60 / 60 / 24;

// Find the difference, duh
long daysBetween = endDay - startDay;

これにより、1月2日から1月1日までの間に「1」が返されます。終了日をカウントする必要がある場合は、に1を追加するだけdaysBetweenです(範囲内の合計日数をカウントしたかったので、コードでこれを行う必要がありました)。

于 2012-12-18T15:45:26.510 に答える
0
import java.util.Calendar;

public class DateDifference
{  
    public static void main(String[] args)
    {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();

        calendar1.set(2012, 01, 10);
        calendar2.set(2012, 07, 01);

        long milliseconds1 = calendar1.getTimeInMillis();
        long milliseconds2 = calendar2.getTimeInMillis();

        long diffDays = diff / (24 * 60 * 60 * 1000);

        System.out.println("Time in days: " + diffDays + " days.");
    }
} 
于 2012-09-25T11:21:55.120 に答える
0

タイムスタンプを削除してから日付を減算して日付の差を取得するか、以下のようにJoda-timeを使用する必要があります。

import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;

Date past = new Date(112, 8, 1); 
Date today = new Date(112, 7, 30); 
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); 
于 2012-09-25T11:16:19.300 に答える
0

ミリ秒ではこれを行うことができません。これは、日の境界がどこにあるか (つまり真夜中) を知る必要があるためです。真夜中の両側の 1 ミリ秒は、2 つの異なる日を意味します。

カレンダーを使用して、2 つの日付の間の間隔内に何日あるのかを判断する必要があります。JodaTimeライブラリには、この種の計算に対する多くの追加サポートがあります。

2 つの Java 日付インスタンスの差の計算も参照してください。

于 2012-09-25T11:17:17.277 に答える
0

日数を見つけようとしているだけですよね?

これを見てみてください。探しているものがあるかもしれません。

于 2012-09-25T11:18:34.073 に答える