Muaj ntau txoj hauv kev los sib piv ob hnub hauv Java hom lus programming. Hauv cov khoos phis tawj, hnub tim tau sawv cev los ntawm tus lej (hom ntaub ntawv Ntev) hauv cov sijhawm ntawm lub sijhawm - uas yog, tus naj npawb ntawm milliseconds uas tau dhau mus txij li Lub Ib Hlis 1, 1970. Hauv Java, Hnub tim yog qhov khoom, uas txhais tau tias nws muaj ntau txoj hauv kev los ua kev sib piv. Txhua txoj hauv kev siv los sib piv ob hnub yog qhov tseem ceeb sib piv cov chav nyob ntawm ob hnub.
Kauj ruam
Txoj Kev 1 ntawm 4: Siv pivTo
Kauj Ruam 1. Siv pivTo
Hnub tim cov khoom siv sib piv yog li 2 hnub tuaj yeem sib piv tau ncaj qha nrog kev sib piv piv. Yog tias ob hnub muaj tib tus lej hauv cov sijhawm, tom qab ntawd txoj kev rov qab xoom. Yog hnub thib ob tsawg dua thawj zaug, tus nqi tsawg dua xoom rov qab los. Yog tias hnub thib ob loj dua thawj zaug, txoj hauv kev rov qab tus nqi ntau dua xoom. Yog tias ob hnub yog tib yam, tom qab ntawv txoj kev yuav rov qab los tsis muaj nuj nqis.
Kauj Ruam 2. Tsim ntau yam khoom Hnub
Koj yuav tsum tsim ntau yam khoom Hnub Ua ntej muab piv rau lawv. Ib txoj hauv kev yooj yim tshaj plaws los ua qhov no yog siv chav kawm SimpleDateFormat. Hoob no ua rau nws yooj yim los hloov tus lej nkag hnub tim rau hauv Cov khoom Hnub.
SimpleDateFormat sdf = tshiab SimpleDateFormat ("yyyy-MM-dd"). Txhawm rau tshaj tawm tus nqi hauv ib qho tshiab '' Hnub Tim '', siv tib hom ntawv thaum tsim hnub. Hnub tim 1 = sdf.parse ("1995-02-23"); // date1 yog Lub Ob Hlis 23, 1995 Hnub tim 2 = sdf.parse ("2001-10-31"); // date2 yog Lub Kaum Hli 31, 2001 Hnub tim 3 = sdf.parse ("1995-02-23"); // hnub 3 yog Lub Ob Hlis 23, 1995
Kauj Ruam 3. Sib piv Cov Khoom Hnub
Cov cai hauv qab no yuav qhia koj piv txwv rau txhua kis - tsawg dua, sib npaug, thiab ntau dua.
date1.compareTo (hnub tim 2); // date1 <date2, xa tus nqi tsawg dua 0 date2.compareTo (date1); // date2> date1, xa tus nqi ntau dua 0 date1.compareTo (date3); // date1 = date3, yog li nws yuav tso tawm 0 ntawm console
Txoj Kev 2 ntawm 4: Siv Sib npaug, Tom Qab thiab Ua Ntej
Kauj Ruam 1. Siv qhov sib npaug, tom qab thiab ua ntej
Cov hnub tuaj yeem sib piv siv qhov sib npaug, tom qab, thiab ua ntej txoj hauv kev. Yog tias ob hnub muaj tus nqi zoo ib yam hauv lub sijhawm, txoj kev sib npaug rov qab muaj tseeb. Cov piv txwv hauv qab no yuav siv Hnub tim cov khoom tsim hauv qhov piv txwv piv rau txoj kev.
Kauj Ruam 2. Sib piv nrog cov txheej txheem ua ntej
Cov cai hauv qab no qhia txog qhov piv txwv uas rov muaj tseeb thiab tsis tseeb. Yog tias hnub tim 1 yog hnub ua ntej hnub tim 2, ua ntej txoj hauv kev rov qab los tiag. Txwv tsis pub, ua ntej txoj kev rov qab cuav.
System.out.print (date1.before (date2)); // tso saib tus nqi '' tseeb '' System.out.print (date2.before (date2)); // rov qab tus nqi '' tsis tseeb ''
Kauj Ruam 3. Sib piv siv cov txheej txheem tom qab
Cov cai hauv qab no qhia txog qhov piv txwv uas rov muaj tseeb thiab tsis tseeb. Yog tias hnub 2 yog hnub tom qab hnub tim 1, tom qab txoj kev rov qab muaj tseeb. Txwv tsis pub, tom qab txoj kev yuav rov qab cuav.
System.out.print (date2.after (date1)); // tso saib tus nqi '' tseeb '' System.out.print (date1.after (date2)); // tso saib tus nqi '' cuav ''
Kauj Ruam 4. Sib piv nrog cov txheej txheem sib npaug
Cov cai hauv qab no qhia txog qhov piv txwv uas rov muaj tseeb thiab tsis tseeb. Yog tias ob hnub sib npaug, txoj kev sib npaug rov qab muaj tseeb. Txwv tsis pub, txoj kev sib npaug rov qab cuav.
System.out.print (date1.equals (date3)); // tso saib tus nqi '' tseeb '' System.out.print (date1.equals (date2)); // tso saib tus nqi '' cuav ''
Txoj Kev 3 ntawm 4: Siv Daim Ntawv Teev Sijhawm Kawm
Kauj Ruam 1. Siv Class Calendar
Cov Hnub Qub kuj tseem muaj pivTo, sib npaug, tom qab, thiab ua ntej cov txheej txheem uas ua haujlwm zoo ib yam li cov tau piav qhia ua ntej rau Hnub Kawm Hnub. Yog li yog cov ntaub ntawv hnub tau khaws tseg hauv Class Calendar, koj tsis tas yuav rho tawm hnub tsuas yog ua qhov sib piv.
Kauj Ruam 2. Tsim ib qho piv txwv ntawm Calendar
Txhawm rau siv cov txheej txheem hauv Class Calendar, koj yuav tsum tsim ntau yam xwm txheej Calendar. Hmoov zoo, koj tuaj yeem siv qhov txiaj ntsig los ntawm qhov piv txwv hnub tsim yav dhau los.
Daim ntawv qhia hnub cal1 = Calendar.getInstance (); // tshaj tawm cal1 Calendar cal2 = Calendar.getInstance (); // tshaj tawm cal2 Calendar cal3 = Calendar.getInstance (); // tshaj tawm cal3 cal1.setTime (date1); // muab hnub tim rau hauv cal1 cal2.setTime (date2); cal3.setTime (hnub tim 3);
Kauj Ruam 3. Sib piv cal1 thiab cal2 siv cov txheej txheem ua ntej
Cov cai hauv qab no yuav tso tawm tus nqi ntawm tr
System.out.print (cal1.before (cal2)); // yuav rov qab tus nqi '' tseeb ''
Kauj Ruam 4. Sib piv cal1 thiab cal2 siv cov txheej txheem tom qab
Cov cai hauv qab no yuav rov qab tsis raug vim cal1 yog hnub ua ntej cal2.
System.out.print (cal1.after (cal2)); // rov qab tus nqi '' tsis tseeb ''
Kauj Ruam 5. Sib piv cal1 thiab cal2 siv txoj hauv kev sib npaug
Cov cai hauv qab no yuav qhia ib qho piv txwv uas rov muaj tseeb thiab tsis tseeb. Lub xeev nyob ntawm daim ntawv piv txwv piv. Cov cai hauv qab no yuav rov qab tus nqi "muaj tseeb", tom qab ntawd "cuav" ntawm kab tom ntej.
System.out.println (cal1.equals (cal3)); // rov qab tus nqi '' tseeb '': cal1 == cal3 System.out.print (cal1.equals (cal2)); // rov qab tus nqi '' tsis tseeb '': cal1! = cal2
Txoj kev 4 ntawm 4: Siv getTime
Kauj Ruam 1. Siv getTime
Koj tseem tuaj yeem sib piv ncaj qha lub sijhawm ntsuas qhov tseem ceeb ntawm ob hnub, txawm hais tias ob txoj hauv kev dhau los yuav yooj yim nyeem thiab nyiam dua. Txoj hauv kev no koj yuav piv 2 hom ntaub ntawv qub, yog li koj tuaj yeem siv cov haujlwm "", thiab "==".
Kauj Ruam 2. Tsim lub sijhawm khoom hauv hom lej ntev
Ua ntej koj tuaj yeem sib piv cov hnub tim, koj yuav tsum tsim Tus Nqi Ntev Ntev los ntawm cov khoom hnub dhau los. Hmoov zoo, txoj kev getTime () yuav ua rau koj.
sijhawm ntev1 = getTime (hnub tim 1); // tshaj tawm thawj lub sijhawm 1 ntawm hnub 1 ntev ntev2 = getTime (date2); // tshaj tawm thawj lub sijhawm 2 tus nqi ntawm hnub 2
Kauj Ruam 3. Ua qhov tsawg dua kev sib piv
Siv qhov tsawg dua (<) ua haujlwm los sib piv ob qhov tseem ceeb no. Txij li lub sijhawm 1 tsawg dua sijhawm 2, thawj cov lus yuav tshwm. Lwm nqe lus suav nrog ua kom tiav cov syntax.
yog (time1 <time2) {System.out.println ("date1 yog hnub ua ntej date2"); // yuav qhia vim time1 <time2} lwm {System.out.println ("date1 tsis yog hnub ua ntej date2"); }
Kauj Ruam 4. Ua kom muaj ntau dua li kev sib piv
Siv qhov ntau dua (>) ua haujlwm los sib piv ob qhov tseem ceeb no. Vim tias lub sijhawm 1 loj dua lub sijhawm 2, thawj cov lus yuav tshwm. Lwm nqe lus suav nrog ua kom tiav cov syntax.
yog (time2> time1) {System.out.println ("date2 yog hnub tom qab date1"); // yuav qhia vim time2> time1} lwm {System.out.println ("date2 tsis yog hnub tom qab date1"); }
Kauj Ruam 5. Ua qhov sib piv sib npaug
Siv cov haujlwm ua haujlwm los txheeb xyuas qhov sib npaug ntawm qhov muaj nqis (==) los sib piv cov lej ob no. Txij li lub sijhawm 1 sib npaug rau lub sijhawm 3, thawj cov lus yuav tshwm. Yog tias txoj haujlwm khiav mus rau lwm nqe lus, nws txhais tau tias ob zaug tsis muaj tus nqi zoo ib yam.
yog (time1 == time2) {System.out.println ("ob hnub yog tib yam"); } lwm {System.out.println ("Qhov thib 1 tsis zoo ib yam li qhov thib ob"); // yuav qhia vim lub sijhawm 1! = sijhawm 2}