2011-10-26 8 views
39

मैं एक निश्चित तारीख से वर्षों में उम्र निर्धारित करने की कोशिश कर रहा हूं। क्या किसी को एंड्रॉइड में ऐसा करने का एक साफ तरीका पता है? मेरे पास जावा एपीआई स्पष्ट रूप से उपलब्ध है, लेकिन सीधा-अप जावा एपीआई बहुत कमजोर है, और मैं उम्मीद कर रहा था कि एंड्रॉइड में मेरी मदद करने के लिए कुछ है।मुझे दो तिथियों के बीच वर्षों की संख्या कैसे मिल सकती है?

संपादित करें: एंड्रॉइड में जोडा समय का उपयोग करने के लिए कई सिफारिशें मुझे Android Java - Joda Date is slow और संबंधित चिंताओं के कारण थोड़ा सा चिंता करती हैं। साथ ही, किसी लाइब्रेरी में खींचने के लिए मंच के साथ भेज दिया गया है, जो इस आकार के लिए संभवतः अधिक है।

+0

ऐसा ही एक सवाल: http://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java – jeha

उत्तर

72
public static int getDiffYears(Date first, Date last) { 
    Calendar a = getCalendar(first); 
    Calendar b = getCalendar(last); 
    int diff = b.get(YEAR) - a.get(YEAR); 
    if (a.get(MONTH) > b.get(MONTH) || 
     (a.get(MONTH) == b.get(MONTH) && a.get(DATE) > b.get(DATE))) { 
     diff--; 
    } 
    return diff; 
} 

public static Calendar getCalendar(Date date) { 
    Calendar cal = Calendar.getInstance(Locale.US); 
    cal.setTime(date); 
    return cal; 
} 
+4

मासिक, वर्ष और DATE को स्थिरांक क्या हैं ?? –

+4

@ChrisSim: वे कैलेंडर के मासिक आयात हैं। माह और इसी तरह। – sinuhepop

+1

मुझे लगता है कि DATE की बजाय यह DAY_OF_MONTH होना चाहिए। – jiahao

14

मैं जावा में संबंधित सभी तारीखों के लिए महान Joda-Time लाइब्रेरी का उपयोग करने की अनुशंसा करता हूं।

आपकी आवश्यकताओं के लिए आप Years.yearsBetween() विधि का उपयोग कर सकते हैं।

+0

मैं Joda एक बहुत यह पिछले परियोजना का उपयोग किया है । यह वास्तव में स्थापित करने के मामले में मार डाला गया है ... – StarWind0

+1

बस विस्तृत करने के लिए: 'सार्वजनिक int getYears (org.java.util.Date समय) { org.joda.time.DateTime अब = org.joda.time। DateTime.now(); org.joda.time.DateTime तो = new org.joda.time.DateTime (time.getTime()); वापसी org.joda.time.Years.years बीच (अब, फिर) .getYears(); } ' – Nielsvh

0

यदि आप जावा के कैलेंडर का उपयोग करके इसकी गणना नहीं करना चाहते हैं तो आप Androids Time class का उपयोग कर सकते हैं यह तेजी से होना चाहिए, लेकिन जब मैंने स्विच किया तो मुझे बहुत अंतर नहीं आया।

मुझे एंड्रॉइड में उम्र के लिए 2 तिथियों के बीच समय निर्धारित करने के लिए कोई पूर्व परिभाषित कार्य नहीं मिला। DateUtils में तिथियों के बीच स्वरूपित समय प्राप्त करने के लिए कुछ अच्छे सहायक कार्य हैं, लेकिन संभवतः यह संभव नहीं है कि आप क्या चाहते हैं।

0

मुझे पता है तुम एक साफ समाधान के लिए कहा है, लेकिन यहाँ दो गंदा एक बार कर रहे हैं:

 static void diffYears1() 
{ 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
    Calendar calendar1 = Calendar.getInstance(); // now 
    String toDate = dateFormat.format(calendar1.getTime()); 

    Calendar calendar2 = Calendar.getInstance(); 
    calendar2.add(Calendar.DAY_OF_YEAR, -7000); // some date in the past 
    String fromDate = dateFormat.format(calendar2.getTime()); 

    // just simply add one year at a time to the earlier date until it becomes later then the other one 
    int years = 0; 
    while(true) 
    { 
     calendar2.add(Calendar.YEAR, 1); 
     if(calendar2.getTimeInMillis() < calendar1.getTimeInMillis()) 
      years++; 
     else 
      break; 
    } 

    System.out.println(years + " years between " + fromDate + " and " + toDate); 
} 

static void diffYears2() 
{ 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
    Calendar calendar1 = Calendar.getInstance(); // now 
    String toDate = dateFormat.format(calendar1.getTime()); 

    Calendar calendar2 = Calendar.getInstance(); 
    calendar2.add(Calendar.DAY_OF_YEAR, -7000); // some date in the past 
    String fromDate = dateFormat.format(calendar2.getTime()); 

    // first get the years difference from the dates themselves 
    int years = calendar1.get(Calendar.YEAR) - calendar2.get(Calendar.YEAR); 
    // now make the earlier date the same year as the later 
    calendar2.set(Calendar.YEAR, calendar1.get(Calendar.YEAR)); 
    // and see if new date become later, if so then one year was not whole, so subtract 1 
    if(calendar2.getTimeInMillis() > calendar1.getTimeInMillis()) 
     years--; 

    System.out.println(years + " years between " + fromDate + " and " + toDate); 
} 
-1

इस प्रयास करें:

int getYear(Date date1,Date date2){ 
     SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy"); 
     Integer.parseInt(simpleDateformat.format(date1)); 

     return Integer.parseInt(simpleDateformat.format(date2))- Integer.parseInt(simpleDateformat.format(date1)); 

    } 
+1

@ जॉनी ग्रेबर ने यह बताने की देखभाल की कि आपने इसे क्यों उठाया ... ehem ... suboptimal उत्तर? – kleopatra

+0

इसके रूप में डाउनवॉटेड दिन को ध्यान में नहीं लेता है। – leparlon

1

मैं जाहिरा तौर पर, अभी तक कोई टिप्पणी नहीं कर सकते हैं, लेकिन मुझे लगता है कि आप कसरत में केवल DAY_OF_YEAR का उपयोग कर सकते हैं यदि आपको एक वर्ष नीचे समायोजित करना चाहिए (वर्तमान सर्वोत्तम उत्तर से कॉपी और संशोधित)

public static int getDiffYears(Date first, Date last) { 
    Calendar a = getCalendar(first); 
    Calendar b = getCalendar(last); 
    int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR); 
    if (a.get(Calendar.DAY_OF_YEAR) > b.get(Calendar.DAY_OF_YEAR)) { 
     diff--; 
    } 
    return diff; 
} 

public static Calendar getCalendar(Date date) { 
    Calendar cal = Calendar.getInstance(Locale.US); 
    cal.setTime(date); 
    return cal; 
} 

इसी प्रकार आप शायद समय के एमएस प्रतिनिधित्व को अलग कर सकते हैं और एक वर्ष में एमएस की संख्या से विभाजित हो सकते हैं। बस सबकुछ लंबे समय तक रखें और यह अधिकतर समय (लीप साल, आउच) के लिए पर्याप्त होना चाहिए, लेकिन यह आपके आवेदन पर वर्षों की संख्या के लिए निर्भर करता है और उस कार्यकर्ता को मौसम कितना प्रदर्शन करना पड़ता है, यह उस तरह के हैक के लायक होगा।

+0

DAY_OF_YEAR का उपयोग करके लीप वर्षों के साथ भी त्रुटियां होती हैं। – sinuhepop

0
// int year =2000; int month =9 ; int day=30; 

    public int getAge (int year, int month, int day) { 

      GregorianCalendar cal = new GregorianCalendar(); 
      int y, m, d, noofyears;   

      y = cal.get(Calendar.YEAR);// current year , 
      m = cal.get(Calendar.MONTH);// current month 
      d = cal.get(Calendar.DAY_OF_MONTH);//current day 
      cal.set(year, month, day);// here ur date 
      noofyears = y - cal.get(Calendar.YEAR); 
      if ((m < cal.get(Calendar.MONTH)) 
          || ((m == cal.get(Calendar.MONTH)) && (d < cal 
              .get(Calendar.DAY_OF_MONTH)))) { 
        --noofyears; 
      } 
      if(noofyears < 0) 
        throw new IllegalArgumentException("age < 0"); 
      System.out.println(noofyears); 
      return noofyears; 
1

यहाँ मुझे लगता है कि एक बेहतर तरीका है है:

public int getYearsBetweenDates(Date first, Date second) { 
    Calendar firstCal = GregorianCalendar.getInstance(); 
    Calendar secondCal = GregorianCalendar.getInstance(); 

    firstCal.setTime(first); 
    secondCal.setTime(second); 

    secondCal.add(Calendar.DAY_OF_YEAR, 1 - firstCal.get(Calendar.DAY_OF_YEAR)); 

    return secondCal.get(Calendar.YEAR) - firstCal.get(Calendar.YEAR); 
} 

संपादित

अलावा एक बग जो मैं तय, इस विधि लीप वर्ष के साथ अच्छी तरह से काम नहीं करता है से। यहां एक पूर्ण परीक्षण सूट है। मुझे लगता है कि आप स्वीकृत उत्तर का उपयोग कर बेहतर हैं।

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.GregorianCalendar; 

class YearsBetweenDates { 
    public static int getYearsBetweenDates(Date first, Date second) { 
     Calendar firstCal = GregorianCalendar.getInstance(); 
     Calendar secondCal = GregorianCalendar.getInstance(); 

     firstCal.setTime(first); 
     secondCal.setTime(second); 

     secondCal.add(Calendar.DAY_OF_YEAR, 1 - firstCal.get(Calendar.DAY_OF_YEAR)); 

     return secondCal.get(Calendar.YEAR) - firstCal.get(Calendar.YEAR); 
    } 

    private static class TestCase { 
     public Calendar date1; 
     public Calendar date2; 
     public int expectedYearDiff; 
     public String comment; 

     public TestCase(Calendar date1, Calendar date2, int expectedYearDiff, String comment) { 
      this.date1 = date1; 
      this.date2 = date2; 
      this.expectedYearDiff = expectedYearDiff; 
      this.comment = comment; 
     } 
    } 

    private static TestCase[] tests = { 
     new TestCase(
       new GregorianCalendar(2014, Calendar.JULY, 15), 
       new GregorianCalendar(2015, Calendar.JULY, 15), 
       1, 
       "exactly one year"), 
     new TestCase(
       new GregorianCalendar(2014, Calendar.JULY, 15), 
       new GregorianCalendar(2017, Calendar.JULY, 14), 
       2, 
       "one day less than 3 years"), 
     new TestCase(
       new GregorianCalendar(2015, Calendar.NOVEMBER, 3), 
       new GregorianCalendar(2017, Calendar.MAY, 3), 
       1, 
       "a year and a half"), 
     new TestCase(
       new GregorianCalendar(2016, Calendar.JULY, 15), 
       new GregorianCalendar(2017, Calendar.JULY, 15), 
       1, 
       "leap years do not compare correctly"), 
    }; 

    public static void main(String[] args) { 
     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
     for (TestCase t : tests) { 
      int diff = getYearsBetweenDates(t.date1.getTime(), t.date2.getTime()); 
      String result = diff == t.expectedYearDiff ? "PASS" : "FAIL"; 
      System.out.println(t.comment + ": " + 
        df.format(t.date1.getTime()) + " -> " + 
        df.format(t.date2.getTime()) + " = " + 
        diff + ": " + result); 
     } 
    } 
} 
+0

यह –

+0

काम नहीं कर रहा प्रतीत होता है कि एक दिन से संबंधित एक बग था। लेकिन यह भी छलांग के वर्षों के साथ अच्छी तरह से काम नहीं करता है। कृपया मेरा संपादन देखें। – SnakE

4

tl; डॉ

ChronoUnit.YEARS.between(LocalDate.of(2010 , 1 , 1) , LocalDate.now(ZoneId.of("America/Montreal"))) 

java.time

वर्ष तिथि-समय वर्गों वास्तव में बुरा है, इतना बुरा है कि दोनों सूर्य & ओरेकल उन्हें java.time साथ प्रतिस्थापित करने के लिए सहमत हैं कक्षाएं। यदि आप डेट-टाइम मानों के साथ कोई महत्वपूर्ण कार्य करते हैं, तो अपनी परियोजना में लाइब्रेरी जोड़ना सार्थक है।जोडा-टाइम लाइब्रेरी अत्यधिक सफल और अनुशंसित थी, लेकिन अब रखरखाव मोड में है। टीम जावा.टाइम कक्षाओं में माइग्रेशन की सलाह देती है।

java.time कार्यक्षमता की ज्यादातर ThreeTen-Backport में जावा 6 & से 7 वापस भेजा जाता है और आगे (How to use… देखें) ThreeTenABP में Android के लिए अनुकूलित।

LocalDate start = LocalDate.of(2010 , 1 , 1) ; 
LocalDate stop = LocalDate.now(ZoneId.of("America/Montreal")); 
long years = java.time.temporal.ChronoUnit.YEARS.between(start , stop); 

कंसोल पर डंप करें।

System.out.println("start: " + start + " | stop: " + stop + " | years: " + years) ; 

शुरू: 2010-01-01 | रोकें: 2016-09-06 | साल: 6

संबंधित मुद्दे

 संबंधित मुद्दे