2017-05-02 12 views
1

2回の正確な時差を計算する方法はありますか?Javaの時差の計算方法は?

の場合。例えば

t1= 11:18 
t2= 20:20 
t2 - t1 = 9.02 

私はこのコードを試しましたが、それは役に立たないです。

String hour1=h1+":"+m1; 
String hour2= h2+":"+m2; 
SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm"); 
Date d1= dateFormat.parse(hour1); 
Date d2= dateFormat.parse(hour2); 
long d=d2.getTime()-d1.getTime(); 
long hour= d/(60*60*1000) % 24; 

このコードを使用すると、分差が見つかりません。

+4

さてあなたは、時間をうまくするためのコードを持っているの...あなたは何分間試しましたか? (ヒント:60で何度も分割しないでください...) –

+0

あなたは 'h1'であなたの時を過ごしています:' m1'と 'h2':' m2'なぜあなたはその値を使って数学? – AxelH

+0

よく、私は最初に@AxelHという理由で数学を間違って試しました。その理由はこの形式に切り替えました – Saurabh

答えて

1

次のコードを使用して、異なるタイムゾーンに変換し、時間の違いを確認しました。あなたはミリ秒単位で時間をつかんで、私の場合のように値を比較することができます。 timeInMillisOneからtimeInMillisTwoを減算するだけです。

Date date = dateFormat.parse(""); 
Calendar cal = Calendar.getInstance(); // that is NOW for the timezone configured on the computer. 

long timeInMillis = cal.getTimeInMillis(); 
cal.setTimeInMillis(timeInMillis + (60000 * 60 * 5) - (60000 * 15)); 
//the line above converts to a different time zone and subtracts fifteen 
//minutes. 
long fifteenMinutesAgo = cal.getTime(); 

if(date.after(fifteenMinutesAgo)){ 
    //do something 
} 
+0

ありがとう:)私はこのコードを試してみます。 – Saurabh

0

これは機能する場合があります。

package cn.sz.cyrus.java8test; 

public class TimeCalcute { 

    public static String calcTime(String time1, String time2) { 
     String[] times1 = time1.split(":"); 
     String[] times2 = time2.split(":"); 
     int timeint1 = Integer.valueOf(times1[0]) * 60 + Integer.valueOf(times1[1]); 
     int timeint2 = Integer.valueOf(times2[0]) * 60 + Integer.valueOf(times2[1]); 
     int interval = timeint1 - timeint2; 
     String result = String.format("%d:%02d", interval/60, interval % 60); 
     return result; 
    } 

    public static void main(String[] args) { 
     String time1 = "20:20"; 
     String time2 = "11:18"; 
     System.out.println(calcTime(time1, time2)); 
     time1 = "22:46"; 
     time2 = "10:59"; 
     System.out.println(calcTime(time1, time2)); 
    } 

} 

これは、印刷結果である:

9時02分

11時47

0

あなたはmillisecondesとして2つの日付の差を得ることができ、その後、あなたは数分または数時間に変換することができます、

public static Long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) { 
     long diffInMillies = date1.getTime() - date2.getTime(); 
     return diffInMillies; 
    } 

public static void main(String[] args) throws IOException { 
    double value= Test.getDateDiff(new Date(System.currentTimeMillis()+37*10000), new Date(System.currentTimeMillis()), TimeUnit.MINUTES); 
    System.out.println("Hour:"+value/(100*60*60)); 

}

2

JDK 8日付時刻APIが大幅にこれらを簡素化し、時間差は、以下のように期間を用いて計算することができます。

LocalTime t1 = LocalTime.of(11, 18); 
LocalTime t2 = LocalTime.of(20, 20); 
System.out.println(Duration.between(t1, t2)); //output - PT9H2M 

出力が9時間2分です。 (9H 2M)

1

あなたはほとんどそれを持っていました。ちょうど必要な分を計算する。

long minutes = d/(60*1000) % 60; 
long seconds = d/1000 % 60; 

場合によっては秒を追加しました。 @ Pallaviの回答のように、Durationを使っていましたが、結果のオブジェクトから実際のデータを引き出すのはもう少し工夫されていました。

1

あなたはJDK 8を使用していない場合は、JodaTimeを使用することができますが、JDK 8で、あなたがこのような何か書くことができます:出力がされます

public static void main(String[] args) { 
    LocalDateTime from = LocalDateTime.of(2017, 2, 5, 11, 18, 0); 
    LocalDateTime to = LocalDateTime.of(2017, 2, 5, 20, 20, 0); 

    long seconds = ChronoUnit.SECONDS.between(from, to); 
    System.out.println(String.format("%d:%02d:%02d", seconds/3600, (seconds % 3600)/60, (seconds % 60))); 
} 

を:午前九時02分○○秒

取得するにはLocalDateTimeDurationクラスを使用できますが、JDK 8のフォーマッタはありません。この特定の出力フォーマットが必要な場合は、サードパーティのライブラリを使用するか、独自のフォーマッタを作成します上記の例のように。

あなたが書くことができる期間を使用するには:

public static void main(String[] args) { 
    LocalDateTime from = LocalDateTime.of(2017, 2, 5, 11, 18, 0); 
    LocalDateTime to = LocalDateTime.of(2017, 2, 5, 20, 20, 0); 

    Duration duration = Duration.between(from, to); 

    long seconds = duration.getSeconds(); 
    System.out.println(String.format("%d:%02d:%02d", s/3600, (s % 3600)/60, (s % 60))); 
} 

乾杯、A.