Mengitung Selisih Waktu di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package selisihwaktu;

import java.util.Calendar;

/**
*
* @author Angga
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Creates two calendars instances
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();

// Set the date for both of the calendar instance
cal1.set(2006,12,30);
cal2.set(2007,5,3);

//Get the represented date in milliseconds
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();

//Calculate difference in milliseconds
long diff = milis2 – milis1;

// Calculate difference in seconds
long diffSeconds = diff / 1000;

// Calculate difference in minutes
long diffMinutes = diff / (60 * 1000);

// Calculate difference in hours
long diffHours = diff / (60 * 60 * 1000);

//Calculate difference in days
long diffDays = diff / (24 * 60 * 60 * 1000);

System.out.println(“In milliseconds: “+ diff +” milliseconds.”);
System.out.println(“In seconds: “+ diffSeconds +” seconds.”);
System.out.println(“In minutes: “+ diffMinutes +” minutes.”);
System.out.println(“In hour: “+ diffHours +” hours.”);
System.out.println(“In days: “+ diffDays +” days.”);

}

}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google

You are commenting using your Google account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s