An warware: shekarun java daga kwanan wata

shekaru daga kwanan wata 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.

Shafi posts:

Leave a Comment