私は2つの日付20-03-2011と1-04-2011、Stringとして格納されます。これを日付形式に解析し、2つの日付の違いを計算できますか?2つの特定の日付の差
答えて
解析はここで説明java.text.DateFormat
で使用する解析機能を:Parse A Java Date
そして、ここで差を計算する:https://stackoverflow.com/a/3100373/66686
そのリンクの差分を計算する方法は遅れています。誰かがDateDiffのループを使っているとは決して推測できません。したがって、このページで利用可能な回答を使用する方がはるかに優れています。 – kape123
我々はこれを達成することができ、多数の方法があるので、次は、一つの解決策である:
はimport java.util.*;
int syear = 2000;
int eyear = 2000;
int smonth = 2;//Feb
int emonth = 3;//Mar
int sday = 27;
int eday = 1;
Date startDate = new Date(syear-1900,smonth-1,sday);
Date endDate = new Date(eyear-1900,emonth-1,eday);
int difInDays = (int) ((endDate.getTime() - startDate.getTime())/(1000*60*60*24));
ご協力ありがとうございます... – rak
ようこそ。 –
public void diff(String str1, String str2)
{
DateTimeFormatter FMT = DateTimeFormat.forPattern("dd-mm-yyyy");
final DateTime dt1 = new DateTime(FMT.parseDateTime(str1));
final DateTime dt2 = new DateTime(FMT.parseDateTime(str2));
Days days = Days.daysBetween(dt1, dt2);
}
これを試してみてください:
long getDateDiff(String str1, String str2) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date1 = simpleDateFormat.parse(str1);
Date date2 = simpleDateFormat.parse(str2);
return date2.getTime() - date1.getTime();
}
は、これは、私はあなたがこれをしたいかもしれない方法を説明するために手早く小さなプログラムです。しかし、バグを含んでいる可能性があるので注意してください。
常にすべてのユニットのテストには、日付計算を含むコードが必要です。すべてのことを行うためのJava APIは非常に厄介で、常にバグの多いコードが生成されるようです。可能な場合は、JodaTimeなど、自分の職場で使用されているような、より洗練された日付と時刻APIを使用してください。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateFun {
public static void main(String[] args) throws ParseException {
String dateOne = "20-03-2011 06:44:03 GMT";
String dateTwo = "25-03-2011 23:12:59 GMT";
String format = "dd-MM-yyyy HH:mm:ss z";
System.out.println(getDiff(parseDateString(format, dateTwo),
parseDateString(format, dateOne)));
dateOne = "20-03-2011 GMT";
dateTwo = "1-04-2011 GMT";
format = "dd-MM-yyyy z";
System.out.println(getDiff(parseDateString(format, dateOne),
parseDateString(format, dateTwo)));
}
private static Calendar parseDateString(final String format,
final String date) throws ParseException {
final Date parsed = new SimpleDateFormat(format).parse(date);
final Calendar cal = Calendar.getInstance();
cal.setTime(parsed);
return cal;
}
private static TimeDiff getDiff(final Calendar calOne, final Calendar calTwo) {
return new TimeDiff(Math.abs(calOne.getTimeInMillis() - calTwo.getTimeInMillis()));
}
private static class TimeDiff {
private static final long MS_IN_SECOND = 1000;
private static final long MS_IN_MINUTE = MS_IN_SECOND * 60;
private static final long MS_IN_HOUR = MS_IN_MINUTE * 60;
private static final long MS_IN_DAY = MS_IN_HOUR * 24;
private final long days;
private final long hours;
private final long minutes;
private final long seconds;
private final long milliseconds;
TimeDiff(final long msDiff) {
long msRemainder = msDiff;
days = msRemainder/MS_IN_DAY;
msRemainder = msRemainder - (days * MS_IN_DAY);
hours = msRemainder/MS_IN_HOUR;
msRemainder = msRemainder - (hours * MS_IN_HOUR);
minutes = msRemainder/MS_IN_MINUTE;
msRemainder = msRemainder - (minutes * MS_IN_MINUTE);
seconds = msRemainder/MS_IN_SECOND;
msRemainder = msRemainder - (seconds * MS_IN_SECOND);
milliseconds = msRemainder;
}
@Override
public String toString() {
return "TimeDiff[days: " + days + ", hours: " +
hours + ", minutes: " + minutes +
", seconds: " + seconds + ", milliseconds: " +
milliseconds + "]";
}
}
}
これに誰かがバグを発見した場合は、私に知らせてください。
コードの下に試してください:あなたは `Date`(SO上SimpleDateFormat``の検索)にそれらを解析し、単に `date.getTimeを()`引く必要があり
public void compareDate(String date1, String date2){
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.setTime(format.parse(date1));
calendar2.setTime(format.parse(date2));
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffSeconds = diff/1000;
long diffMinutes = diff/(60 * 1000);
long diffHours = diff/(60 * 60 * 1000);
long diffDays = diff/(24 * 60 * 60 * 1000);
System.out.println("\nThe Date Different Example");
System.out.println("Time in milliseconds: " + diff+ " milliseconds.");
System.out.println("Time in seconds: " + diffSeconds+ " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");
System.out.println("Time in days: " + diffDays + " days.");
}
- 1. Pandasの日付列と特定の日付の日付差を見つける
- 2. MySQLの2つの日付の差
- 3. codeigniterの2つの日付の差
- 4. 2つの日付の分数の差
- 5. 特定の日付との2つの日付を比較
- 6. 2つの日付の差sqlite
- 7. 2つの日付の差(秒)ios
- 8. 2つの日付の時差
- 9. 2つの日付間の差(分)
- 10. PL/SQL:2つの日付の差
- 11. Adobe Airの2つの日付の日付の差
- 12. 2つの日付のSQLクエリの日付の差
- 13. C++ 2つの日付間の日付の差
- 14. 2列の日付の差
- 15. 時差Zendの2つの日付間の差
- 16. インパラの2つの日付間の日数の差
- 17. 日グリッド内の2つの日付の間の差
- 18. (年、月と日)のギでアンドロイドで2つの日付の差
- 19. 2つのタイムスタンプの日付の差を見つけるには
- 20. 特定の人物の2つの日付間のPHPデータ
- 21. 指定された期間に1日あたり最大2つの日付の間の日付差
- 22. ElasticSearch 2.4 2つの日付フィールドの差を使った日付範囲のヒストグラム
- 23. 2つの日付ピッカーコンテンツコントロール間の日付の差異ワード2016 vbaコード
- 24. 特定の値に基づく日付差の検索
- 25. Teradataの2つの日付間の時間差の計算
- 26. スカラの2つの日付の月の差
- 27. 2つの日付の差とタイムスタンプの結果の取得
- 28. moment.jsの2つのタイムゾーン形式の日付の差分
- 29. 同じテーブルの2つのレコードの日付の差
- 30. Javaの2つの日付の差異(開始日と終了日を含む)
まず。結果はミリ秒です。 –
@TomaszNurkiewicz – rak
@TomaszNurkiewicz私は以下のコードを試しましたが、解析できないdate.DateFormatフォーマッタとしてエラーを表示します。 \t \t \t \t \t \t \t \t \t \t日付DATE1。 \t \t \t \t \t \t \t \t \t \t日の日付2。 \t \t \t \t \t \t \t \t \t \tフォーマッタ=新しいてSimpleDateFormat( "DD/MMM/YY")。 \t \t \t \t \t \t \t \t \t \t試み{ \t \t \t \t \t \t \t \t \t \t \t日付1 =(日)フォーマッタ。解析する(LeaveStartDate); \t \t \t \t \t \t \t \t \t \t \tのSystem.out.println( "日付1 --------" + DATE1)。 \t \t \t \t \t \t \t \t \t \t}キャッチ(はParseException電子){ \t \t \t \t \t \t \t \t \t \t \t // TODO自動生成されたcatchブロック \t \t \t \t \t \t \t \t \t \t \t e.printStackTrace(); \t \t \t \t \t \t \t \t \t \t} – rak