2013-10-01 71 views
46

Tôi đang làm việc trên một ứng dụng Java tạo báo cáo cho một khoảng thời gian do người dùng nhập vào trong dòng lệnh. Người sử dụng cần nhập ngày theo định dạng sau: dd-MM-yyyyCách kiểm tra xem ngày tháng có lớn hơn ngày khác trong Java không?

> java report startDate endDate

Ví dụ:

báo cáo java 01-01-2013 31-03-2013

Trong mã tôi lưu ngày tháng trong hai chuỗi. Tôi phải đảm bảo rằng ngày bắt đầu được người dùng nhập phải là ngày sớm hơn ngày kết thúc. Có chức năng tích hợp nào có thể giúp tôi đạt được điều này bằng cách truyền hai chuỗi này cho nó?

+2

Chuyển đổi chuỗi thành java.util.Date. Ngày thực hiện Comparable. – PeterMmm

+0

Cập nhật: Các lớp java.time thay thế các lớp ngày tháng cũ của phiền hà như 'Date' và' Calendar'. –

Trả lời

84

Bạn có thể sử dụng Date.before() hoặc Date.after() hoặc Date.equals() để so sánh ngày.

Taken từ here:

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

public class DateDiff { 

    public static void main(String[] args) 
    { 
     compareDates("2017-01-13 00:00:00", "2017-01-14 00:00:00");// output will be Date1 is before Date2 
     compareDates("2017-01-13 00:00:00", "2017-01-12 00:00:00");//output will be Date1 is after Date2 
     compareDates("2017-01-13 00:00:00", "2017-01-13 10:20:30");//output will be Date1 is before Date2 because date2 is ahead of date 1 by 10:20:30 hours 
     compareDates("2017-01-13 00:00:00", "2017-01-13 00:00:00");//output will be Date1 is equal Date2 because both date and time are equal 
    } 

    public static void compareDates(String d1,String d2) 
    { 
     try{ 
      // If you already have date objects then skip 1 

      //1 
      // Create 2 dates starts 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
      Date date1 = sdf.parse(d1); 
      Date date2 = sdf.parse(d2); 

      System.out.println("Date1"+sdf.format(date1)); 
      System.out.println("Date2"+sdf.format(date2));System.out.println(); 

      // Create 2 dates ends 
      //1 

      // Date object is having 3 methods namely after,before and equals for comparing 
      // after() will return true if and only if date1 is after date 2 
      if(date1.after(date2)){ 
       System.out.println("Date1 is after Date2"); 
      } 
      // before() will return true if and only if date1 is before date2 
      if(date1.before(date2)){ 
       System.out.println("Date1 is before Date2"); 
      } 

      //equals() returns true if both the dates are equal 
      if(date1.equals(date2)){ 
       System.out.println("Date1 is equal Date2"); 
      } 

      System.out.println(); 
     } 
     catch(ParseException ex){ 
      ex.printStackTrace(); 
     } 
    } 

    public static void compareDates(Date date1,Date date2) 
    { 
     // if you already have date objects then skip 1 
     //1 

     //1 

     //date object is having 3 methods namely after,before and equals for comparing 
     //after() will return true if and only if date1 is after date 2 
     if(date1.after(date2)){ 
      System.out.println("Date1 is after Date2"); 
     } 

     //before() will return true if and only if date1 is before date2 
     if(date1.before(date2)){ 
      System.out.println("Date1 is before Date2"); 
     } 

     //equals() returns true if both the dates are equal 
     if(date1.equals(date2)){ 
      System.out.println("Date1 is equal Date2"); 
     } 

     System.out.println(); 
    } 
} 
9

Phân tích hai ngày firstDatesecondDate sử dụng SimpleDateFormat.

firstDate.after(secondDate);

firstDate.before(secondDate);

8

Phân tích các chuỗi ký tự vào ngày, sau đó so sánh sử dụng compareTo, before hoặc sau

Date d = new Date(); 
d.compareTo(anotherDate) 

tức là

Date date1 = new SimpleDateFormat("MM/dd/yyyy").parse(date1string) 
Date date2 = new SimpleDateFormat("MM/dd/yyyy").parse(date2string) 

date1.compareTo(date2); 

javadoc cho compareTo http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#compareTo(java.util.Date)

+2

Trả về 'giá trị 0 nếu đối số Ngày bằng Ngày này; một giá trị nhỏ hơn 0 nếu ngày này là trước đối số ngày; và giá trị lớn hơn 0 nếu ngày này sau đối số ngày.' và 'NullPointerException - nếu anotherDate là null.' –

Các vấn đề liên quan