Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Membandingkan Dua Tanggal dalam Java

Jika anda ingin mengetahui perbandingan antara dua Tanggal, misalnya tanggal x adalah sebelum tanggal y atau sebaliknya maka anda bisa menggunakan code java seperti contoh di bawah ini :


import java.util.*;
import java.text.*;

public class CompareDates {
  public static void main(String[] args) throws ParseException {

    DateFormat df = new SimpleDateFormat ("dd-MM-yyyy");

    // Get Date 1
    Date d1 = df.parse(args[0]);

    // Get Date 2
    Date d2 = df.parse(args[1]);

    String relation;
    if (d1.equals(d2))
      relation = "the same date as";
    else if (d1.before(d2))
      relation = "before";
    else
      relation = "after";
    System.out.println(d1 + " is " + relation + " " + d2);
  }
}

Selamat mencoba dan semoga bermanfaat...


This post first appeared on Bow's, please read the originial post: here

Share the post

Membandingkan Dua Tanggal dalam Java

×

Subscribe to Bow's

Get updates delivered right to your inbox!

Thank you for your subscription

×