A cikin duniyar yau, ฦididdige shekaru daga ฦayyadaddun kwanan wata aiki ne na gama gari, musamman a aikace-aikacen da tabbatar da shekaru ko sanarwar ranar haihuwa muhimmin fasali ne. Sau da yawa muna buฦatar ฦididdige shekarun mutum bisa ranar haihuwarsa. A cikin wannan labarin, za mu tattauna yadda ake lissafin shekaru daga kwanan wata da aka bayar ta amfani da Java. Za mu yi bayani mataki-mataki kan lambar, da zurfafa cikin fannoni daban-daban da suka shafi matsalar, kamar ษakunan karatu da ayyukan da ke ciki.
Don magance matsalar ฦididdige shekaru daga kwanan wata, za mu iya amfani da ginanniyar azuzuwan Java kamar LocalDate da Period. Wadannan azuzuwan wani bangare ne na Java Time API da aka gabatar a Java 8, wanda aka tsara shi don sauฦaฦe lissafin kwanan wata da lokaci.
import java.time.LocalDate; import java.time.Period; public class AgeCalculator { public static void main(String[] args) { LocalDate birthDate = LocalDate.of(1990, 1, 1); LocalDate currentDate = LocalDate.now(); int age = calculateAge(birthDate, currentDate); System.out.println("Age: " + age); } public static int calculateAge(LocalDate birthDate, LocalDate currentDate) { Period period = Period.between(birthDate, currentDate); return period.getYears(); } }
Bari mu rushe lambar mataki-mataki. Na farko, muna shigo da azuzuwan da ake buฦata, LocalDate da Period. Sai mu ฦirฦiri wani aji mai suna AgeCalculator tare da babbar hanyar da ke fara ranar haihuwa da kwanan watan ta amfani da ajin LocalDate. Ana kiran hanyar 'calculateAge' tare da samar da ranar haihuwa da kwanan wata a matsayin muhawara.
A cikin hanyar 'calculateAge', muna amfani da hanyar'Period.between()', wanda ke ฦididdige lokaci tsakanin kwanakin biyun. A ฦarshe, muna dawo da bambancin shekaru ta hanyar kiran hanyar ''getYears()' akan lokacin ฦididdigewa.
API ษin Lokacin Java
The API ษin Lokacin Java, wanda kuma aka sani da Java Date and Time API, babban ษakin karatu ne da aka gabatar a cikin Java 8 don sarrafa kwanan wata da ayyuka masu alaฦa. An ฦera shi don ya zama mai fahimta, mai ฦarfi, da sauฦin amfani fiye da wanda ya gabace shi, java.util.Date da azuzuwan Kalanda, waษanda ke da batutuwa masu yawa da iyakancewa.
Wasu mahimman fasalulluka na API Time Java sun haษa da:
- Azuzuwan da ba za a iya canzawa ba kuma mai aminci.
- Share rarrabuwa tsakanin kwanan wata da lokaci da mutum zai iya karantawa da wakilcin lokacin inji.
- API mai sassauฦa da tsawaitawa don tallafawa kalanda daban-daban da tsarin kiyaye lokaci.
- Ginin tallafi don yankunan lokaci da lokacin ceton hasken rana.
Amfani da Lokaci da Kwanan Gida
The Period aji a Java yana wakiltar lokacin da aka bayyana a cikin shekaru, watanni, da kwanaki. Aji ne mai amfani don ฦididdige bambanci tsakanin kwanakin biyu, kamar yadda aka nuna a misalin lissafin shekarunmu.
The Kwanan wata gida aji, a gefe guda, yana wakiltar kwanan wata ba tare da lokaci da bayanin yanki-lokaci ba. Yana da amfani don wakiltar kwanakin haihuwa, kwanakin abubuwan da suka faru, ko kowace rana inda bayanin lokaci bai zama dole ba.
A cikin misalin lissafin shekarun mu, mun yi amfani da hanyar `Period.between()` don ฦididdige bambanci tsakanin lokuta biyu na LocalDate - ranar haihuwa da kwanan wata na yanzu. Abun da aka samu na Zamani yana ba mu bambancin shekaru, watanni, da ranaku tsakanin kwanakin biyu, yana sauฦaฦa ฦididdige shekarun mutum.
A ฦarshe, ฦididdige shekaru daga kwanan wata ana iya samun sauฦi ta amfani da ginanniyar azuzuwan Java kamar LocalDate da Period. Waษannan azuzuwan, tare da Faษin Lokacin API na Java, suna ba da mafita mai ฦarfi da sassauฦa don sarrafa lissafin kwanan wata da lokaci a cikin aikace-aikacen Java ษinku.