2013-02-18 44 views
5

Giả sử rằng tôi có hai ngày như sau.So sánh các ngày bỏ qua giây và mili giây giây của DateTime trong Joda

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata")); 
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45"); 
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45"); 

tôi muốn so sánh hai thời điểm này để xem liệu firstDate là sớm hơn, sau đó hoặc bằng secondDate.

Tôi có thể thử cách sau.

System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isBefore(secondDate)); 
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isAfter(secondDate)); 
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.equals(secondDate)); 

Mã sản phẩm này chính xác là những gì tôi muốn.

Nó tạo ra kết quả sau.

firstDate = 2012-02-16T12:03:45.000+05:30 
secondDate = 2013-02-17T12:03:45.000+05:30 
comparison = true 

firstDate = 2012-02-16T12:03:45.000+05:30 
secondDate = 2013-02-17T12:03:45.000+05:30 
comparison = false 

firstDate = 2012-02-16T12:03:45.000+05:30 
secondDate = 2013-02-17T12:03:45.000+05:30 
comparison = false 

tôi cần phải bỏ qua những giây và phần nghìn giây của những ngày này. Tôi đã cố gắng sử dụng các phương pháp withSecondOfMinute(0)withMillis(0) như sau.

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata")); 
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillis(0); 
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillis(0);   

Nhưng nó mang lại kết quả như sau.

firstDate = 1970-01-01T05:30:00.000+05:30 
secondDate = 1970-01-01T05:30:00.000+05:30 
comparison = false 

firstDate = 1970-01-01T05:30:00.000+05:30 
secondDate = 1970-01-01T05:30:00.000+05:30 
comparison = false 

firstDate = 1970-01-01T05:30:00.000+05:30 
secondDate = 1970-01-01T05:30:00.000+05:30 
comparison = true 

Tài liệu của phương pháp withSecondOfMinute() mô tả.

Trả lại bản sao của ngày giờ này với trường thứ hai của phút được cập nhật. DateTime là không thay đổi, do đó, không có phương pháp thiết lập. Thay vào đó, phương thức này trả về một phiên bản mới với giá trị của giây phút đã thay đổi.

Và tài liệu của phương pháp withMillis() nói.

Trả lại bản sao của thời gian này với các millis khác nhau. Đối tượng trả về sẽ là một phiên bản mới hoặc điều này. Chỉ có milli sẽ thay đổi, thời gian và múi giờ được lưu giữ.

So sánh ngày bằng cách bỏ qua phần thời gian hoàn toàn có thể dễ dàng thực hiện bằng cách sử dụng DateTimeComparator.getDateOnlyInstance() tương tự như sau.

if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)==0){} 
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)<0){} 
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)>0){} 

Cách so sánh hai ngày bỏ qua khoảnh khắc cụ thể trong DateTime (giây và mili giây trong trường hợp này)?

Trả lời

6

Tôi nghĩ rằng bạn muốn sử dụng DateTime#withMillisOfSecond() thay vì DateTime#withMillis():

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata")); 
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillisOfSecond(0); 
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillisOfSecond(0); 

Thiết DateTime#withMillis()-0, sẽ thiết lập lại cả ngày của bạn để 1/1/1970, và do đó bạn sẽ có được true cho equals gọi vào chúng.

Tương tự, hãy đặt DateTime#withMillisOfDay() thành 0, sẽ đặt thời gian thành midnight.

+0

Cảm ơn bạn rất nhiều. Làm xong! – Tiny

+0

@Tiny. Bạn được chào đón :) –

8

Một cách khác là để tạo ra một DateTimeComparator với phút là giới hạn dưới:

DateTimeComparator comparator = DateTimeComparator.getInstance(
     DateTimeFieldType.minuteOfHour()); 

mà sẽ bỏ qua phần thứ hai và phần nghìn giây của các đối tượng được so sánh.

+0

Tôi sẽ thử phương pháp này sau. Cảm ơn bạn. – Tiny

0
public static String getFromatDateTime(Date date) { 
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); 
    final GregorianCalendar gc = new GregorianCalendar(); 
    gc.setTime(date); 
    //gc.set(Calendar.HOUR_OF_DAY, 0); 
    //gc.set(Calendar.MINUTE, 0); 
    //block ignore second and millisecond 
    gc.set(Calendar.SECOND, 0); 
    gc.set(Calendar.MILLISECOND, 0); 
    String strDate = sdfDate.format(gc.getTime()); 
    return strDate; 
} 
public static void main(String[] args) throws ParseException { 
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); 
    Date now = new Date(); 
    String currentDate = Testing.getFromatDateTime(now); 
    String fullDate = "2015-12-07 14:53:39.30"; 
    String effDateStr = Testing.getFromatDateTime(sdfDate.parse(fullDate)); 

    System.out.println("Currennt Date: " + currentDate); 
    System.out.println("Effective Date: " + effDateStr); 
    System.out.println(currentDate.compareTo(effDateStr)==0); 
} 
Các vấn đề liên quan